\mbox
& \makebox
¶Synopsis, one of:
\mbox{text} \makebox{text} \makebox[width]{text} \makebox[width][position]{text}
Create a box, a container for material. The text is typeset in
LR mode (see Modes) so it is not broken into lines. The
\mbox
command is robust, while \makebox
is fragile
(see \protect
).
Because text
is not broken into lines, you can use \mbox
to prevent hyphenation. In this example, LaTeX will not hyphenate
the tank name, ‘T-34’.
The soviet tank \mbox{T-34} is a symbol of victory against nazism.
The first two command invocations shown, \mbox
and
\makebox
, are roughly the same. They create a box just wide
enough to contain the text. (They are like plain TeX’s
\hbox
.)
In the third version the optional argument width specifies the width of the box. Note that the space occupied by the text need not equal the width of the box. For one thing, text can be too small; this creates a full-line box:
\makebox[\linewidth]{Chapter Exam}
with ‘Chapter Exam’ centered. But text can also be too wide for width. See the example below of zero-width boxes.
In the width argument you can use the following lengths that refer
to the dimension of the box that LaTeX gets on typesetting
text: \depth
, \height
, \width
,
\totalheight
(this is the box’s height plus its depth). For
example, to make a box with the text stretched to double the natural
size you can say this.
\makebox[2\width]{Get a stretcher}
For the fourth command synopsis version the optional argument position gives position of the text within the box. It may take the following values:
c
The text is centered (default).
l
The text is flush left.
r
Flush right.
s
Stretch the interword space in text across the entire width.
The text must contain stretchable space for this to work. For
instance, this could head a press release:
\noindent\makebox[\textwidth][s]{\large\hfil IMMEDIATE\hfil
RELEASE\hfil}
A common use of \makebox
is to make zero-width text boxes. This
puts the value of the quiz questions to the left of those questions.
\newcommand{\pts}[1]{\makebox[0em][r]{#1 points\hspace*{1em}}} \pts{10}What is the air-speed velocity of an unladen swallow? \pts{90}An African or European swallow?
The right edge of the output ‘10 points ’ (note the ending space
after ‘points’) will be just before the ‘What’. You can use
\makebox
similarly when making graphics, such as in TikZ
or Asymptote
, where you put the edge of the text at a known
location, regardless of the length of that text.
For boxes with frames see \fbox
& \framebox
. For colors
see Colored boxes.
There is a related version of \makebox
that is used within the
picture
environment, where the length is given in terms of
\unitlength
(see \makebox
(picture)).
As text is typeset in LR mode, neither a double backslash
\\
nor \par
will give you a new line; for instance
\makebox{abc def \\ ghi}
outputs ‘abc defghi’ while
\makebox{abc def \par ghi}
outputs ‘abc def ghi’, both on
a single line. To get multiple lines see \parbox
and minipage
.