LaTeX/Boxes
This page may need to be updated to reflect current knowledge. You can help update it, discuss progress, or request assistance. |
This module may require a complete rewrite in order to suit its intended audience. You can help rewrite it. Please see the relevant discussion. |
TeX boxes and glue overview
editA box is the TeX term for an invisible container that can hold a visible element, nothing, or other boxes. Glue is the TeX term for an invisible connector that determines the relative position of joined boxes. Each separate visible element contained within a TeX document is contained within a box. A visible element can be a letter, image, geometric shape, etc. TeX builds pages by gluing boxes together according to the default TeX rules, default LaTeX rules, or document commands. In a typical document, letter boxes are glued to other letter boxes to form words, which are then elastically glued to other words to form sentences. Sentences are broken into lines and placed in paragraph boxes. Elastic glue is squeezed or stretched to fully justify lines within paragraph boxes. Paragraph boxes are glued to diagram boxes, and so on.
While it is true that boxes can hold other boxes, not all commands that can generate boxes can be used within all other commands that can generate boxes. There are often workarounds for these limitations.
The size of a box is typically related to the size and position of its contents, but it doesn't have to be. Many box commands accept custom widths and/or heights, and there are other commands that affect the shape and position of boxes. Boxes are placed relative to other boxes, while visible elements are placed relative to the boxes which contain them.
A more complete description of boxes and glue can be found in chapters 11 and 12 of Donald E. Knuth's, 'The TeXbook'. A list of basic LaTeX box and glue commands can be found at http://www.personal.ceu.hu/tex/spacebox.htm .
boxes
editcharacter boxes
editTeX character boxes have three dimensional properties:
- The height is the length between the baseline and the top of the box.
- The depth is the length between the baseline and the bottom of the box.
- The width is the width of the box.
Character boxes are glued together at the baseline.
parbox, minipage, and pbox
editA \parbox
is a box of specific width formatted in paragraph mode. In paragraph mode, text is broken into lines and lines are broken into pages.
\parbox[pos][height][contentpos]{width}{text}
width defines the width of the paragraph box. Text will be broken into lines so that it fits within this width. Besides fixed lengths, you can also provide user defined length macros or TeX/LaTeX defined length macros and primitives such as \width
, \height
, \depth
and \totalheight
. (See character box above for explanations of depth and height.)
height defines the height of the \parbox
.
pos selects which baseline to join. It can be top, bottom, or center. This parameter is often confusing to new users! See the special note below.
contentpos positions the contents of the box within the box. It can be one of center, top, bottom or spread. Note that contentpos has no effect if the box is not larger than the text it contains.
\pbox
is available in the pbox package. A \pbox
has the same parameters as a \parbox
, but if the user provided width parameter is larger than the actual contents of the pbox the pbox shrinks to fit the content. This is only useful if the content contains manual line breaks and you wish to fit the resulting material.
\pbox[pos][height]{width}{text}
The minipage environment takes the same parameters as a \parbox
, and behaves nearly identically to it. The difference between a minipage and a \parbox
is that a \parbox
can only contain a single paragraph and you cannot use all commands and environments inside it, while a minipage may contain multiple paragraphs and, in fact, almost anything.
\begin{minipage}[pos][height][contentpos]{width} text \end{minipage}
You can make use of minipage, \parbox
, and \pbox
to embed paragraphs in non-paragraph boxes. For instance:
\fbox{%
\parbox{\textwidth}{
Some very long text\\
that would not be allowed\\
in an fbox.
}%
}
|
special notes on the pos parameter
editThe pos alignment parameter does not refer to the \parbox
's borders when contentpos is either missing or equal to pos and anytime pos= center! Under any of these circumstances the alignment parameter selects which line of text within the \parbox
will be used to align the paragraph box. The \parbox
is placed so the baseline of that chosen line of text is aligned to the baseline of the box that the \parbox
is glued to. Thus, if the pos is set top, the baseline of the top line of text in the \parbox
will line up with the baseline outside of the \parbox
. In the special case of a parbox that has only one line of text, that one and only line of text is the top, bottom, and center line of text simultaneously, and changing pos will appear to do nothing unless additional text is added.
If the contentpos is present and not the same as pos and pos is not center, the \parbox
will align at its borders.
makebox and mbox
editMakebox creates a single-line box, optionally of fixed width, but otherwise large enough to hold its contents. Note that the width does not have to be wider than the contents: for instance, setting width to 0 typesets the content without changing the current position. (E.g., this would allow for an overstrike.) Makebox is typically used to prevent hyphenation (see Hyphenation) or simply to keep text that belongs together from being placed on separate lines. You cannot place line breaks (\\) within a Makebox. mbox is the shorthand no-option version of Makebox.
\mbox{text}
\makebox[width][pos]{text}
|
The pos parameter takes a one letter value: center, flushleft, flushright, or spread the text to fill the box.
\makebox[0pt]{Some text} over this text
\makebox[15ex][s]{Censored text}\hspace{-15ex}\makebox[15ex][s]{X X X X X}
Text \makebox[2\width][r]{running away}
|
framebox and fbox
editThe command \framebox
behaves identically to \makebox
except that it additionally draws a box around its contents.
\fbox{text}
\framebox[width][pos]{text}
|
The following example shows you some things you could do with the \makebox
and \framebox
commands:
\makebox[\textwidth]{c e n t r a l} \par
\makebox[\textwidth][s]{s p r e a d} \par
\framebox[1.1\width]{Guess I'm framed now!} \par
\framebox[0.8\width][r]{Bummer, I am too wide} \par
\framebox[1cm][l]{never mind, so am I}
Can you read this?
|
You can tweak the following frame lengths.
\fboxsep
: the distance between the frame and the content.\fboxrule
: the thickness of the rule.
This prints a thick and more distant frame:
\setlength{\fboxsep}{10pt}
\setlength{\fboxrule}{5pt}
\fbox{A frame.}
|
This shows the box frame of a letter.
\setlength{\fboxsep}{0pt}
\fbox{A}
|
The framed package is available that adds the framed environment which provides an an easy way to frame a paragraph within a document:
\usepackage{framed}
% ...
\begin{framed}
This is an easy way to box text within a document!
\end{framed}
|
savebox/usebox/newsavebox
editA savebox is a non-rendered box that is saved for later repeated rendering via the usebox command.
\newsavebox{\boxname}
\savebox{\boxname}{some content}
\usebox{\boxname}
|
The command \newsavebox
creates a placeholder for storing content;
the command \savebox
stores the specified content in the placeholder without rendering it in the document; and \usebox
renders the content of the placeholder into the document.
colorbox and fcolorbox
editSee Colors.
\fcolorbox
can also be tweaked with \fboxsep
and \fboxrule
.
fancybox
editThe fancybox package provides additional boxes.
\doublebox
\ovalbox
\shadowbox
box modifiers
editraisebox
editNow that we control the horizontal, the obvious next step is to go for the vertical. No problem for LaTeX. The
\raisebox{lift}[height][depth]{text}
|
command lets you define the vertical properties of a box. You can use \width
, \height
, \depth
and \totalheight
in the first three parameters, in order to act upon the size of the box inside the text argument. The two optional parameters set for the height and depth of the raisebox. For instance you can observe the difference when embedded in a framebox.
\raisebox{0pt}[0pt][0pt]{\Large%
\textbf{Aaaa\raisebox{-0.3ex}{a}%
\raisebox{-0.7ex}{aa}%
\raisebox{-1.2ex}{r}%
\raisebox{-2.2ex}{g}%
\raisebox{-4.5ex}{h}
}
}
he shouted but not even the next
one in line noticed that something
terrible had happened to him.
|
rotatebox
editSee Rotations.
resizebox and scalebox
editThe graphicx package features additional boxes.
\resizebox{10ex}{2\baselineskip}{Dunhill style}
\scalebox{10}{Giant}
|