A guide to writing mathematics

1. Writing mathematics

3. Whitespace

2. Typesetting text

4. Typesetting mathematics

4 Typesetting mathematics

TeX offers two modes of displaying mathematics: inline and display mode. The inline mode (using either $...$ or \(...\) – there is no real difference) appears in line with text, while display mode (using a number of TeX commands, such as \[...\], \begin{equation}...\end{equation}, and more) appears on a separate line.

4.1 Displays and punctuation

When a display is part of a sentence (which it usually is), any following punctuation should be placed in the display itself. For instance, note the misplaced comma here:

I like the equation \[ e^{-i\pi}+1=0 \], since it includes all of my favourite constants.

It should be typeset as follows:

I like the equation \[ e^{-i\pi}+1=0, \] since it includes all of my favourite constants.

Including commas and periods (and, to a lesser degree, colons and semicolons) in displays is correct and appropriate, but you should avoid question and exclamation marks in your displays. A display should be able to stand both as an independent statement, as part of the surrounding sentence, and (if it has an equation number) as part of any other sentence where it is refered to. Question and exclamation marks are rarely compatible with these requirements.

4.2 Numbering equations

Displays which are typeset with environments such as equation, align, gather, etc. (see below) are automatically numbered. To refer to that display later on you need to label it:

It can be shown that
\begin{equation}
e^{i\pi}+1=0. \label{eq:myequation}
\end{equation}
The identity \eqref{eq:myequation} is \emph{Euler's identity.}

(Giving equations names beginning with eq: is a common convention, and not a requirement. Note also that the formulations This identity is … or The above identity is … would have given a more easy flowing text.)

TeX will number equations even if you don't label them. Therefore, if you typeset all displays using numbered display environments, you will quickly end up with equation numbers as high as 50 or even 100, of which only a handful are actually refered to later in the text. Your document will be cluttered with useless information, and the reader will find it difficult to remember individual equation numbers.

Therefore: As a general principle, you should only number displays that you plan to refer to later on. If you see a numbered display without a \label, remove the numbering.

If you have included the package amsmath then every display environment (equation, align, etc.) has a “starred”, unnumbered variant (equation*, align*, etc.); you should prefer the starred environments over the numbered environments. See §4.4 Multi-line displays and alignment for an overview of the various display environments in LaTeX.

As a sidenote, you should almost never refer to a display that appears later in the text; such references should only appear as paranthetical remarks: In Section 3 we will derive a condition (see equation (3.5)) which will solve all of our problems.

4.3 Sizing and spacing delimiters

Delimiters are symbols such as (, ), {, } and so on that mark the start or end of a specific type of content. Since the size of this content may vary considerably, it is important to size delimiters according to their context. Consider the following example:

\[
\{(x,y)\in\mathbb{R}^2 \mid (x-y)(f(x,y)(x+g(x-y,s))-2s)<3s \ \forall\ s\in[\int_0^1e^{-t^2}dt, \frac{5}{2}]\}
\]

Content such as powers and fractions jut out of their enclosing braces, and the nested parantheses are difficult to decipher. The result is an expression which is difficult to read.

4.3.1 \big and its siblings

TeX provides the commands \big, \Big, \bigg and \Bigg to resize delimiters. For instance, \big( will print an opening paranthesis which is slightly bigger than a normal (. Its bigger siblings \Big, \bigg and \Bigg will print symbols that are 1.5, 2 and 2.5 times bigger than \big does.

\[
\bigg\{(x,y)\in\mathbb{R}^2 \mid (x-y)\big(f(x,y)\big(x+g(x-y,s)\big)-2s\big)<3s \ \forall\ s\in\bigg[\int_0^1e^{-t^2}dt, \frac{5}{2}\bigg]\bigg\}
\]

Apart from the clear improvement in sizing of the square and curly brackets, note the more subtly improved sizing of the parantheses.

TeX also offers the variants \bigl, \bigr and so on, which produce opening and closing delimiters. You should prefer these over \big whenever sizing delimiters; see §3.4 Keep an eye on TeX’s automatic spacing. If you need to enlargen relational symbols such as |, there is a \bigm command. For instance, replacing the above \mid by \bigm| produces a taller vertical bar, but with the correct horizontal spacing.

Many mathematicians often replace some parantheses with square brackets [] or curly brackets {} in order to improve readability and make the expression easier to decipher. As a general rule, you should try to avoid doing so; square and curly brackets have many other uses, so using them in place of parantheses might be confusing. Try resizing the parantheses before replacing them with alternative delimiters.

4.3.2 \left, \right, and why you shouldn’t use them

Using \big and its siblings to manually resize delimiters might seem overly complicated. And indeed, TeX provides a way to automatically resize symbols through the \left and \right commands:

\[
\left(x^2 - 3\right) < \left(\frac{1}{1-x}\right)^2
\]

TeX evaluates what is between \left( and \right), computes its height, and resizes the delimiters ( and ) (or whichever delimiters you have used) to be as tall as their content.

There are at least four cases where \left and \right will produce suboptimal results:

  1. A \left ... \right clause cannot be broken into multiple lines. (See §4.4 Multi-line displays and alignment for multi-line displays.)
  2. The sizing might be too small. Consider the example from §4.3.1:
    \[
    (x-y)\left(f(x,y)\left(x+g(x-y,s)\right)-2s\right)<3s
    \]
    None of the parantheses are resized, and the expression remains difficult to read.
  3. The sizing might be too big:
    \[
    \left(\sup_{s\in[0,T]}a(s)\right)
    \]
  4. They often produce too much horizontal spacing:
    $f(x)g(y)$ is correct; $f\left(x\right)g\left(y\right)$ is not.
    TeX does not resize the parantheses in this case, but still inserts thin spaces on either side of \left(...\right), leading to a semantically incorrect expression. (The reason that TeX inserts thin spaces is not relevant here, but for the interested reader: TeX treats \left(...\right) as an Inner atom, and TeX always inserts a thin space between an Inner and an Ord; see §3.4 Keep an eye on TeX’s automatic spacing.)

(The \mleft and \mright commands from the mleftright package fix the issue with spacing around the delimiters, but the other problems pointed out above remain.)

In my opinion, these problems are sufficient reasons to avoid almost all use of \left and \right. If you do choose to use them, be wary of the abovementioned situations.

4.3.3 Declaring new delimiters

Delimiters other than parentheses often occur in absolute values, norms, inner products, etc. For expressions such as these, is common to define a macro which automatically fills in the delimiters:

\newcommand{\innerproduct}[2]{\langle #1, #2 \rangle}
$x$ and $y$ are orthogonal since $\innerproduct{x}{y}=0$.

The downside to this solution is that it’s impossible to resize the delimiters when the arguments are large. (As explained above, inserting \left, \right in the definition is not a good idea.) The \DeclarePairedDelimiter and \DeclarePairedDelimiterX commands from the mathtools package come to the rescue:

\DeclarePairedDelimiter\abs{|}{|}
\DeclarePairedDelimiterX\ip[2]{\langle}{\rangle}{#1,#2}
\[ \abs{-\frac12} = \abs*{-\frac12} = \abs[\Big]{-\frac12} \]
\[ \ip{\frac{x}{2}}{y} = \ip*{\frac{x}{2}}{y} = \ip[\Big]{\frac{x}{2}}{y}\]

The basic version of the new \abs command simply inserts left and right delimiters (both | in this case); the starred variant inserts \left, \right commands, and the optional argument can be any of the \big, \Big, … commands. With the \DeclarePairedDelimiterX command you can specify the number of input parameters, and specify the “body” of the expression.

4.4 Multi-line displays and alignment

Mathematical expressions or equations are often too large to fit into one line. The LaTeX package amsmath provides several environments for both single- and multi-line expressions. It's a good idea to always load amsmath in order to have these at hand.

amsmath provides the four environments equation, multline, gather and align for numbered mathematical displays (a display is a centered mathematical expression). Each of these have a “starred” variant (equation*, multline*, gather* and align*) which works the same, but does not number the equation(s). As explained in §4.2 Numbering equations, you should prefer the starred variant whenever you don't plan to refer to that display later on (that is, most of the time).

The \[...\] macro produces a single-line, non-numbered display. If amsmath has been loaded then \[...\] is merely a synonym for \begin{equation*} ... \end{equation*}.

In multi-line displays, the \\ macro signals the end of a line. (Note that the final line of a multi-line display should not end with \\.) This macro has a starred variant \\* which ensures that the page will not break at that point. (In any case, this can only happen if display breaks have been enabled with \allowdisplaybreaks.) The \\ macro also takes an optional argument to explicitly set the vertical spacing between the two lines: \\[2en].

4.4.1 The equation environment

The equation environment produces a single-line, horizontally centred display. It produces a single equation number which is vertically centred.

Consider the equation
\begin{equation}\label{eq:myequation}
\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}x = \begin{pmatrix} 1 \\ -1 \end{pmatrix}.
\end{equation}
It is easy to solve \eqref{eq:myequation}.

4.4.2 The multline environment

The multline environment produces a multi-line display. The first line is left-aligned, the last line is right-aligned, and any remaining lines are centred. A multline produces a single equation number which is either top- or bottom-aligned, depending on your configuration (cf. the amsmath documentation, §3.3).

However, the alignment produced by multline often goes against well-established conventions; see §3.6 Aligning displays. For this reason, I would generally recommend against its use.

4.4.3 The gather environment

The gather environment gathers two or more equations. Each line is numbered and center aligned, independently of the others.

\begin{gather}
1+1=2 \label{eq:two} \\
1+1+1=3 \label{eq:three}
\end{gather}

4.4.4 The align environment

align/align* is used for multi-line, aligned expressions. The unstarred version numbers each line, and the lines are aligned wherever you put an ampersand (&):

\begin{align}
f(x) &= 4x - 3x^3 \label{eq:fdefinition} \\
g(x) &= e^x - \sin x + 2\cos x + x - 2x^2 \label{eq:gdefinition}
\end{align}

It is important that the ampersand is placed to the left of relational operators (i.e., symbols such as =, <, >, etc.). If you write =& then TeX will not treat the = symbol as a relational operator, resulting in incorrect spacing. If you really do need the ampersand after the =, then write ={}& or \mathrel{=}& to make TeX interpret = correctly. This can be useful when the right-hand side of an equality does not fit in one line.

align/align* can also distribute aligned expressions over multiple columns, by adding extra ampersands:

\begin{align*}
f(x) &= 4x - 3x^3 & h(x) &= \cos x \\
g(x) &= e^x - \sin x + 2\cos x + x - 2x^2 & i(x)&= \sin x
\end{align*}

align/align* has two siblings: alignat and flalign. The alignat environment works like align, but you can explicitly specify the column spacing:

\begin{alignat*}{2}
f(x) &= \sum_{k=0}^\infty x^k && \text{(by definition)} \\
&= 1+x+x^2+x^3+x^4+\dots &\quad& \text{(writing things out)} \\
&= \frac{1}{1-x} && \text{(geometric series formula)}
\end{alignat*}

(Note that the equals sign is placed either in the middle of an expression, or at the beginning of a line. You should never end a line with an equals sign.)

The argument to alignat (2 in this example) is the number of columns in the display. The \quad (see §3.3 Insert space to increase readability) in the second line specifies the spacing between the columns.

The flalign (“full length align”) environment works like align, but makes the space between columns as large as possible. Consult §3.6 of the Amsmath documentation for its usage.

4.4.5 Embedded multi-line expressions

In addition to the above environments for producing displays, amsmath offers the split, gathered, aligned and alignedat environments. These environments are not used on their own, but must be embedded in a display environment.

The gathered, aligned and alignedat environment are analogous to their gather, align and alignat counterparts (see §4.4.2§4.4.4), but are meant to be embedded in display environments in order to compose larger, more complex expressions:

Consider the compressible Navier--Stokes equations
\begin{align}
\left.
\begin{aligned}
    \partial_t \rho + \nabla\cdot (\rho u) &= 0 \\
    \partial_t (\rho u) + \nabla\cdot(\rho u\otimes u) &= \Delta (\rho u)
\end{aligned}
\right\}&
& &\text{for } x\in\Omega,\ t>0 \label{eq:navier-stokes} \\
\left.
\begin{aligned}
    \rho(x,0) &= \rho_0(x) \\
    u(x,0) &= \frac{m_0(x)}{\rho_0(x)}
\end{aligned}
\right\}&
& &\text{for } x\in\Omega. \label{eq:initialdata}
\end{align}

(I have indented the source for clarity; this does not affect the compiled document.)

The align environment in this example has two numbered lines and two columns; the first column is right-aligned and the second left-aligned. The first column consists of two embedded aligneds, each of which contains an aligned expression. It is the align, and not the aligned environments, that produces the equation numbers.

aligned can also be very useful for adjusting horizontal spacing in long expressions; see §3.6 Aligning displays.

Similarly to aligned, its siblings gathered and alignedat produce embedded expressions that are analogous to gather and alignat.

The final embedded environment, split, is similar to aligned, but with two important differences: a split always takes up the entire width of the page, and it allows only zero or one alignment point (using an ampersand &). It is usually used in conjunction with equation to typeset multiline expressions with a single equation number:

Consider the incompressible Navier--Stokes equations
\begin{equation}\label{eq:navier-stokes}
\begin{split}
\partial_t u + u\cdot\nabla u + \nabla p &= \Delta u \\
\mathrm{div}(u) &= 0.
\end{split}
\end{equation}

4.5 Some lesser sins

Finally, we collect some smaller, but still important, rules that are often bent or broken.

4.5.1 Differentials

When typesetting an integral, the integrand and the differential should always be separated by a thin space, as should each differential in an iterated integral. (See §3.3 for more on spaces.) Some people prefer to typeset the differential symbol d as a roman (upright) symbol, but this is a matter of taste. Whichever variant you choose, it’s a good idea to define a macro for denoting the differential:

\newcommand{\diff}[1]{\,d#1}
\newcommand{\diffr}[1]{\,\mathrm{d}#1}
Inline: $\iint xy \diff x \diff y = \iint xy \diffr x \diffr y$.
Display: \[ \iint xy \diff x \diff y = \iint xy \diffr x \diffr y. \]

(The above macros can be used as both \diff{x} and \diff x.)

The debate of roman versus slanted d in differentials is very old, dating at least as far back as the 1700s, and mathematicians still have not agreed upon a standard. (Physics and engineering publications are much more uniform in employing roman ds.) I personally prefer the slanted d because I find that the roman d results in an inconsistent overall look of the expression: The integral symbol and the d logically belong together, and since the integral symbol is slanted, so should the d.

It is also reassuring to note that both Donald Knuth and Michael Spivak, two emminent authorities in mathematical typesetting, both write slanted differentials.

4.5.2 Colons

In the expression (which is read as f is a function from A to B) the colon is a punctuation mark, not a relational operator. A colon typeset as : is treated by TeX as a relational operator (and hence gets thin spaces on either side), whereas \colon is treated as punctuation (and hence only gets a thin space to its right).

Thus, always typeset as f \colon A \to B, and not as f:A \to B. To help you remember this, it’s a good idea to redefine this command using \newcommand{\from}{\colon}, so that is typeset as f \from A \to B.

4.5.3 “Such that” symbols

When defining sets, the symbols : and | are often used to mean “such that” or “satisfying”: . Whichever “such that symbol” you choose, it should be typeset as a relational operator, so as to get typeset with a thin space on both sides. As seen above, : is always interpreted as a relational operator, but the | character is not. Write instead \mid (which is shorthand for \mathrel{|}).

4.5.4 Norms

The double vertical bar symbol that denotes a norm is typeset as \|, and not as ||. The latter is interpreted by TeX as “| times |” and produces too much space between the bars.

4.5.5 “Defined as” symbols

The symbol ≔ is often used for definitions: . If typeset as := (a colon and an equals sign), the colon will be slightly too low. Instead, use the command \coloneqq from the mathtools package. The command \eqqcolon produces the mirrored variant .

Make sure you use these symbols correctly! The expression means “x is a new symbol, and it has the same value as y”, and not the opposite! Thus, is correct, whereas is meaningless.

4.5.6 Symbols for multiplication

Never, ever use the symbol * for multiplication. As far as possible, you should follow the convention that an expression of the form “x times y” is typeset as xy. In the rare cases where this notation causes confusion (such as when both x and y are decimals), use a centred dot, typeset as \cdot.

You should try to avoid the symbol × (typeset as \times) for multiplication, since it has many other meanings, including the cross product and the Cartesian product. But never, ever use *.

4.5.7 Fractions

Fractions are usually typeset using \frac, which is shorthand for \dfrac in display mode and \tfrac in text mode. Both of these might be too large in a sub- or superscript, though, and the alternative a/b is not always so elegant. The \nicefrac command from the nicefrac package (some prefer \sfrac from the xfrac package) produces a split level fraction:

\[
\frac{1}{2}=\dfrac{1}{2}=\tfrac{1}{2}=1/2=\nicefrac{1}{2} \qquad
5^{\frac{2}{3}}=5^{2/3}=5^{\nicefrac{2}{3}}
\]