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?
[Tex/LaTex] What are the different kinds of boxes in (La)TeX
boxes
Related Solutions
I made the following diagram for the adjustbox
bundle. It will be part of the trimclip
manual (The trimclip
package will be an extracted part of the current adjustbox
package).
As you can see the text lies on the baseline, the height is everything above it and the depth everything below it. Usually only some lower-case character and punctuation go below it. Both together is the totalheight. Note that the coordinate system for the depth is going down, i.e. it is always positive. The width is the normal width of the content.
The macros in the document show how to access these values in LaTeX macros like \raisebox
(first line) or with a TeX box register called \br
here (second line).
There are further diagram which explain the trimming and clipping of boxes. The current work-in-progress version of the manual with this diagrams can be downloaded as PDF from the code repository. The source code for this and all other diagrams are also available.
\hbox
is a TeX primitive and \mbox
is a LaTeX macro defined via
\long\def\mbox#1{\leavevmode\hbox{#1}}
The \leavevmode
means that it starts a paragraph, compare:
\hbox{one}
\hbox{two}
three
with
\mbox{one}
\mbox{two}
three
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
:
\documentclass{article}
\begin{document}
compare
\hbox{one}
\hbox{two}
three
with
\mbox{one}
\mbox{two}
three
see?
compare
zero
\hbox{one}
\hbox{two}
three
with
zero
\mbox{one}
\mbox{two}
three
see?
\end{document}
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 with one
and two
so they are appended vertically to the current vertical list. Then comes the letter t
of three
; 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 the three
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 after one
, and three
follows in the same paragraph.
After the second compare
, TeX is again in vertical mode, but this time the paragraph is started by zero
, 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.)
Best Answer
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 characterl
c
r
ors
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 boxThe LaTeX commands for vertical boxes are
\parbox[<alignment>]{<width>}{<text>}
, vertical box\begin{minipage}[<alignment>]{<width>}
, vertical boxThe optional
<alignment>
isc
(default),t
orb
, 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:
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 bec
,t
,b
ors
(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
andminipage
are quite subtle and not very relevant for a general discussion:I used the following in a recent answer
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:
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
: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 dothe 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):
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
(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
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 thepicture
package is loaded). It's been thought mainly for thepicture
environment, but it can be used anywhere. The effect ofis the same as
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
orxcolor
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 thestandalone
package and class, for instance. The environment has precisely the same syntax asminipage
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 atabbing
environment, sovarwidth
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{<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
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 boxesThe 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 likewill stack the two boxes vertically, with interline glue between them:
If one does
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 instancewill stretch (or shrink) the available glue for getting a box 1cm wide. Alternatively
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
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 atmathtools
); plain TeX hasn't it, but it has\line
as a shorthand for\hbox to \hsize
and\centerline
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.