# What are the different kinds of boxes in (La)TeX?

I've seen hbox, vbox, mbox, parbox and others used in various code samples. What are the different kinds of box in (La)Tex? Which are suitable for use in LaTeX, as opposed to plain TeX?

The primitive TeX commands for building boxes are

• \hbox, horizontal box
• \vbox, vertical box with reference point at the last line inside it
• \vtop, vertical box with reference point at the first line inside it
• \vcenter, vertical box with reference point in the middle (almost)

Their usage in LaTeX documents is not encouraged, but when programming some macro they can be handier than the LaTeX substitutes. Of course one should know that some risks are next door, when using low level commands.

The LaTeX commands for horizontal boxes are

• \mbox{<text>} (analogous to \hbox), horizontal box with natural size of <text>

• \makebox[<width>][<alignment>]{<text>}, horizontal box where the first optional argument sets the final width, independently of the natural width of <text> (which is nonetheless available as \width); the second optional argument is a character l c r or s that specifies the alignment of <text> with respect to the stated width

• \sbox{<bin>}{<text>} and \savebox{<bin>}[<width>][<alignment>]{<text>} are analogous to \mbox and \makebox respectively, but store the box in the <bin> declared in advance with \newsavebox

• \fbox{<text>} and \framebox[<width>][<alignment>]{<text>} are analogous to \mbox and \makebox respectively, but also draw a frame around the built box

The LaTeX commands for vertical boxes are

• \parbox[<alignment>]{<width>}{<text>}, vertical box
• \begin{minipage}[<alignment>]{<width>}, vertical box

The optional <alignment> is c (default), t or b, that states the alignment of the box with respect to the context. They are implemented with \vcenter, \vtop and \vbox respectively.

There are other optional arguments, however:

\parbox[<alignment>][<height>][<inner arrangement>]{<width>}{<text>}


The <height> must be a length which tells LaTeX to build the box as if its vertical dimension were the stated length. The <inner arrangement> must be c, t, b or s (by default the stated or implicit <alignment> is used), that tells how to vertically arrange the text in the available space; s means "spread": all flexible glue available will be used for getting the first line at the top and the last line at the bottom of the available space.

The same optional arguments are available for minipage, with the same meaning. The differences of boxes built with \parbox and minipage are quite subtle and not very relevant for a general discussion:

\begin{minipage}[<alignment>][<height>][<inner arrangement>]{<width>}
<text>
\end{minipage}


I used the following in a recent answer

\parbox[t]{0pt}{...}


so that the box is aligned with respect to its top line, but its vertical size is computed as zero; it was to be used inside a tabular environment, where other cells provide a vertical size. This of course can be accomplished with \vtop, but remembering the details and the correct placement of \vss is always a pain: using \parbox is much handier.

There's also a very useful environment:

\begin{lrbox}{<bin>}
<text>
\end{lrbox}


that's pretty analogous to \sbox{<bin>}{<text>}, but allows \begin{lrbox}{<bin>} to be in the "start" code of a newly defined environment and the \end{lrbox} in the "finish" code. The built box will be printed (or enclosed in another box) with \usebox{<bin>}. (Note: never use \lrbox and \endlrbox, unless you know well what you're doing.)

A rather similar construction is \raisebox:

\raisebox{<lift>}[<height>][<depth>]{<text>}


that works similarly to \mbox (that is, it builds a horizontal box), but also raises it by the dimension specified in <lift>. The optional arguments are used to correct the idea TeX gets of this lifted box. For instance, the height and depth of a "g" are (in the ten point Computer Modern font), 4.3pt and 2pt respectively (the depth is how much the glyph sticks out under the baseline). If we do

\raisebox{3pt}{g}


the height will be 7.3pt and the depth zero (it doesn't become negative). With \raisebox{-5pt}{g} the height would become zero and the depth 7pt. With \raisebox{3pt}[1pt][2pt]{g} the height (as perceived by TeX) would be 3pt and the depth 2pt.

An interesting application is to make for a zero height and depth box (thanks to A.Ellet):

\raisebox{0pt}[0pt][0pt]{g}


would make TeX thinks that the "g" doesn't occupy any space. A similar effect can be obtained (more efficiently, but perhaps in a less LaTeX way) with

\smash{g}


(a \leavevmode used to be necessary in older versions of LaTeX, before 2018-12-01, if used at the beginning of a paragraph).

Finally the syntax

\makebox(<x>,<y>){<text>}


builds a box of width <x> multiples of \unitlength (default 1pt) and height <y> multiples of \unitlength (so <x> and <y> can be decimal numbers, but not dimensions, unless the picture package is loaded). It's been thought mainly for the picture environment, but it can be used anywhere. The effect of

\makebox(0,0){some text}


is the same as

\smash{\makebox[0pt]{some text}}


but easier to type. The position of the reference point can be modified from the default "center and baseline of <text>" with an optional argument (see the LaTeX manual or the LaTeX Companion for details).

Another reason for using the LaTeX commands is that they are "color safe". I won't mention all the possibilities given by the color or xcolor package for drawing colored boxes, but I want to recall that the code for \makebox and the like has already a protection mechanism for colors, which the primitive commands don't have.

In many occasions the varwidth environment provided by the package with the same name (by Donald Arsenau) reveals useful; it's used by the standalone package and class, for instance. The environment has precisely the same syntax as minipage

\begin{varwidth}[<alignment>][<height>][<inner arrangement>]{<width>}
<text>
\end{varwidth}


with the difference that if one specifies explicit line breaks in the <text>, the width of the box will be the maximum width of the obtained lines or the specified <width>, whichever is smaller.

It should be noted that minipage has the same behavior when it contains only a tabbing environment, so varwidth can be seen as a generalization of this.

Yet another type of box making commands is provided by mathtools:

• \mathmbox
• \mathmakebox

have the same syntax as \mbox and \makebox respectively, but typeset their argument in math mode, respecting the size (normal, first level sub/superscripts, second level sub/superscripts). There are also variants:

• \clap{<text>} is similar to \makebox[0pt][c]{<text>} (complementing \llap and \rlap)
• \mathllap{[itex]} is similar to \mathmakebox[0pt][r]{[itex]}
• \mathclap{[itex]} is similar to \mathmakebox[0pt][c]{[itex]}
• \mathrlap{[itex]} is similar to \mathmakebox[0pt][l]{[itex]}

The last three, however, accept an optional argument; for instance

\mathclap[\scriptstyle]{[itex]}


typesets the formula [itex] pretending that it doesn't occupy any space, sticking out half to the left and half to the right, but forcing \scriptstyle; with \mathclap{[itex]} one would obtain whatever style is implied by TeX rules. With \mathllap the math material occupies no space and all sticks to the left of the current position, with \mathrlap to the right. See Alexander Perlis's article on TUGboat for more details.

## Addendum: a very short introduction to \hbox and \vbox and plain TeX boxes

The basic constructions \hbox and \vbox are TeX primitives, so use them with care. The second type is easier: it's essentially the same model as the standard typesetting, with the difference that a single object is produced. Paragraphs are typeset according to the current \hsize (which can be reset manually after \vbox{ and will be restored at the end, because the contents of both \hbox and \vbox forms a group).

Conversely, the contents of \hbox is typeset as a single horizontal unit, with no line break.

One should be aware that \hbox and \vbox don't change the mode TeX is in at the moment they are processed. This can be confusing at the beginning: something like

<blank line>
\hbox{abc}\hbox{def}


will stack the two boxes vertically, with interline glue between them:

abc
def

If one does

\vbox{
\hbox{abc}
\hbox{def}
}


the result will be similar; it's important, though, because the width of a \vbox is taken as the widest width of an \hbox inside it (possibly after paragraphing has been performed).

Both constructions allow a <box specification>; for instance

\hbox to 1cm{abc def}


will stretch (or shrink) the available glue for getting a box 1cm wide. Alternatively

\hbox spread 1cm{abc def}


will make a box 1cm wider than its “natural width”, by stretching (or shrinking if the dimension is negative) the available glue.

The same specifications can be given to \vbox, with the difference that the height is modified.

The reference point of a \vbox is the reference point of the last item in it.

The alternative \vtop behaves essentially like \vbox, but the reference point is the reference point of the first item in it. When a <box specification> is given, the depth is modified.

The primitive \vcenter can only be used in math mode; it behaves like \vbox, but eventually the reference point is set so that half the total width plus depth is above the formula axis.

The plain TeX macros \llap and \rlap are defined in terms of \hbox

\def\llap#1{\hbox to 0pt{\hss#1}}
\def\rlap#1{\hbox to 0pt{#1\hss}}


where \hss is essentially equivalent to \hskip 0pt plus 1fil minus 1fil. The argument to \llap will be typeset in a zero width box sticking to the left of it. Similarly for \rlap. A macro \clap could be obtained in a similar way (see above at mathtools); plain TeX hasn't it, but it has \line as a shorthand for \hbox to \hsize and \centerline

\def\centerline#1{\line{\hss#1\hss}}


so the material will be centered on the line, sticking outside the line length on either side if oversized.

A word of notice: \llap, \rlap and \centerline are also defined in LaTeX, but they have a peculiar behavior in that, differently from their LaTeX counterparts \makebox[0pt][r]{...}, \makebox[0pt][l]{...} and \makebox[\columnwidth]{...} they don't start a paragraph if found in vertical mode.

• Also lrbox here? – Joseph Wright Nov 23 '12 at 12:54
• +1 even though you don't mention .\unsetbox(0.0+0.0)x50.0 :-) – David Carlisle Nov 23 '12 at 13:08
• You mentioned all my favorites except \raisebox{<lift>}[<height>][<depth>]{text}. In my work, I occasionally need boxes of zero width and on a very rare occasion a box with zero height or depth. \raisebox very nicely creates boxes with zero height. As far as I know---short of tinkering with box dimension in TeX---this is the only way to achieve this effect in LaTeX. Someone PLEASE correct me if I am wrong. – A.Ellett Nov 23 '12 at 15:59
• @ted That comment was just a jibe at egreg really: claiming he'd missed a case. You would just see log files as unsetbox(0.0+0.0) is the tracing format used if you turn on tracing into the log file. An unset box is a special kind of box used internally in tabular alignments to hold the contents of a cell before TeX has decided the column width, you can not explicitly make one other than by & stuff & and TeX holds stuff in an unset box until it has finished all the rows. If it hadn't been egreg's answer I would not have mentioned it:-) – David Carlisle Sep 12 '13 at 9:37
• @egreg Not only are you an expert but kind too, sorry for having bothered you with the comment! – 0fnt Apr 26 at 18:23