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 a box, with a height and a width, just like it is 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 DVI or 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 2(a) will apply.}
          
Please make sure you send in your completed forms by January 1st next year, or the penalty clause 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 align differently with respect to any surrounding text instead 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}{3in}
  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 will 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 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).

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. They will also 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 the next chapter.

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.2. 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 Sidebars and panels

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 using the minipage environment to typeset the text inside a special Sbox environment which fancybox defines. The minipage formats the text but the Sbox ‘captures’ it, allowing you to delay putting the frame around until it is complete.

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