Math: Basics
General Tips
Get Gratzer's book "Math into LaTeX".
This book is a must for anyone doing a significant amount of
mathematical typesetting. It is the only LaTeX book that fully covers
the enhancements provided by the amslatex package. If you buy any book at
all, get this one. If you don't want to spend the money on the book, download a pdf file
of the first chapter. (This is based on an older edition, but still
useful.)
Use the amslatex packages.
Amslatex is a collection of packages that greatly enhance
the mathematical capabilities of Latex and have become indispensible for
typesetting documents with significant mathematical content. The principal
packages are amsmath and amsthm.
To make these packages available, add
"\usepackage{amsmath, amsthm}"
near the beginning of the document, right after "\documentclass{...}".
(With the "amsart" documentclass, these are automatically loaded, but
there is no harm in leaving the \usepackage{...} instruction in.)
Most of the tips below take advantage of the features provided by
these packages and assume that you have loaded the packages, as shown
above.
Use the math mode of TeX correctly: enclose all mathematical material in dollar
signs (or equivalent environments), while leaving any nonmathematical material
outside dollar sign pairs.
This might seem like a an obvious rule, but it is commonly violated. Here are
some examples:
-
Enclose variables and numbers embedded in regular text
within dollar signs.
For example, in a phrase like "Let F be a field"
"F" represents a mathematical quantity and thus should be enclosed in dollar
signs: "Let $F$ be a field." The same goes for numbers, if used in a
mathematical context: Instead of "In dimension 3 we have ..." use "In
dimension $3$ we have ...". (However, in textual contexts such as "Section 3"
or "Theorem 1", the numbers should be left in text mode. The same goes for
item labels, e.g. "by part (i) of Theorem 1", etc.)
-
Leave punctuation signs outside (inline) mathematical expressions.
A surprisingly common mistake is to include punctuation signs within the
dollar signs delimiting a math formula. Punctuation signs are not part
of the formula (they belong to the surrounding text), and therefore
should not be enclosed within the dollar signs. For example, in the
phrase "let $f(x)= cx$, where c is a constant", the comma is not part of
the math expression and therefore should be outside the dollar sign
pair. It would be wrong to set this as "let $f(x)= cx,$
where c is a constant".
-
Use mode-specific font commands.
Most font changing commands come in two versions, one for ordinary text
(e.g., \textbf{...}), and the other for mathematical material
(e.g. \mathbf{...}). Use the version appropriate for the
mode, e.g.: " a \textbf{field} is ...", "... a vector
$\mathbf{v}$ ...".
-
Enclose text material inside displays in \text{...}.
\text{...} causes the expression enclosed in braces to be typeset
in text mode. This is useful in displayed formulas that
involve some textual material.
For example, in the expression "f(x)= \sin x and g(x)=\cos x ", the word
"and" is ordinary text and thus should be typeset in text mode:
"\[ f(x)=\sin x \quad \text{and}\quad g(x)=\cos x \]".
(Note here that the text is separated from the surrounding formulas by a
"\quad" spacing command.)
-
Don't italicize words by placing them inside $ ... $.
The letters do come out italicized, but the spacing looks awful, since
it is optimized for mathmode and the letters will be typeset as if they
were mathematical variables, multiplied together.
-
Use \operatorname for words acting as mathematical operators.
For example, the correct way to typeset the phrase
"Let rank A denote the rank of a matrix A" is
"Let $\operatorname{rank} A$ denote the rank of a matrix $A$". The second
occurrence of "rank" is part of the ordinary English text and thus should
be left in text mode. By contrast, in the first occurrence ("... rank A ...")
"rank" is mathematical operator that should be enclosed
within the dollar sign pair. The "\operatorname{...}" command (see below)
ensures that it gets typeset with the spacing and fonts appropriate for a "log
like" operator.
Use spacing commands in math mode sparingly;
if you do need explicit spacing, use standard spacing commands such as
"\quad", "\qquad", rather than multiple instances of single blanks ("\ \ \ "),
or ties ("~~~~").
While there are spacing commands like "\," "\!" available to finetune the
spacing in math mode, TeX usually gets the spacing right on its own,
so those commands should be used very sparingly.
In my experience, the vast majority of authors who use such manual spacing
commands do so inappropriately, sometimes to compensate for other coding
errors (e.g., not using the "\operatorname{...}" construct for a math
operator). This usually results in a typeset output that looks worse than the
output that TeX would have generated without such forced spacing.
If in doubt, trust TeX on getting the spacing right.
One situation where explicit spacing commands are needed is in displayed
formulas, e.g., to offset an expression such as "(i=1,2,...,n)" or "for
i=1,2,...,n" from a formula, or in multiline displays to cause continuation lines
to be shifted a bit to the right. In those instances I would use
"\quad" or "\qquad" (the latter only in multiline displays)
to obtain the appropriate amount of space. I recommend against using multiple
explicit blanks (e.g., "\ \ \ " or "~~~~") since that makes for ugly looking
and hard to read and maintain code, and haphazard spacing. By sticking to
standard units (quads) in the spacing, one achieves a more uniform look.
Keep in mind that in math mode TeX ignores all spaces (except for a blank
line). Thus, you can break up complex formulas by inserting blank spaces
and linebreaks at appropriate places in the code, in order to improve the
readability of the code.
This is especially useful in complex, multiline displays. I often
see such displays set as a single, very long line. However, this makes the
editing of the displays a tricky and potentially risky undertaking (since
editors typically operate in line mode, a single incorrect editing command may
mess up the entire line), and it makes hard to track down errors and spot
such things as forgotten parantheses.
Thus, I recommend breaking up long formulas into shorter "chunks", separated
by (single) linebreaks. This has no effect on the output since TeX ignores
spaces (including linebreaks) in math mode,
but it greatly
improves the readability and maintainability of the code.
For example, a fraction set with "\frac{....}{....}", with complex
expressions in the numerator and denominators, becomes more readable if the
second pair of braces is placed on a separate line by itself.
The only exception to the spacing rule for math mode is
a blank line (more precisely, two consecutive linebreaks, or two linebreaks
separated only by other spacing commands). This is
not allowed in math mode and will cause an error message.
Avoid forcing displaystyle with \displaystyle or \limits for inline math
material.
TeX typesets fractions, sums, integrals, and similar "large" expressions,
differently depending on whether they occur "inline" (i.e., embedded in
a paragraph of regular text), or in a displayed formula. For example, in sums
occurring inline the summation limits are set as subscripts or superscripts
to the summation symbol, while for sums in displayed equations these limits
appear above and below the summation symbol. In fractions set inline the
numerator and denominator are reduced, while in displayed fractions numerator
and denominator appear in normal size. These and similar typesetting
conventions ensure that mathematical expressions set inline do not protrude
too much into the surrounding text or consume an excessive amount of vertical
space.
One could override this default behavior of TeX and force display style on
inline math material with the \displaystyle command, or, in the case of sums,
use the \limits command to force the summation limits to appear above and
below the summation symbol. However, this is almost always a bad idea, as the
resulting output would look very poor. If the expression is complex enough
(e.g., one involving multiple levels of subscripts or fractions) that it would
become too small when set in inline style, it should probably be set as a
displayed equation, or rewritten, e.g., using slashed fractions or other
notational devices (e.g., rewriting a fraction using negative exponents).
Set lengthy formulas or "tall"
mathematical expressions as displays, rather than inline.
Complex mathematical material usually does not look good inline. Moreover,
even though TeX can break lines in inline math material, there are usually
far too few good spots for linebreaks; as a result, one often has to deal with
overfull or underfull boxes, and poor linebreaks. Furthermore, trying to
fix such linebreaks may introduce additional bad linebreaks or
overfull/underfull boxes further down in the paragraph.
These problems can be avoided by setting larger, more complex expressions as
displays. What should be displayed depends, of course, on the context, but
as a general rule, if an equation takes up more than half a line when set
inline, I would consider using a display for it.
Use "slashed fraction" notation for fractions set inline, occurring
in a subscript or superscript context.
Fractions set in the standard "stacked fraction" notation do not look good
when they occur in inline math mode, or in a context (e.g., a superscript, subscript,
summation or integration limit) where the size of a fraction is reduced. In
almost all of those situations, it is usually best to rewrite the fraction
as "slashed fraction": For example, in inline mode, replace
"\frac{1}{2}" by "1/2", and "\frac{q}{1-p}" by "q/(1-p)".
In a display, replace
"\sum_{\frac{x}{y}\le n \le x}f(n)^{\frac{1}{3}}" by
"\sum_{x/y\le n\le x}f(n)^{1/3}".
Use "\operatorname" to define new "log like" math operators.
For example,
"\newcommand{\Ext}{\operatorname{Ext}}" causes "\Ext" to behave much like
"\sin" or "\log". This ensures that the spacing and fonts come out right. This
is preferable to manual constructs with "\mbox", "\text", etc.
Miscellaneous tips.
Use "\substack", not "\atop", for multiline summation conditions.
The \atop command is a relic from plain TeX and has been rendered obsolete by
the \substack construct provided by the amsmath package. Using \atop in a
document with the amsmath package loaded generates a warning message.
Use "\mid", not "|", for the vertical bar in the set notation.
The proper way to code the vertical bar in a set "{ ... | ....}" is with
"\mid", rather than the vertical bar character ("|"). This generates a
vertical bar, just the bar character, but it also adds an appropriate amount
of space to the left and the right of the bar symbol.
Use "\Vert" (or "\|"), not "||", for a double bar indicating a norm.
With two separate bar symbols, the bars would be spaced out too much.
The "\Vert" macro (or equivalently, "\|") generates a "double bar"
with the correct amount of spacing for a norm.
Use "\dots", not "...", for ellipsis in math mode.
Three single periods ("...") are appropriate for an ellipsis in text mode, but not
in a mathematical context, such as a "k=1,2,...,n", or "1 + 2 + ... + n".
In the latter cases, the "\dots" command is appropriate. It generates not only
the right amount of spacing between the dots, but it also places the dots at the
appropriate level, namely on the baseline in the first context, and centered in the
second context. Instead of "\dots" one could have used the more explicit
commands "\ldots" (lower dots) in the first case, and "\cdots" (centered dots)
in the second. However, using "\dots" has the advantage that one doesn't have
to worry about the placement of the dots since the TeX gets it right in almost
all cases. The only exception is the case of terminating dots that should be
centered (e.g., in "f(1)+f(2)+..."). In this case, an explicit "\cdots" is
needed.
Use "\binom" for binomial coefficients.
A surprisingly common mistake, even by otherwise TeX-literate authors,
is to generate binomial coefficients in a cumbersome manner, e.g., using a
matrix or array environment (which generate inferior output),
or using the "\choose" construct (a relic from plain TeX, which generates a rather
uninformative warning message when used in a document with the amsmath package
loaded.) The correct way to typeset binomial coefficients is with the
"\binom{...}{...}" macro, which has the same syntax as the fraction macro
"\frac".
Back to the LaTeX Tips Page
Last modified: Tue 23 Aug 2011 05:47:56 PM CDT
A.J. Hildebrand