11.7 Fine-tuning the mathematical layout

Although LaTEX generally does a good job of laying out the elements of a formula, it is sometimes necessary to fine-tune the positioning. This section describes how to achieve some of the many detailed adjustments to the layout that are used to produce mathematical typography that is just a little bit better. Most of this section applies to all LaTEX mathematical material, but a few features are available only with the amsmath or mathtools package; these are clearly labeled.

11.7.1 Controlling the automatic sizing and spacing

Letters and mathematical symbols normally get smaller, and are more tightly spaced, when they appear in fractions, superscripts, or subscripts. In total, TEX has eight different styles in which it can lay out formulas:

The prime versions (D′, T′, etc.) represent the so-called cramped styles, which are similar to the normal styles except that superscripts are not raised so much.

TEX uses only three type sizes for mathematics in these styles: text size (also used in ), script size, and scriptscript size. The size of each part of a formula can be determined according to the following scheme:

In LaTEX, the top-level part of a formula set in running text (within a $ pair or between (…)) is typeset using text style (style T ). A displayed formula (e.g., one between […]) is typeset in display style (style D). The kind of style used in a subformula can then be determined from Table 11.7, where the last two columns describe the styles used in the numerator and the denominator of a fraction or similar construct, for example, produced with or . In particular, notice this difference between fractions in display and text style; in the latter case the numerator and denominator are typeset in script style, i.e., noticeably smaller.

11.7.2 Subformulas

Whereas in text a pair of braces can simply indicate a group to which the effects of some declaration should be confined, within mathematics they do more than this. They delimit a subformula, which is always typeset as a separate entity that is added to the outer formula. As a side effect, subformulas are always typeset at their natural width and do not stretch or shrink horizontally when TEX tries to fit a formula in a paragraph line during line breaking. As shown earlier, the subformula from a simple brace group is treated as if it was just a single symbol (of class Ordinary). An empty brace group, therefore, generates an invisible symbol that can affect the spacing. The exact details can be found in Chapters 17 and 18 and Appendix G of The TEXbook [84].

The contents of subscripts/superscripts and the arguments of many (but not all) commands, such as and , are also subformulas and get this same special treatment. Important examples of arguments that are not necessarily set as subformulas include those of (see Section 12.2.1). If a group is needed only to limit the scope of a declaration (i.e., where a separately typeset subformula would be wrong), then and and not {…} should be used. Note that specialized mathematical declarations such as style changes apply until the end of the current subformula, irrespective of the presence of any other groups.

In the following artificial example you can see that the mathematical styles are restricted to the subformulas of the operator subscripts, but the inside is neither restricted to the argument nor does it end at the . However, that group delimits the scope of the color declaration.

11.7.3 Line breaking in inline formulas

LaTEX can break an inline formula (e.g., produced with \(...\)) over several lines if necessary, but it does this by default only after Relation or Binary symbols. The penalty for breaking at these positions is customizable through the TEX counters (default value 500) and (default 700). This means that LaTEX discourages breaking a formula, but if necessary, it slightly prefers breaking after a Relation over breaking after a Binary symbol. If you want to discourage this further, you can set the parameter to even higher values, but it has to be done with low-level assignments, e.g.,

If you never want any automatic line breaking happening within a formula, you can set both to 10000 (TEX’s number for ∞), but you might have to also alter the definitions of and because they use their own hardwired penalties — or handle them manually.

The penalty values used are the ones that are current at the end of the formula; thus, changing their values anywhere inside a formula affects all Relation and Binary symbols, not just a single one. For local adjustments, it is therefore better to place explicit penalties inside a formula that you want to affect, i.e., for preventing a break after a specific symbol1 or , , or to allow, encourage, or discourage it at any point within the formula, which makes it possible to break anywhere.

In the next example LaTEX breaks after the + sign, which looks a bit odd with the b on its own at the beginning of the line. So we correct it by adding , after which LaTEX uses the only remaining breakpoint after the = sign. This makes the line look a bit spaced out in the example (because it is so narrow), but in real life with a wider paragraph measure, this adjustment would be a clear improvement.

11.7.4 Big-g delimiters

To provide direct control of the sizes of extensible delimiters, LaTEX offers four commands: , , , and . These take a single parameter, which must be an extensible delimiter, and they produce ever-larger versions of the delimiter, from 1.2 to 3 times as big as the base size.

Three extra variants exist for each of the four commands, giving four sizes of Opening symbol (e.g., ), four sizes of Relation symbol (e.g., ), and four sizes of Closing symbol (e.g., ).1 All 16 of these commands can (and must) be used with any symbol that can come after either , , or (see Table 11.5 on page 190).

In standard LaTEX the sizes of these delimiters are fixed. With the amsmath package, however, the sizes adapt to the size of the surrounding material, according to the type size and mathematical style in use, as shown in the next example. The same is true when you load the exscale package (see Section 9.5.7) or when you use a font package that implements the exscale functionality as an option (e.g., several of the packages discussed in Sections 12.3 and 12.5).

11.7.5 Radical movements

In standard LaTEX, the placement of the index on a radical sign is sometimes not good. With amsmath, the commands and can be used within the optional argument of the command to adjust the positioning of this index.

Positive integer arguments to these commands move the root index to the left and up, respectively, while negative arguments move it right and down. These arguments are given in terms of math units (see Section 11.7.7), which are quite small, so these commands are useful for fine adjustments.

11.7.6 Ghostbusters™

To get math spacing and alignment “just right”, it is often best to make creative use of some of primitive TEX’s unique and sophisticated typesetting abilities. These features are accessed by a collection of commands related to and ; and they can be used in both mathematical and other text.

For instance, the large alignment example (Example 11-2-9 on page 136) uses lots of phantoms to get the alignment just right. Each of these phantoms produces an invisible “white box” whose size (width and total height plus depth) is determined by typesetting the text in its argument and measuring its size.

Conversely, the command typesets its contents (in an LR-box) but then ignores both their height and depth, behaving as if they were both zero. The standard LaTEX command is a combination of these, producing the equivalent of : an invisible box with zero height and depth but the width of the phantom contents.

The command makes the width of the phantom zero but preserves its total height plus depth. An example is the command , which is defined as “(” so that it produces a zero-width box of height and depth equal to that of a parenthesis.

The amsmath package provides an optional argument for , used as follows: ignores the height of the box’s contents but retains the depth, while [b] ignores the depth and keeps the height. Compare these four lines, in which only the handling of √y varies:

To get the three radical signs looking pleasantly similar, it seems that the thing to do may be to give the y some extra height with a strut — but that makes things only worse! The best solution turns out to be to smash the bottom of the y (but not the whole of it!).

In the next example, the two case lines are spread too far apart due to the depth of the denominator in the first line and the superscript on the numerator of the large fraction in the second line.

As the lines do not overlap if we bring them closer together, we can ask LaTEX to disregard the depth of the p in the denominator on the first line and the top of the large fraction in the second line using bottom and top commands. We also have to add an empty brace group because of the unfortunate optimization of TEX to ignore the if it is the only object in the fraction; see also page 203.

This, of course, would bring the two lines in this example confusingly close together. For this reason we also add a

so that the numerator of the main fraction still appears to have a certain height (a , which has the dimensions of a parenthesis, would not produce enough extra space):

As you probably realize, in this case a much simpler way to achieve the same effect is to request some negative vertical space between the case lines, e.g., using \[-4pt]. Nevertheless, some moderate use of smashing is often of benefit to such unbalanced displays.

You may also think that this only partly solves the issue with this display and that it would look better if there is a bit more vertical space in the main fraction. In that case you could try adding a strut to both its numerator and denominator. A is too small and would have no effect, and a text

is too large. For such cases mathtools offers .

This produces a strut of the height and depth of a parenthesis in the current math style, e.g., smaller in a script or scriptscript situation. The mandatory enlarge argument defines the factor by which it is enlarged at the top and by default also at the bottom. If the optional bottom-enlarge is also given, it is used for bottom instead. Thus, makes a strut that is 30% larger than a normal with 20% being added above. Using a value of 0 in an argument means that this part of the strut is not altered at all, while using a negative value shortens the strut (usually useful only for the top part).

The behavior is best explained in an example. We show different commands inside a command and highlight its height and depth by showing a vertical blue rule that extends through the full size of the box (using ).

The setting for and in the example was made to prevent the command from adding its own rules or spaces. The reason that -0.5 in the last line of the example does not remove the strut altogether is due to the fact that the top part of a strut is roughly 70% of the overall size. Thus, 20% remains, while in the bottom part -0.3 would already remove everything.

Using , we can now easily open up the parts of the fractions a bit to make them more readable; we used it three times with slightly different values— finding the most appropriate values may need some experimentation.

Another collection of examples illustrates a very common application of smashing: using a partial to give fine control over the height of surrounding delimiters. It also shows that smashing can lead to problems because the real height of the line needs to be known; this is restored by . In the following code, is the compound symbol defined by

11.7.7 Horizontal spaces

Even finer, and more difficult, tuning requires the explicit spacing commands shown in Table 11.8 on the next page. Both the full and short forms of these commands are robust, and they can also be used outside math mode in normal text. They are related to the thin, medium, and thick spaces available on the machines used to typeset mathematics in the mid-20th century.

If used in text mode, the amount of space added by these ..space commands is based on fractions of 1em as listed in the rightmost column of the table. However, if used in math mode, the amounts are, in fact, defined by the current values of the three parameters , , and ; the table lists their default values with amsmath. These very low-level TEX parameters require values in “mu” (math units). They must therefore be set only via low-level TEX assignments (as shown in Example 11-8-2 on page 210) and not by or similar commands. Moreover, in normal circumstances their values should not be modified because they are used internally by TEX’s mathematical typesetting (see Table 11.9 on page 210).

One math unit (1mu) is 1/18 of an em in the current mathematical font size (see also Table A.1 on page 652). Thus, the absolute value of a math unit varies with the mathematical style, giving consistent spacing whatever the style.

These math units can be used more generally to achieve even better control over space within mathematics. This is done via the amsmath command , which is like except that it can be used only within mathematics, and its length argument must be given in math units (e.g., ). Thus, while a in a mathematical formula always produces the same space of 1em based on the main mathematical font size, specifying produces a space that is about two-thirds the space in a double subscript size. This is shown in the next example, where different em-based spaces are used in the first and second line and then corresponding mu-based spaces are used in the third line.

11.7.8 resizegather — Downscaling an equation

Sometimes a formula is just a tiny bit too large to fit the text width, as is the case with the next example, which is quite ugly looking:

In such a case you have the option of manually breaking the formula over two lines, but usually that does not work well when the excess width is only small, because then both lines are fairly empty. As an alternative, Heiko Oberdiek developed the package resizegather that redefines gather, equation, and their starred forms (but not […]) so that they scale down the formula in each line to fit into the available space.

Usually this is preferable to a split of the line, but only when the excess is small. Therefore, if it is larger than 5%, a warning is given, and you better visually verify that the result is acceptable. With the package option warningthreshold, you can alter the default. In the example we increase it to 10% to get only warnings for substantial resizing.

In the previous example the first line is fine (correcting Example 11-7-19), but the second line in direct comparison becomes too tiny, and you get the following warning on the terminal

to alert you about this fact so that you can take manual actions.

11.7.9 subdepth — Normalizing subscript positions

Subscripts in formulas are not always placed at the same vertical position by the TEX engine. Instead, their placement depends on surrounding conditions: if there is also a superscript, then TEX lowers the subscript slightly in order to leave enough space between the two. If you compare the different subscripts in

you see that the c is lower than a or b. This is usually fine and desirable, but there are applications, for example, when typesetting chemical formulas, where it is preferable if all subscripts are properly aligned; e.g., the 2 is in the same position as the 5.

To help with this task, Donald Arseneau wrote a short piece of code that later got packaged by Will Robertson as subdepth. All you have to do is to load this package in order to get all subscripts in all formulas aligned.

If you look carefully, you see that this not only aligns the subscripts but also slightly raises the superscripts in certain situations (e.g., in Zc2). If you do not like that aspect of the package behavior, load it with the package option low-sup.

11.7.10 Color in formulas

If you prepare educational material, but also in other situations, it is sometimes helpful if parts of a formula are highlighted, e.g., by using color. Even though color is not a concept natively available in TEX (and therefore needs to be handled by the backend, for example, in the Portable Document Format (PDF) generation), it is fairly easy to color parts of a formula without any negative side effects to spacing as long as you avoid a few traps.

To color a complete formula, all you need to do is to place the whole formula inside \textcolor. It is also possible to use inside the formula for this purpose, but this may color less than you intended if you place the command in a place with a restricted scope, e.g., between and or into one of the amsmath display environments, because in there the scope ends at the next & or the end of the line. In either case, the color is automatically reset when the scope ends.

While you can put whole formulas inside \textcolor, you should not use it inside a math formula, because this usually results in bad spacing as shown in the next example where the binary minus suddenly comes out as a unary minus — a defect that does not happen with that was designed for this.

Thus, if you want to color only parts of a formula, you have two options: either change the color back and forth using several commands (which is fairly cumbersome and on some occasions results in spacing problems or even errors) or, assuming you have a recent LaTEX release, use inside the formula.

As you see, using is much simpler than trying to alter the color with , and it is designed to work correctly even in far more complicated cases, e.g., when coloring just a large operator but not its limits1 or coloring an opening or closing symbol made with or . With all this is easy, so it should be the preferred choice.

It preserves the nature of the math symbols inside of its argument and allows them to interact correctly with their uncolored neighbors, which explains why the spacing around the equal sign in the next example, or the minus in the previous one, remains correct. It also understands about subscripts and superscripts and puts the color changes in appropriate places in order to not affect their placement.