Boxes

Ask what TeX's single most fundamental idea is, and the answer is not fonts, not mathematics, not the line-breaking algorithm — it is the box. Everything LaTeX puts on a page is a box: a rectangle with a width, a height and a depth, glued to its neighbours, and never looked inside again once it has been assembled. That is why \mbox stops a word being hyphenated, why an overflowing line is reported as an Overfull \hbox, and why \parbox and minipage part company where they do. This page walks through the box commands you actually type — \mbox, \makebox, \framebox, \raisebox, \parbox, minipage, \newsavebox — starting from the one idea underneath all of them.

Width, height and depth: why a box has three dimensions

A box has three dimensions, not two: width, height and depth. Height is measured upward from the baseline, depth downward from it — so a box is not a rectangle drawn around the ink but a rectangle hung on the line the letters sit on. \the\wd, \the\ht and \the\dp print the three values. Box the word Hx in 10pt Computer Modern and its depth is 0.0pt; box Hxg and the depth becomes 1.94444pt, because the g reaches below the baseline. The width grew as well, of course — but the depth grew for a completely different reason.

This one fact answers a puzzle almost everyone meets before they know what a box is: why does \fbox{Hx} sit tight around the letters while \fbox{Hxg} looks as if it has sagged? Because \fbox frames the box, not the letters, and the box of Hxg is 1.94pt deeper. Line up a row of framed words in a table and the frames bob up and down with the descenders. The cure is a strut: \fbox{\strut Hx} and \fbox{\strut Hxg} come out with exactly the same height and depth, because \strut is an invisible zero-width box that claims 0.7 of the current \baselineskip above the baseline and 0.3 below it. Struts get a section of their own further down.

latex
\newsavebox{\probe}
\newcommand{\dims}[1]{\sbox{\probe}{#1}%
  \typeout{[#1] w=\the\wd\probe\space h=\the\ht\probe\space d=\the\dp\probe}}

\dims{Hx}    % w=12.77782pt  h=6.83331pt  d=0.0pt      <- no descender
\dims{Hxg}   % w=17.77783pt  h=6.83331pt  d=1.94444pt  <- the g adds depth

% same words framed: only the second frame sinks below the baseline
\fbox{Hx}\quad\fbox{Hxg}\quad \fbox{\strut Hx}\quad\fbox{\strut Hxg}

\mbox: how to stop a line break and hyphenation

\mbox{...} keeps its contents on one line and disables hyphenation inside them. The reason is the definition itself: LaTeX declares \mbox as nothing more than \leavevmode\hbox{#1}. An \hbox is a box that has already been assembled, and TeX never reaches back into a finished box to hunt for a break point or a hyphenation position. So \mbox{antidisestablishmentarianism} is not being politely asked not to break — it simply cannot break. LaTeX calls a box of this kind, which streams its contents left to right and never wraps, an LR box (its inside is in LR mode); \mbox, \makebox, \fbox and \framebox are the family, and they stand opposite the paragraph boxes, \parbox and minipage, which fix a width and wrap. TeX's whole worldview, that typesetting is boxes joined by glue, is condensed into this one command.

The price is that an unbreakable box can be too wide for the line, and then TeX complains. The message repays a literal reading: Overfull \hbox (29.62221pt too wide) in paragraph at lines 5--5. It names an \hbox rather than a line because every line TeX produces is an hbox — the paragraph breaker packs words and stretchable glue into one hbox per line, the model Donald Knuth and Michael Plass published in 1981 as “Breaking paragraphs into lines”. Add the draft class option and LaTeX sets \overfullrule to 5pt, printing a black bar in the margin beside every offending line; that bar is itself nothing but a filled box. Use \mbox for the things that genuinely must not split — a product name, a phone number, a value and its unit — and not as a blanket cure for ugly line breaks.

latex
% never split, never hyphenated
Released for \mbox{Mac OS X} and \mbox{Windows 11}; call \mbox{+81 3-1234-5678}.

% too wide for the measure -> Overfull \hbox (...pt too wide) in the log
\parbox{3.4cm}{aaa \mbox{antidisestablishmentarianism}}

% \documentclass[draft]{article} sets \overfullrule=5pt and marks the line

\makebox: choosing a width and an alignment, and the zero-width box

\makebox[width][position]{text} is \mbox with the choice of width taken out of TeX's hands and put into yours. [position] says where the contents sit inside the width you asked for: l flush left, r flush right, c centred (the default), and s stretches the interword glue so the contents fill the width exactly — \makebox[3cm][s]{a b c d} really does come out 3cm wide. Any other letter is accepted too, but LaTeX emits the warning Unexpected alignment and falls back to c.

Inside the width argument, four names stand for the natural size of whatever you are boxing: \width, \height, \depth and \totalheight (height plus depth). They are set from the contents before the width is evaluated, so \makebox[2\width]{word} measures word at 21.4167pt and then builds a box of 42.8334pt. It is the tidy way to say “twice as wide as this”, whatever this turns out to be. The same four names work in the arguments of \framebox, \savebox and \raisebox.

The most useful width of all is 0pt. A zero-width box is typeset but does not advance the current position, so it prints on top of whatever follows. \makebox[0pt][l]{...} hangs the contents to the right of the current point, \makebox[0pt][r]{...} to the left, and [c] straddles it. These two have short names — \rlap and \llap — and LaTeX defines them exactly that way, as \hbox to 0pt{#1\hss} and \hbox to 0pt{\hss#1}. Overstrikes, hanging punctuation, a mark in the margin and most home-made strike-through macros are all this single trick.

latex
\makebox[3cm][l]{left}|\makebox[3cm][r]{right}|
\makebox[3cm][s]{a b c d}|   % interword glue stretched to exactly 3cm
\makebox[2\width]{word}|      % twice the natural width of "word"

% zero width: the slash overprints the space that follows the X
X\rlap{/}\quad Y              % identical to X\makebox[0pt][l]{/}\quad Y

\fbox vs \framebox, and the two lengths that shape a frame

The difference between \fbox{text} and \framebox[width][position]{text} is exactly the difference between \mbox and \makebox: \fbox frames the contents at their natural width, \framebox frames them inside a width you choose, with the same l/c/r/s positions. Both rule all four sides of the box, and the look is governed by two lengths: \fboxrule, the thickness of the rule, is 0.4pt, and \fboxsep, the gap between rule and contents, is 3pt. The arithmetic checks out: Hx is 12.77782pt wide and \fbox{Hx} comes out at 19.5778pt — the difference is exactly twice 3.4pt. Say \setlength{\fboxsep}{0pt} and the frame hugs the letters.

\fbox is an LR box, so it frames one line. To frame a paragraph you frame a paragraph box\fbox{\parbox{6cm}{...}}. The outer box does not care what is inside it, only how big it is. That composability is the whole point of the box model, and it is also why a frame drawn around a \parbox inherits the descender problem from the section above; a \strut at the start and end of the boxed text keeps a stack of them level. Once you want rounded corners, a background colour or a frame that breaks across pages, that is the territory of packages such as framed and tcolorbox, covered on the Frames page.

latex
\setlength{\fboxrule}{1pt}   % default 0.4pt
\setlength{\fboxsep}{6pt}    % default 3pt
\fbox{one line}\quad\framebox[4cm][r]{4cm wide, flush right}

% frame a whole paragraph: put a paragraph box inside the frame
\fbox{\parbox{6cm}{\strut A framed paragraph. The frame follows the box,
and the struts keep its height independent of ascenders and descenders.\strut}}

The difference between \parbox and minipage

Both fix a width and let the text wrap inside it. The difference is not, as is often repeated, that \parbox holds only one paragraph. It holds as many as you like, and lists too — \parbox{4cm}{First.\par Second.} really does produce two paragraphs. The real difference is that a minipage is an environment and a \parbox is a command, and that decides what may legally appear inside. The signatures otherwise match: \parbox[position][height][inner-pos]{width}{text} and \begin{minipage}[position][height][inner-pos]{width}. Both reset \parindent and \parskip to zero and set \linewidth to the box width, which is why \includegraphics[width=\linewidth]{...} does the right thing inside either one.

[position] decides how the box meets the surrounding line, and it is the argument people misread. [t] does not align the top of the box: it puts the baseline of the box's first line on the surrounding baseline. Measure a three-line \parbox[t] and its height is 4.30554pt — the height of a lowercase o, not of the box — while its depth is 24pt, two full lines hanging below the baseline. [b] does the mirror image with the last line, and the default [c] centres the box on the line. So two [t] minipages line up on their first baselines, and if one of them begins with an image or a larger font the tops will still be uneven. The cure is a \strut at the start of each, or a \raisebox on one of them. The four-argument form \parbox[position][height][inner-pos]{width}{text} also forces a height, and [inner-pos] then says whether the contents sit at the top (t), the middle (c) or the bottom (b) of that height, or are stretched to fill it (s).

Where they really part company is what you may put inside. A \parbox's text is read as a macro argument, with category codes already fixed, so \parbox{5cm}{\begin{verbatim}...\end{verbatim}} fails outright with ! Argument of \@xverbatim has an extra }. — the same verbatim works inside a minipage. Footnotes are worse than an error: a \footnote inside a \parbox prints its mark and then loses the note text entirely, because the insertion is trapped in the box and never reaches the page. A minipage handles it deliberately, putting the note directly beneath itself with an italic lowercase letter from its own mpfootnote counter. Floats are out of bounds in both: a figure inside a minipage stops with ! LaTeX Error: Not in outer par mode. And neither can break across a page, because a box is by definition placed whole.

Put inside\parboxminipage
\parseveral paragraphs are fineseveral paragraphs are fine
itemizeworksworks (list depth is reset too)
verbatim! Argument of \@xverbatim has an extra }.works
\footnotemark prints, note text is lostappears below the box, italic letter
figurenot allowed! LaTeX Error: Not in outer par mode.
\parindentreset to 0reset to 0

The commonest job for a minipage is a pair of side-by-side columns: two of them at about 0.48 of \textwidth, with \hfill between. Keep the total under one or the second will not fit. And here is the pitfall that costs everyone half a day once: the newline in your source between \end{minipage} and \begin{minipage} is an ordinary space, so the two boxes are pushed apart by an interword space you never typed. End the line with a % and the space disappears. When the columns need to align by height, or you want text to flow around the box, move on to the Floats & placement page.

latex
\noindent
\begin{minipage}[t]{0.48\textwidth}
  \strut Left column. Several paragraphs, lists and a local footnote
  are all fine here.\footnote{Printed under this box, marked a, b, \dots}
\end{minipage}\hfill%   <- the % kills the newline; without it the boxes drift apart
\begin{minipage}[t]{0.48\textwidth}
  \strut Right column. [t] aligns the FIRST BASELINES, not the tops --
  the struts are what make the tops line up as well.
\end{minipage}

\raisebox: shifting a box, and lying about its size

\raisebox{lift}{text} moves its contents up by lift, or down if the value is negative, and it is the standard tool for lining up two things set at different sizes. The interesting part is the two optional arguments: \raisebox{lift}[height][depth]{text} makes LaTeX report that height and depth to its surroundings, whatever is actually drawn. \raisebox{0pt}[0pt][0pt]{\Huge Ag} still occupies its full 28.66008pt of width, yet measures 0pt high and 0pt deep (the truth is 16.99782pt and 4.83937pt), so the line spacing around it is computed as though nothing were there. That is how an ornament or an oversized glyph is dropped into running text without opening a gap, and the four shorthands \width, \height, \depth and \totalheight are available here too. Of course, a declaration that contradicts the ink will overlap its neighbours — so fake a size only where the overlap is the point.

\rule and the strut: an invisible box that reserves height

\rule[lift]{width}{height} draws a solid black rectangle — very nearly the only LaTeX command that puts ink on the page in the shape of a box itself. \rule{0.5\linewidth}{0.4pt} is a thin horizontal line half the measure. The optional [lift] shifts the rectangle relative to the baseline while keeping its size: \rule[-2pt]{10pt}{8pt} measures 10pt wide, 6pt high and 2pt deep — of the 8pt rectangle, 2pt hangs below the line.

Set the width to zero and the rule stops printing but goes on reserving space — that is a strut. \rule{0pt}{2.6ex} is invisible, 0pt wide and 11.19443pt tall; drop it at the start of a table row and that row is guaranteed to be at least that high. LaTeX's own \strut is a ready-made one sized to the current leading: at a \baselineskip of 12pt it measures 8.39996pt high and 3.60004pt deep — a 0.7/0.3 split of the line. That is why \fbox{\strut Hx} and \fbox{\strut Hxg} came out identical: the strut is taller and deeper than either word, so the descender no longer decides the size of the box. It is the standard fix for text crowding an \hline, and the tool for keeping a row of boxes on a common vertical rhythm.

latex
% a centred horizontal rule
\noindent\hfil\rule{0.5\linewidth}{0.4pt}\hfil

% a strut forces a taller, roomier table row
% (\strut is the ready-made one, sized to the current \baselineskip)
\begin{tabular}{|l|}
  \hline
  \rule{0pt}{2.6ex}Tall, uncramped row \\
  \hline
  \strut Same idea, sized to the current leading \\
  \hline
\end{tabular}

% put two differently sized boxes on one baseline
big \raisebox{-0.4ex}{\Huge A} small

Saving a box with \newsavebox, \sbox and \usebox

Material that appears more than once can be typeset once and then replayed. \newsavebox{\name} reserves a box register, \sbox{\name}{text} fills it — or \savebox{\name}[width][position]{text} if you want the \makebox-style form — and \usebox{\name} emits it. The contents are actually typeset at the moment of \sbox and never again, so a long table or an intricate diagram used in three places costs one typesetting pass, and all three copies are identical to the point.

Box registers are a finite resource, and for about two decades they were a scarce one. The original TeX had exactly 256 of them, numbered 0 to 255, and a document that allocated too many stopped with ! No room for a new \box. eTeX raised the ceiling to 32768 and LuaTeX to 65536, which is why nobody meets it any more — LaTeX picks the limit from the engine when the format is built, its internal \e@alloc@top coming out as 255, 32767 or 65535. Two notes on the interface: a save box is an LR box, so multi-line contents go in wrapped in a \parbox or minipage first, and the environment form \begin{lrbox}{\name} … \end{lrbox} is the one to use when the contents include verbatim. Older references warn that \savebox is fragile; in current LaTeX it is not\mbox, \makebox, \fbox, \parbox, \raisebox, \sbox and \savebox are all declared robust.

The companions to \usebox are \settowidth{\len}{text}, \settoheight and \settodepth, which typeset the text invisibly and store one dimension of the result in a length. They exist so that you can draw something to the exact size of something else — measure a signature line, then rule it with \rule.

latex
\newsavebox{\mylogo}
\sbox{\mylogo}{\fbox{\textbf{Draft}}}   % typeset once, here

Header: \usebox{\mylogo} \dots\ Footer: \usebox{\mylogo}

% verbatim inside a save box needs the environment form
\newsavebox{\snippet}
\begin{lrbox}{\snippet}
  \begin{minipage}{6cm}\begin{verbatim}int main(void) { }\end{verbatim}\end{minipage}
\end{lrbox}
\usebox{\snippet}

% measure a box into a length, then rule exactly that wide
\newlength{\siglen}
\settowidth{\siglen}{Signature}
Signature\par\rule{\siglen}{0.4pt}

Which box command to use when

CommandKindReach for it when
\mboxLRthis phrase must never be split
\makeboxLRyou want to choose the width, or overprint with [0pt]
\rlapLRshorthand for \makebox[0pt][l] (\llap hangs left)
\fboxLRa frame at the natural width
\frameboxLRa frame at a chosen width (tune \fboxsep, \fboxrule)
\parboxparagraphwrap text at a fixed width, e.g. inside a table cell
minipageparagraphyou need verbatim, footnotes or side-by-side columns
\raiseboxtransformshift up or down, or declare zero height and depth
\rulefilled boxdraw a line; zero width makes an invisible strut
\strutstruteven out the height of lines or table cells
\useboxstorageemit a box built with \newsavebox + \sbox
\settowidthmeasurestore the width (also \settoheight, \settodepth) in a length