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
:
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{<math>}
is similar to \mathmakebox[0pt][r]{<math>}
\mathclap{<math>}
is similar to \mathmakebox[0pt][c]{<math>}
\mathrlap{<math>}
is similar to \mathmakebox[0pt][l]{<math>}
The last three, however, accept an optional argument; for instance
\mathclap[\scriptstyle]{<math>}
typesets the formula <math>
pretending that it doesn't occupy any space, sticking out half to the left and half to the right, but forcing \scriptstyle
; with \mathclap{<math>}
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 height plus depth is above the formula axis (where fraction lines lie).
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.
Best Answer
\hbox
is a TeX primitive and\mbox
is a LaTeX macro defined viaThe
\leavevmode
means that it starts a paragraph, compare:with
The other difference is that the argument is parsed as a normal macro argument, so for example
\mbox a
works and is identical to\mbox{a}
whereas\hbox a
is a syntax error.\mbox{\verb|{|}
will lead to errors but\hbox{\verb|{|}
will box a verbatim{
.If you do use a brace group for the argument it has to be explicit
{}
(or other characters with catcodes 1 and 2) but\hbox
can use implicit braces such as\hbox\bgroup
...\egroup
(in particular you can start a box in one macro and end it in another).Don't use
\hbox
in a LaTeX document (although you will often see it in internal package code).As requested, some further details on
\leavevmode
:After the first
compare
is a paragraph break, so TeX goes into vertical mode. In vertical mode, boxes are stacked vertically. The next two boxes are\hbox
es withone
andtwo
so they are appended vertically to the current vertical list. Then comes the lettert
ofthree
; letters are not allowed in vertical mode so TeX pushes it back into the input and instead starts a new paragraph, adds the paragraph indentation and then sees thethree
again in horizontal mode.After the first
with
there is again a paragraph break, but this time TeX sees\leavevmode
(or more exactly the\unhbox
in its definition), so it starts a new paragraph, adds the indentation and then sees\hbox{one}
in horizontal mode.\leavevmode
does nothing in h-mode so after the glue from the word space resulting from the end-of-line character, TeX sees\hbox{two}
, which comes horizontally afterone
, andthree
follows in the same paragraph.After the second
compare
, TeX is again in vertical mode, but this time the paragraph is started byzero
, so TeX is already in horizontal mode before it sees the\hbox
, so\hbox
and\mbox
act the same way.Basically LaTeX goes to some lengths to never expose the primitive TeX box behaviour: all LaTeX boxes start with
\leavevmode
so that they act like\mbox
and not\hbox
. (The difference is basically the same as the difference between\parbox
(LaTeX) and\vbox
(TeX) and\rule
(LaTeX) and\hrule
(TeX) for example.)