\mbox{text} for an unbreakable inline box, \fbox{text} for a framed box, \makebox[width][pos]{text} or \framebox[width][pos]{text} for a fixed-width single-line box, and \parbox{width}{text} for a multi-line text box. To draw an empty box of fixed width and height, place an invisible strut such as \rule{0pt}{0.9cm} inside \framebox[3.5cm][c]{...}, or put \hfill inside \fbox{\parbox[c][0.9cm]{3.5cm}{\hfill}} so the empty paragraph box keeps its full width.
Text boxes, framed boxes, and empty boxes in LaTeX
For measurement units you can pass as widths and heights (
pt, cm, mm, em, \textwidth), see LaTeX lengths and units.
latex-boxes.tex
Ready to use this syntax? Continue in the browser editor when you want to adapt the example in a real project.Open LaTeX Cloud Studio
Keep \setlength{\fboxsep}{...} and \setlength{\fboxrule}{...} inside a braced group ({...}) when you only want to change the padding or border thickness of one box.
The TeX box model: hbox, vbox, width, height, and depth
Every page TeX builds is composed of nested rectangular boxes:- Horizontal boxes (
hbox) arrange items left to right along a baseline (glyphs in a word, words in a line,\mbox,\makebox,\fbox). - Vertical boxes (
vbox) stack items top to bottom (lines in a paragraph,\parbox,minipage, and the page itself).
- Width (
\wd): horizontal distance from the left edge to the right edge. - Height (
\ht): vertical distance from the baseline up to the top edge. - Depth (
\dp): vertical distance from the baseline down to the bottom edge (for descenders such asg,p, andy).
\newsavebox and \sbox, inspect its dimensions with \the\wd, \the\ht, and \the\dp, and typeset it with \usebox.
box-dimensions.tex
Area has 0.0pt depth because none of its letters descend below the baseline, whereas Typography has a positive depth from y, p, and g. The second part shows how [t], [c], and [b] align the top, vertical center, or bottom line of a \parbox with the surrounding baseline.
Inspecting TeX boxes with LuaLaTeX
When you compile with LuaLaTeX, Lua code can read TeX box registers directly throughtex.box[n]. LuaTeX stores dimensions in scaled points (1pt = 65536sp), and node.traverse(box.head) walks the internal node list (glyphs, glue, kerns, or lines) inside the box.
In LaTeX Cloud Studio, projects compile on TeX Live 2026 (full distribution); to run LuaLaTeX explicitly, open Settings → General and change LaTeX Engine from Auto (Recommended) to LuaLaTeX (PDFLaTeX and XeLaTeX are also available).
luatex-inspect-box.tex
luacode package provides luacode*, which keeps Lua backslashes literal so you do not need to double-escape TeX commands inside Lua strings. (LuaLaTeX defaults to OpenType Latin Modern rather than Computer Modern TFM fonts, which accounts for the slight metric differences compared with the pdfLaTeX measurements above.)
Common mistakes
- Using
\\or blank lines inside\mbox,\fbox,\makebox, or\framebox: All four build horizontal boxes and ignore line breaks. Wrap a\parbox{width}{...}(orminipage) inside\fbox{...}for multi-line framed text. - Overfull
\hboxfrom a wide\mboxor\fbox: Because horizontal boxes cannot break across lines, any box wider than\textwidthprotrudes into the right margin. - Running
\directluaorluacodeunder pdfLaTeX: Lua box inspection requires the LuaLaTeX engine; under pdfLaTeX,\directluafails withUndefined control sequenceand\usepackage{luacode}stops withPackage luacode Error: LuaTeX is required for this package. Aborting..
Try it in the editor
Start from the Basic Article template in LaTeX Cloud Studio and replacemain.tex with the complete example (latex-boxes.tex or box-dimensions.tex for pdfLaTeX, or luatex-inspect-box.tex after selecting LuaLaTeX in project settings).
