LaTeX/Boxes

TeX boxes and glue overview
A 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.

character boxes
TeX 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
A is a box of specific width formatted in paragraph mode. In paragraph mode, text is broken into lines and lines are broken into pages.

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, , and. (See character box above for explanations of depth and height.)

defines the height of the.

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.

positions the contents of the box within the box. It can be one of center, top, bottom or spread. Note that has no effect if the box is not larger than the text it contains.

is available in the pbox package. A has the same parameters as a, 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.

The environment takes the same parameters as a, and behaves nearly identically to it. The difference between a and a  is that a  can only contain a single paragraph and you cannot use all commands and environments inside it, while a  may contain multiple paragraphs and, in fact, almost anything.

You can make use of, , and to embed paragraphs in non-paragraph boxes. For instance:

special notes on the parameter


The alignment parameter does not refer to the 's borders when  is either missing or equal to  and anytime = center! Under any of these circumstances the alignment parameter selects which line of text within the will be used to align the paragraph box. The is placed so the baseline of that chosen line of text is aligned to the baseline of the box that the  is glued to. Thus, if the is set top, the baseline of the top line of text in the  will line up with the baseline outside of the. 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 will appear to do nothing unless additional text is added.



If the is present and not the same as  and  is not center, the  will align at its borders.



makebox and mbox
Makebox 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.

The parameter takes a one letter value: center, flushleft, flushright, or spread the text to fill the box.

framebox and fbox
The command behaves identically to  except that it additionally draws a box around its contents.

The following example shows you some things you could do with the and  commands:

You can tweak the following frame lengths.
 * : the distance between the frame and the content.
 * : the thickness of the rule.

This prints a thick and more distant frame:

This shows the box frame of a letter.

The package is available that adds the  environment which provides an an easy way to frame a paragraph within a document:

savebox/usebox/newsavebox
A savebox is a non-rendered box that is saved for later repeated rendering via the usebox command.

The command creates a placeholder for storing content; the command stores the specified content in the placeholder without rendering it in the document; and  renders the content of the placeholder into the document.

colorbox and fcolorbox
See Colors. can also be tweaked with and.

fancybox
The package provides additional boxes.

raisebox
Now that we control the horizontal, the obvious next step is to go for the vertical. No problem for LaTeX. The

command lets you define the vertical properties of a box. You can use, , and  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.

rotatebox
See Rotations.

resizebox and scalebox
The package features additional boxes.