Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 160 down vote accepted
+250

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}

In a recent answer I used

\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

\leavevmode\smash{g}

(the \leavevmode can be omitted in the middle of a paragraph, but it must be remembered that \smash doesn't start one, by itself).

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

\leavevmode\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{<math>} is similar to \mathmbox[0pt][r]{<math>}
  • \mathclap{<math>} is similar to \mathmbox[0pt][c]{<math>}
  • \mathrlap{<math>} is similar to \mathmbox[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.

share|improve this answer
2  
+1 for diligence ;-) BTW: \vcenter is only available in math mode. Maybe minipage can be added? –  Marco Daniel Nov 23 '12 at 12:50
2  
Also lrbox here? –  Joseph Wright Nov 23 '12 at 12:54
1  
+1 even though you don't mention .\unsetbox(0.0+0.0)x50.0 :-) –  David Carlisle Nov 23 '12 at 13:08
3  
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
1  
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.