Length commands
15.5.2 Length commands
While a counter contains integers, a length command contains a real number and a dimensional unit.
L A TEX recognizes many different dimensional units. We list five absolute units: cm centimeter
in inch pc pica (1 pc = 12 pt) pt point (1 in = 72.27 pt) mm millimeter
and two relative units: em, approximately the width of the letter M in the current font ex, approximately the height of the letter x in the current font
L A TEX defines many length commands. For instance, Section 4.1 of The L A TEX Companion, 2nd edition [46] lists 17 length commands for page layout alone. You can
find some of them in Figure 10.4. A list environment sets about a dozen additional length commands (see Figure 15.2). Length commands are defined for almost every
aspect of L A TEX’s work, including displayed math environments—a complete list would probably contain a few hundred. Many are listed in Leslie Lamport’s L A TEX: A Docu- ment Preparation System, 2nd edition [43] and in The L A TEX Companion, 2nd edition [46]. Many more are hidden in packages such as amsmath.
404 Chapter 15 Customizing L A TEX The most common length commands are:
\parindent, the amount of indentation at the beginning of a paragraph \parskip, the extra vertical space inserted between paragraphs \textwidth, the width of the text on a page
A more esoteric example is \marginparpush, the minimum vertical space between two marginal notes. Luckily, you do not have to be familiar with many length com- mands because L A TEX and the document class set them for you.
Defining new length commands You can define your own length commands. For example,
\newlength{\mylength} makes \mylength a new length command with a value of 0 points. Note that while
you have to type \newcounter{counter}
to get a new counter, typing \newlength{mylength} results in an error message such as
! Missing control sequence inserted. <inserted text>
\inaccessible
l.3 \newlength{mylength} Setting length
The \setlength command sets or resets the value of a length command. So \setlength{\textwidth}{3in} creates a very narrow page. The first argument of \setlength must be a length com-
mand, not simply the command name, that is \setlength{textwidth}{3in} % Bad is incorrect. The second argument of \setlength must be a real number with a di-
mensional unit, for instance, 3in, and not simply a real number. In other words, \setlength{\textwidth}{3} % Bad is also incorrect.
15.5 Numbering and measuring 405
Tip
A common mistake is to type a command such as \setlength{\marginpar}{0} Instead, type \setlength{\marginpar}{0pt} Always be sure to include a dimensional unit.
The \addtolength command adds a quantity to the value of a length command. For instance,
\addtolength{\textwidth}{-10pt} narrows the page width by 10 points.
If you define \newlength{\shorterlength}
\setlength{\shorterlength}{\mylength} \addtolength{\shorterlength}{-.5in}
then \parbox{\shorterlength}{...} always typesets its second argument in a box 1/2 inch narrower than the parboxes set to be of width \mylength.
When L A TEX typesets some text or math, it creates a box. Three measurements are used to describe the size of the box:
the width the height, from the baseline to the top the depth, from the baseline to the bottom
as illustrated in Figure 15.1. For instance, the box typesetting “ aa” has a width of 10.00003 pt, a height of 4.30554 pt, and a depth of 0 pt. The box typesetting “ ag” has
Figure 15.1: The measurements of a box.
406 Chapter 15 Customizing L A TEX the same width and height, but a depth of 1.94444 pt. The box “ Ag” (see Figure 15.1)
has a width of 12.50003 pt, a height of 6.83331 pt, and a depth of 1.94444 pt. The commands
\settowidth \settoheight \settodepth
each take two arguments. The first argument is a length command, the second is text (or math) to be measured by L A TEX. The corresponding measurement of the box in which the second argument is typeset is assigned to the length command in the first argument. For example, if \mylength is a length command, then
\settowidth{\mylength}{Ag} assigns 12.50003 pt to \mylength. It should be clear from this example how the
\phantom and \hphantom commands (see Section 5.8.1) are related to this command. To perform more complicated arithmetic with length commands, use the calc package.
Rubber lengths In addition to rigid lengths, such as 3in, L A TEX can also set rubber lengths, that is,
lengths that are allowed to stretch and shrink. Here is an example: \setlength{\stretchspace}{3in plus 10pt minus 8pt} Assuming that \stretchspace is a length command, this command assigns it a value
of 3 inches that can stretch by 10 points or shrink by 8 points, if necessary. So a box of width \stretchspace is 3 inches wide, plus up to 10 points, or minus up to 8 points.
Stretchable vertical spaces are often used before and after displayed text environ- ments. L A TEX adjusts these spaces to make the page look balanced. An example can be found in Section 15.1.7. \medskipamount is defined as
6.0pt plus 2.0pt minus 2.0pt See Section 15.6.3 for more examples.
The \fill command is a special rubber length that can stretch any amount. The stretching is done evenly if there is more than one \fill present. See the second example of brand-new environments in Section 15.2.5.