1. Writing mathematics | 3. Whitespace |
2. Typesetting text | 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.
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:
It should be typeset as follows:
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.
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:
(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.
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:
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.
\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.
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.
\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:
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:
\left ... \right
clause cannot be broken into multiple lines. (See §4.4 Multi-line displays and alignment for multi-line displays.)
\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.
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:
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:
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.
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]
.
equation
environment
The equation
environment produces a single-line, horizontally centred display. It produces a single equation number which is vertically centred.
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.
gather
environment The gather
environment gathers two or more equations. Each line is numbered and center aligned, independently of the others.
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 (&
):
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:
align
/align*
has two siblings: alignat
and flalign
. The alignat
environment works like align
, but you can explicitly specify the column spacing:
(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.
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:
(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 aligned
s, 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:
Finally, we collect some smaller, but still important, rules that are often bent or broken.
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:
(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.
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
.
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{|}
).
\|
, and not as ||
. The latter is interpreted by TeX as “| times |” and produces too much space between the bars.
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.
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 *
.
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: