At its core, LaTeX puts things in boxes and then puts the boxes on a page. So these commands are central.
There are many packages on CTAN that are useful for manipulating boxes. One useful adjunct to the commands here is adjustbox.
\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 table name, ‘T-4’.
See Table~\mbox{T-4}
The first two command versions, \mbox
and \makebox
, are
roughly equivalent. 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 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)
will be just before the ‘What’ (note the space after
‘points’). 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)).
If you put a double-backslash into text then LaTeX will not
give you a new line; for instance \makebox{abc def \\ ghi}
outputs ‘abc defghi’ while \makebox{abc def \par ghi}
outputs ‘abc def ghi’, but neither go to a second line. To get
multiple lines see \parbox and minipage.
\fbox
& \framebox
Synopses, one of:
\fbox{text} \framebox{text} \framebox[width]{text} \framebox[width][position]{text}
Create a box with an enclosing frame, four lines surrounding the space.
These commands are the same as \mbox
and \makebox
except
for the frame (see \mbox & \makebox). The \fbox
command is
robust, the \framebox
command is fragile (see \protect).
\fbox{Warning! No work shown, no credit given.}
LaTeX puts the text into a box that cannot be split or hyphenated. Around that box, separated from it by a small gap, are four lines making a frame.
The first two command invocations, \fbox{...}
and
\framebox{...}
, are roughly the same. As to the third and
fourth invocations, the optional arguments allow you to specify the box
width as width and the position of the text inside that box as
position. See \mbox & \makebox for the full description but
here is an example creating an empty box that is 1/4in wide.
\setlength{\fboxsep}{0pt}\framebox[0.25in]{\strut}}
The \strut
inserts a vertical height of \baselineskip
(see \strut).
These parameters determine the frame layout.
\fboxrule
The thickness of the lines around the enclosed box. The default is
0.2pt. Change it with a command such as
\setlength{\fboxrule}{0.8pt}
(see \setlength).
\fboxsep
The distance from the frame to the enclosed box. The default is 3pt.
Change it with a command such as \setlength{\fboxsep}{0pt}
(see \setlength). Setting it to 0pt is useful sometimes:
this will put a frame around the picture with no white border.
{\setlength{\fboxsep}{0pt} \framebox{% \includegraphics[width=0.5\textwidth]{prudence.jpg}}}
The extra curly braces keep the effect of the \setlength
local.
As with \mbox
and \makebox
, LaTeX will not break lines
in text. But this example has LaTeX break lines to make a
paragraph, and then frame the result.
\framebox{% \begin{minipage}{0.6\linewidth} My dear, here we must run as fast as we can, just to stay in place. And if you wish to go anywhere you must run twice as fast as that. \end{minipage}}
See Colored boxes for colors other than black and white.
The picture
environment has a version of this command where the
units depend on picture
’s \unitlength
(see \framebox (picture)).
\parbox
Synopses, one of:
\parbox{width}{contents} \parbox[position]{width}{contents} \parbox[position][height]{width}{contents} \parbox[position][height][inner-pos]{width}{contents}
Produce a box of text that is width wide. Use this command to make a box of small pieces of text, of a single paragraph. This command is fragile (see \protect).
\begin{picture}(0,0) ... \put(1,2){\parbox{1.75in}{\raggedright Because the graph is a line on this semilog paper, the relationship is exponential.}} \end{picture}
The contents are processed in a text mode (see Modes) so
LaTeX will break lines to make a paragraph. But it won’t make
multiple paragraphs; for that, use a minipage
environment
(see minipage).
The options for \parbox
(except for contents) are the same
as those for minipage
. For convenience a summary of the options
is here but see minipage for a complete description.
There are two required arguments. The width is a rigid length (see Lengths). It sets the width of the box into which LaTeX typesets contents. The contents is the text that is placed in that box. It should not have any paragraph-making components.
There are three optional arguments, position, height, and
inner-pos. The position gives the vertical alignment of the
parbox
with respect to the surrounding material. The possible
values are c
or m
to make the vertical center of the
parbox
lines up with the center of the adjacent line (this is the
default), or t
to match the top line of the parbox
with
the baseline of the surrounding material, or b
to match the
bottom line.
The optional argument height overrides the natural height of the box.
The optional argument inner-pos controls the placement of
content inside the parbox
. Its default is the value of
position. Its possible values are: t
to put the
content at the top of the box, c
to put it in the vertical
center, b
to put it at the bottom of the box, and s
to
stretch it out vertically (for this, the text must contain vertically
stretchable space).
\raisebox
Synopsis, one of:
\raisebox{distance}{text} \raisebox{distance}[height]{text} \raisebox{distance}[height][depth]{text}
Raise or lower text. This command is fragile (see \protect).
This example makes a command for the restriction of a function by lowering the vertical bar symbol.
\newcommand\restricted[1]{\raisebox{-.5ex}{$|$}_{#1}} $f\restricted{A}$
The first mandatory argument distance specifies how far to raise the second mandatory argument text. This is a rigid length (see Lengths). If it is negative then it lowers text. The text is processed in LR mode so it cannot contain line breaks (see Modes).
The optional arguments height and depth are dimensions. If they are specified, they override the natural height and depth of the box LaTeX gets by typesetting text.
In the arguments distance, height, and depth 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).
This will align two graphics on their top (see Graphics).
\usepackage{graphicx} \usepackage{calc} % in preamble ... \begin{center} \raisebox{1ex-\height}{% \includegraphics[width=0.4\linewidth]{lion.png}} \qquad \raisebox{1ex-\height}{% \includegraphics[width=0.4\linewidth]{meta.png}} \end{center}
The first \height
is the height of lion.png while the
second is the height of meta.png.
\sbox
& \savebox
Synopsis, one of:
\sbox{box-cmd}{text} \savebox{box-cmd}{text} \savebox{box-cmd}[width]{text} \savebox{box-cmd}[width][pos]{text}
Typeset text just as with \makebox
(see \mbox & \makebox) except that LaTeX does not output it but instead saves it
in a storage bin named box-cmd. The bin name box-cmd begins
with a backslash, \
. You must have previously allocated the bin
box-cmd with \newsavebox
(see \newsavebox).The
\sbox
command is robust while \savebox
is fragile
(see \protect).
This creates and uses a bin.
\newsavebox{\fullname} \sbox{\fullname}{John Jacob Jingleheimer Schmidt} ... \usebox{\fullname}! His name is my name, too! Whenever we go out, the people always shout! There goes \\usebox{\fullname}! Ya da da da da da da.
One advantage of using and reusing a bin over a \newcommand
is
efficiency, that LaTeX need not repeatedly retypeset the contents.
See the example below.
The first two command invocations,
\sbox{box-cmd}{text}
and
\savebox{box-cmd}{text}
, are roughly equivalent.
As to the third and fourth, the optional arguments allow you to specify
the box width as width, and the position of the text inside that
box as position. See \mbox & \makebox for the full
description.
In the \sbox
and \savebox
commands the text is
typeset in LR mode so it does not have line breaks (see Modes). If
you use these then LaTeX doesn’t give you an error but it ignores
what you want: if you enter \sbox{\newbin}{test \\ test}
and
\usebox{\newbin}
then you get ‘testtest’, while if you
enter \sbox{\newbin}{test \par test}
and
\usebox{\newbin}
then you get ‘test test’, but no error or
warning. To fix this use a \parbox
or minipage
as here.
\savebox{\abin}{% \begin{minipage}{\linewidth} \begin{enumerate} \item First item \item Second item \end{enumerate} \end{minipage}} ... \usebox{\abin}
As an example of the efficiency of reusing a bin’s contents, this puts the same picture on each page of the document by putting it in the header. LaTeX only typesets it once.
\usepackage{graphicx} % all this in the preamble \newsavebox{\sealbin} \savebox{\sealbin}{% \setlength{\unitlength}{1in}% \begin{picture}(0,0)% \put(1.5,-2.5){% \begin{tabular}{c} \includegraphics[height=2in]{companylogo.png} \\ Office of the President \end{tabular}} \end{picture}% } \markright{\usebox{\sealbin}} \pagestyle{headings}
The picture
environment is good for fine-tuning the placement.
If the bin has not already been defined then you get something like ‘Undefined control sequence. <argument> \nobin’.
lrbox
Synopsis:
\begin{lrbox}{box-cmd} text \end{lrbox}
The text inside the environment is saved in the bin
box-cmd
. The box-cmd must begin with a
backslash. You must create this bin in advance with \newsavebox
(see \newsavebox). This is the environment form of the \sbox
and \savebox
commands, and is equivalent to them. See \sbox & \savebox for the full information.
In this example the environment is convenient for entering the
tabular
.
\newsavebox{\jhbin} \begin{lrbox}{\jhbin} \begin{tabular}{c} \includegraphics[height=1in]{jh.png} \\ Jim Hef{}feron \end{tabular} \end{lrbox} ... \usebox{\jhbin}
\usebox
Synopsis:
\usebox{box-cmd}
Produce the box most recently saved in the bin box-cmd by the
commands \sbox
or \savebox
, or the lrbox
environment. See \sbox & \savebox for more information and examples.
(Note that box-cmd starts with a backslash.) This command is
robust (see \protect).