# Math: Displays

## General tips

### Use the backslash/bracket pair ("$...$") instead of a double dollar construct ("$$...$$") for single line displays.

The double dollar construct, while still widely used, is not part of the "official" LaTeX syntax. Its use is discouraged, and, while in most cases it will work just fine, it does cause problems in certain situations. For example, the "\qedhere" trick described below, does not work with an equation set with double dollar signs. (The backslash/bracket construct is for unnumbered equations. If you want an equation numbered, use the $$...$$ environment.)

### Use align/align* instead of eqnarray/eqnarray* for multiline displays.

The eqnarray/eqnarray* environments have been superseded and rendered obsolete by the align/align* environments provided by the amsmath package. See below for more on these and related amslatex environments see below.

### Set multiline displays using appropriate multiline environments, not by stringing together several single line displays.

Multiline display environments generate the appropriate amount of spacing between the lines of the display and enable alignments. By stringing together single line displays or several individual multiline displays, alignments would get lost, and the spacing would be uneven, with lines belonging to different display environments excessively spaced out because of the extra vertical spacing generated by these environments.

### Do not leave blank lines before or after displays, unless you want a paragraph break at that spot.

While multiple spaces are equivalent to a single space in TeX, the same is not true for linebreaks. Two or more successive linebreaks generate a blank line, which in turn is interpreted as a paragraph break. (In fact, one or more blank lines are equivalent to the explicit paragraph breaking command "\par".) A surprisingly common mistake is to surround displays by blank lines in an attempt to make them stand out more. This is wrong, as TeX would then insert a paragraph break before and after the displays, possibly adding unnecessary vertical spacing and indenting the text following the display. The proper way to make a display stand out is by placing the begin/end display commands on lines by themselves (see below).

### Format the source code for displays in a manner that makes it easy to read and debug the code.

Displays are the main sources of TeX errors, and hunting down errors in a lengthy display can be very frustrating as TeX usually detects errors only when it reaches the end of the display and thus won't be able to pinpoint the exact location of the error. Here are some tips on writing the code for displays in a manner that makes this tasks easier.
• Put the backslash/bracket pairs ("$", "$") and the begin/end commands for display environments ("", "", etc.) on lines by themselves. This makes displays stand out visually. Although TeX does not care about such niceties, and makes subsequent editing of the file a lot easier.
• Place double backslashes (\\) indicating linebreaks in displays either at the end of a line or on a line by itself. In the middle of a line, amongst regular tex code, these symbols are much harder to spot.
• Insert frequent linebreaks in the source code of displays. A display set as a single long line is very hard to debug, and a single editing error on that line can mess up the entire display. Break the display up into small, logical "chunks" by inserting linebreaks at appropriate places, e.g., between the numerator and denominator of a large fraction set with \frac{...}{....}, or between terms in a formula.

### Spacing in displays: Use \quad or \qquad if explicit spacing is needed, do not use backslashed blanks ("\ ") or ties ("~") for spacing, and use math spacing commands like "\," and "\!" very sparingly, if at all.

Explicit spacing commands such as "\," and "\!" are rarely appropriate and should be used sparingly, if at all. In particular, no explicit spacing should be inserted between a formula at the end of a line in a display and a punctuation sign (comma or period).

That said, explicit spacing commands are sometimes needed in displays to separate formulas, to offset text from formulas, or to "indent" continuation lines. In those cases, I recommend using standard units such as "\quad" or "\qquad" rather than multiple instances of single blanks ("\ \ \ "), or ties ("~~~~"). Here are some common situations:

A displayed formula with a range or condition. Use a single \quad to separate the condition from the formula.


f(i)=i^2\quad \text{for $i=1,2,\dots,n$}


• Two formulas in a single line, separated by a comma. Again use a \quad to separate the formulas. for example:

f(x)=\cos x, \quad g(x)=\sin x


• Two formulas separated by the word "and". Put \quad before and after "and" (make sure to use \text{and} to force "and" to be set in text mode):



• A continuation line in a multiline display. Here a \qquad is the appropriate amount of indentation. Align at the equal sign (or equivalent), and on continuation lines insert a \qquad after the alignment symbol. If a secondary continuation line is needed, indent that line by an additional \qquad. Here is an example:

\begin{align*}
S(n) &= \sum_{k=1}^nf(k) +\sum_{k=1}^ng(k) +\sum_{k=1}^ng(k)
\\
+ \sum_{k=1}^n\left(1+k+k^2+\dots + k^k\right)^2
\\
\end{align*}



### Use "large" delimiters to surround "large" expressions (like sums or fractions).

An expression like "(\sum_{i=1}^na_i)^2", surrounded by ordinary parentheses, looks very poor when typeset. To correct this, one can use either one of the explicit delimiter sizing commands ("\biggl", etc.), or precede the two parentheses by \left and \right and let TeX take care of the sizing. Using manually sized delimiters may require some trial and error to get the appropriate size, so I usually take advantage of the auto-sizing mechanism of TeX, and let TeX do the job. In almost all cases this works very well. Here are some tips on delimiter sizing:
• When using \left and \right, make sure that the text enclosed does not contain a linebreak symbol (e.g., "\\"), or an alignment symbol ("&"). Alignment symbols usually occur near the beginning of a line, so they rarely interfer with \left-\right pairs. However, in complex multiline displays, it can happen that the two matching delimiters occur on different lines and thus cross a linebreak. In this case one has two options: Either use manual sizing, or use delimiter place holders "\right." and "\left.", placed at the end of the first line, and near the beginning of the continuation line. The latter option avoids the problem of crossing linebreaks in between a \left-\right pair (one still needs to make sure that the pairs do not enclose an alignment symbol enclosed). However, since the sizing in that case is determined by the expressions between the two pairs "\left(" and "\right." and "\left." and "\right)", this only works if those two expressions have approximately the same height. If that is not the case, the closing parenthesis would come out smaller; in this case, I would use manual sizing.
• When using manually sized delimiters, make sure to be consistent in the sizing. In particular, make sure that matching delimiters are of the same size. With auto-sized delimiters, TeX ensures that the sizes of the matching delimiters are the same. With manually sized delimiters, it is up to the author to ensure that this is the case; TeX will follow whatever sizing instruction it is provided with.
• Avoid using sized delimiters in inline expressions. Delimiters usually add a bit of additional vertical space, making a bulky expression even bulkier. If an inline math formula is large enough so that it looks poor when set with a normal size delimiter, it probably should not be set inline in the first place.

### How to break up formulas in multiline displays.

Breaking up overlong lines in displays can be very tricky, and requires a good knowledge of the underlying mathematics as well as a feel for good mathematical typesetting. For that reason, TeX doesn't break formulas, as it does with ordinary text. However, there are some general guidelines. Here are possible breaking points, in decreasing order of desirability:
• Right before an equal sign or equivalent (e.g., a "less than" sign). In this case, place the alignment symbol (ampersand) placed before the equal sign, i.e., " ... + x \\ &= ". The equal signs (or equivalent) should be aligned.

• Before a plus or minus sign. In that case, the continuation line should be shifted to the right by a \qquad. If the continuation line is very short, two \qquad's might be better.

• Between two large "chunks" that are multiplied. The chunks could be large parenthesized expression, sums or integrals. This should be used only as a last resort, and in that case the preceding line should end with an explicit multiplication symbol ("\times"), the continuation line should begin with "\times" symbol and be shifted as far to the right (by preceding it with a few "\qquad"'s after the alignment symbol).

### When to display a mathematical formula or equation.

Formulas set inline are harder to spot, may cause problems with bad linebreaks (which one has to fix by inserting explicit linebreaks), and they may look poor, especially if they involve fractions, sums, integrals, or other "large" objects. On the other hand, one should not indiscriminately display every equation or formula. A judicious choice of what to display can make a a significant difference in the overall appearance and readability of a paper. Here are some guidelines. You should display formulas/equations in the following cases:
• A numbered equation. This is a no-brainer. If you need to refer back to an equation, the equation needs to be numbered, and therefore should be set as a display.
• A formula that is excessively long. If a formula takes up more than about half of a line of space, it would probably be good to display it. Formulas set inline should never be longer than one line (even though tex would accept multiline inline displays and probably do a reasonably good job in splitting the formula).
• A formula that takes up excessive vertical space. Formulas that involves complicated sums or integrals with multiple subscripts or superscripts, or stacked fractions, take up a lot of vertical space and cause subsequent lines to be moved down, if set as inline formulas. In these situations, it may be appriopriate to display the formula. Simple sums, integrals, or fractions, are usually not a problem; for example, an expression like "\sum_{k=1}^nk=n(n+1)/2" doesn't need to be displayed. (Note the "slashed fraction" notation at the end; in an inline context, this is preferable to a regular fraction notation.)
• A formula that you want to give special emphasis. Even if a formula would look fine inline from a typesetting point of view, it may be appropriate to display it for emphasis. This could be the case, for example, with an important definition, or a crucial condition/hypothesis in a theorem.

### Pagebreaks in multiline displays.

By default, TeX does not allow pagebreaks inside display environments such as align. If a paper has many multiline formulas, this may cause "underful vbox" warning messages. If the "badness" of the underful box (as reported by tex in the log file) is less than 2000 or so, this is barely noticeable and I usually leave it alone. However, with larger badness values, especially those above 5000, the text on the offending pages will be excessively spaced out and look very poor.

One way to get around this would be to break up a the multiline display into several smaller displays. However, I recommend against this doing this. In the first place, this makes it nearly impossible to keep the same alignment throughout the entire display. More importantly, however, the vertical spacing in the display would no longer be uniform since TeX adds additional vertical spacing before and after each display.

A better approach is via the "\allowdisplaybreaks" command that explicitly permits breaks in displays. Simply enclose the offending display in a group containing \allowdisplaybreaks:

{\allowdisplaybreaks
\begin{align}
...
\end{align}}%

(The comment symbol (%) at the end is needed to prevent spurious spaces in the text following the display.)

The best solution, however, is to prevent this problem by avoiding excessively long displays (say, with five or more lines). It is often not hard to break up a very long display into two, for example by inserting a phrase like "By the Cauchy-Schwarz inequality, the last expression is at most" in the middle of the overlong display, followed by the remaining lines of the display.

### Displays ending a proof: the "\qedhere" trick.

If a proof ends with a displayed equation, then "\end{proof}" would normally place the symbol one line below the display, which looks odd. To place the symbol on the same line as the display, add "\qedhere" at the end of the display (right before "", or its equivalent). Note that this does not work if the display is set with double dollar signs or an eqnarray environment (this is one of many reasons to not use these constructs); it will work with the backslash/bracket environments or any of the amsmath display environments.

## Recommended display environments

• The backslash/bracket pair, "$...$". The simplest environment, and the one that requires the least amount of typing; it replaces the double dollar ($$...$$) construct of plain TeX whose use is discouraged (see below). Use this for all unnumbered displayed equations. A side benefit of the backslash/bracket notation is that it makes it easy to change a displayed equation to one set inline, and vice versa, by simply swapping the backslash/bracket pair with a dollar sign.

• $$...$$ and \begin{equation*}$$...$$\end{equation*}. Use the first (unstarred) version if you want the equation to be automatically numbered. You can later reference the equation with "\eqref{...}", where the text inside the braces is the equation label. (Note that no parentheses are required with "\eqref".) For more on equation numbering, see below. In the starred version the automatic numbering is disabled, but it allows you to create an equation number manually with the "\tag{...}" command.

• \begin{align} ... \end{align} and \begin{align*} ... \end{align*}. These are the primary environments for multiline displays. In the vast majority of situations, those environments are more than adequate. The difference between "align" and "align*" is the same as between "equation" and "equation*": The starred versions don't automatically generate equation numbers. Use those versions, if you don't want equation numbers, or if you want to number equations manually.

• \begin{multline} ... \end{multline} and \begin{multline*} ... \end{multline*}. This is primarily intended for a two line display, in which the second line is a continuation of the first line (rather than the second line in a chain of inequalities or equations). A typical example is the definition of a set "A= \{x\in R : .... \}, that does not fit on a single line. In this case one should break the display at an appropriate place inside the "...." part, with the continuation line shifted to the right as far as possible. This is exactly what the multiline environments do. All one needs to do is specify the location of the break in the usual way, with a "\\", and TeX takes care of the rest. The advantage over using an align environment here is that one does not have to hunt for an appropriate alignment place and insert alignment symbols. The difference between the starred and unstarred versions is again in the equation numbering.

• \begin{gather} ... \end{gather} and \begin{gather*} ... \end{gather*}. These are intended for multiline displays without alignment, i.e., in which each line of the display is to be centered. In my experience, the situations where this should be done are quite are. Usually, multiline dispays either involve a chain of equations or inequalities (which, of course, should be aligned at the inequality sign), or a sequence of parallel equations, in which case the first equality sign provides a natural place for alignment.

• \begin{cases} ... \end{cases}. This is a "subsidiary" display environment, in the sense that it needs to be placed inside another display environment, such as "$...$". It is a very handy environment for definitions involving several cases, and much easier to use and work with than manual constructions like "\left\{ \begin{array}....\end{array}\right.". Here is a typical example:
$|x|= \begin{cases} x & \text{if x≥0,} \\ -x &\text{if x\le 0.} \end{cases}$

Note that the part after the alignment symbol usual contains some text and is best typeset using "\text{... }", and enclosing any math material within the braces in dollar signs.

• Other amslatex display environments. Amslatex provides several other environments for multiline displays, such as split, aligned, alignat, gathered , as well as starred version of most of these. These may do a marginally better job in some special situations, but the (mostly cosmetic) gain that these environments provide in those situations is not worth the effort of memorizing the various alternatives to the "align/align*" environments. I have never felt a need to use any of those esoteric environments. In fact, the only display environments that I use with any frequency are the backslash/bracket and equation/equation* environments for single-line displays, align/align* for multiline displays, and, the "cases" environment for cases constructs.

• Display environments to avoid.
• The double dollar symbol ($$). (Use instead the backslash/bracket pair "$", "$".) The double dollar sign as a begin/end marker for displayed math material is a relic from plain TeX, but has been officially deprecated in LaTeX. In fact, many LaTeX books don't even mention it, since it is not supposed to be used. While it works just fine in most situations, and many authors use it with impunity, there are a few situations where it causes problems, and it may not work under future versions of TeX. If you come from a plain TeX/Amstex background and are in the habit of typing$$'s, try to gradually switch over to using the backslash/bracket pair. If you are new to Tex/LaTeX, learn it right from the start (i.e., use the backslash/bracket method).

• The eqnarray/eqnarray* and array environments. (Use instead the align/align* environments provided by the amsmath package.) In the pre-amslatex period, eqnarray, eqnarray*, and array were the only available environments to typeset multiline equations, matrices, etc, and in many Latex books and online guides one still finds these environments recommended, with no mention of alternatives. However, amslatex has rendered these environments completely obsolete. Provided the "amsmath" package has been loaded (see above), one has access to a variety of display math environments; in particular, the "align/align*" environments provide all the functionality of eqnarray/eqnarray*/array, produce superior looking output, and are easier to use. There is no reason whatsoever to still use "eqnarray" instead of "align".
Converting from eqnarray/eqnarray* to align/align*: The main difference in the syntax is that the eqnarray/eqnarray* environments require two alignment symbols (&), with the first typically placed before the equal sign (or equivalent) and the second after the equal sign, while the align/align* environments require only one such symbol, placed before the equal sign. Thus, aside from replacing "eqnarray" by "align" (which can easily be done with a global search/replace), one needs to delete the second of the two alignment symbols in each line of the display. After doing that, the file usually compiles without problems, but one should inspect the displays visually and adjust the placement of the alignment symbols if necessary.

• The \begin{display} ... \end{display} construct. (Use instead $and$ instead.) The backslash/bracket notation is completely equivalent to a \begin{display} ... \end{display} environment, so there is no reason to use the latter.

Last modified: Tue 23 Aug 2011 05:48:04 PM CDT A.J. Hildebrand