Your support for our advertisers helps cover the cost of hosting, research, and maintenance of this document

Formatting Information — An introduction to typesetting with LATEX

Chapter 4: Lists, tables, figures

Section 4.6: Boxes, sidebars, and panels

LATEX, like most typesetting systems, works by setting text into boxes. Each character is also a box, with a height and a width, just like it was in metal type; characters are assembled into lines, which are also boxes; and lines are assembled into pages, which are also boxes. The page-making mechanism also works like an old compositor’s galley (tray) from the days of metal type: the box accumulates lines of typeset text until it’s a bit longer than the height of the page. TEX then works out how much of it really will fit on the page, cuts it off and ships it out to the PDF file, and puts the remainder back into the galley (box) at the top, ready to start accumulating more material for the following page.

4.6.1 Boxes of text

Because of this ‘box model’, LATEX can typeset any text into a box of any width. The simplest command for small amounts of text is \parbox. This command needs two arguments in curly braces: the first is the width you want the text set to, and the second is the text itself, as in the example shown.

\parbox{3in}{Please make sure you send in your 
completed forms by January 1st next year, or the 
penalty clause in 2(a) will apply.}
Please make sure you send in your completed forms by January 1st next year, or the penalty clause in 2(a) will apply.

The text is typeset to the required width, and the box is extended downwards for as long as is required to fit the text. Note that the baseline of a \parbox is set to the midpoint of the box, so if you include a \parbox in mid-sentence, the centre of the box will be lined up with the line of type currently being set. You can specify that the top or bottom should be the alignment point by adding an optional t (top) or b (bottom) in square brackets before the width. For example, \parbox[t]{3in}{...} will produce a box with the baseline aligned with the top line of the text in the box.

Where the contents is more complex, use the minipage environment.

\begin{minipage}{4in}
  Please make sure you send in your completed forms 
  by January 1st next year, or the penalty clause 
  2(a) will apply:
  \begin{itemize}[noitemsep]
    \item Incomplete forms will be returned to you
          unprocessed.
    \item Forms must be accompanied by the correct fee.
    \item There is no appeal. The adjudicators' 
          decision is final.
  \end{itemize}
\end{minipage}

Please make sure you send in your completed forms by January 1st next year, or the penalty clause 2(a) will apply.

  • Incomplete forms will be returned to you unprocessed.

  • Forms must be accompanied by the correct fee.

  • There is no appeal. The adjudicators’ decision is final.

Notice that when setting very narrow measures with type that is too large, the words may not fit nicely and the spacing may become uneven or there may be too much hyphenation. Either use \raggedright or reduce the type size, or (in extreme cases) reword the text or break each line by hand. Fortunately, it is rare for LATEX to need this level of attention.

Within a minipage environment you can use virtually everything that occurs in normal text (eg lists, paragraphs, tabulations, etc) with the exception of floats like Tables and Figures. The minipage environment takes a compulsory argument just like \parbox does, and it means the same: the width you want the text set to.

Note that in both minipages and \parboxes, the paragraph indentation (\parindent) is reset to zero. If you need to change it, do so inside the minipage or \parbox using the \setlength command (see § 2.7 above).

Because a minipage is typeset independently from the rest of your text, any footnotes inside a minipage will be typeset at the end of the minipage, not at the foot of the containing page, and they will be done using lowercase letters by default, to keep them separate from the normal footnotes. We haven’t done footnotes yet, but they’re in § 5.1 below.

There are other ways of typesetting text to widths other than the normal text width: you can use a one-row, one-cell tabular environment with the p column type specification; or you can use the technique of setting the material into a special box that remembers it, and then emitting it where you want it (this is implemented by the standard lrbox environment or by the Sbox environment from the fancybox package, but these are advanced techniques).

4.6.2 Framed boxes

To put a frame round \fbox{some text}, use the \fbox command:

\fbox{some text}

We already saw this used in the Quick Start document and also to frame an image in Figure 4.3 above. For text, this works for a few words in mid-line, but the framed box and its contents won’t break over the end of a line. To typeset multiline text in a box, put it in a \parbox, or use a minipage or tabular environment as described above, and enclose the whole thing in a \fbox.

\fbox{\parbox{3in}{Please make sure you send in your 
completed forms by January 1st next year, or the 
penalty clause 2(a) will apply.}}

The spacing between text and box is controlled by the value of \fboxsep, and the thickness of the line by \fboxrule, both of which can be reset with the \setlength command.

If you are using colour, the xcolor package extends boxing to the \colorbox command, which takes two arguments: a colour name or code for the background colour, and the text (which will need a foreground colour if black would not be suitable):

\colorbox{green}{\textcolor{white}{some text}}

The package also provides \fcolorbox which puts a frame around a coloured box; in this case the first argument is the frame colour, the second the background colour, and the third the contents.

4.6.3 Panels and sidebars

The fancybox package lets you extend the principle of \fbox with commands to surround text in square, oval (round-cornered), and drop-shadow boxes (eg \ovalbox, \shadowbox, etc: see the documentation for details).

You can create panels of any size with these borders by typesetting your text in a minipage environment inside the Sbox environment which fancybox provides. The minipage formats the text but the Sbox ‘captures’ it, allowing you to delay putting the frame around until it is complete (so it knows the size).

The printed version of this document uses this extensively and there is a worked example shown in § 7.5 below.

Sidebars are blocks of text, usually explaining something too detailed to put in the main text. They sometimes go at the side of the page as their name suggests, but can also be floats like Tables and Figures. The float already mentioned lets you create your own new types of float as environments, so you could define one called sidebar and use that to handle your sidebars, putting a boxed block of text in each one.