LaTeX/Mathematics
![]() |
---|
One of the greatest motivating forces for Donald Knuth when he began developing the original TeX system was to create something that allowed simple construction of mathematical formulae, while looking professional when printed. The fact that he succeeded was most probably why TeX (and later on, LaTeX) became so popular within the scientific community. Typesetting mathematics is one of LaTeX's greatest strengths. It is also a large topic due to the existence of so much mathematical notation.
If your document requires only a few simple mathematical formulas, plain LaTeX has most of the tools that you will ever need. If you are writing a scientific document that contains numerous complex formulas, the amsmath package[1] introduces several new commands that are more powerful and flexible than the ones provided by basic LaTeX. The mathtools package fixes some amsmath quirks and adds some useful settings, symbols, and environments to amsmath.[2] To use either package, include:
\usepackage{amsmath}
or
\usepackage{mathtools}
in the preamble of the document. The mathtools package loads the amsmath package and hence there is no need to \usepackage{amsmath}
in the preamble if mathtools is used.
Contents
- 1Mathematics environments
- 2Symbols
- 3Greek letters
- 4Operators
- 5Powers and indices
- 6Fractions and Binomials
- 7Roots
- 8Sums and integrals
- 9Brackets, braces and delimiters
- 10Matrices and arrays
- 11Adding text to equations
- 12Formatting mathematics symbols
- 13Color
- 14Plus and minus signs
- 15Controlling horizontal spacing
- 16Manually Specifying Formula Style
- 17Advanced Mathematics: AMS Math package
- 18List of mathematical symbols
- 19Summary
- 20Notes
- 21Further reading
- 22External links
Mathematics environments[edit | edit source]
LaTeX needs to know when the text is mathematical. This is because LaTeX typesets math notation differently from normal text. Therefore, special environments have been declared for this purpose. They can be distinguished into two categories depending on how they are presented:
- text — text formulas are displayed inline, that is, within the body of text where it is declared, for example, I can say that {\displaystyle a+a=2a}
within this sentence.
- displayed — displayed formulas are on a line by themselves.
As math requires special environments, there are naturally the appropriate environment names you can use in the standard way. Unlike most other environments, however, there are some handy shorthands for declaring your formulas. The following table summarizes them:
Type | Inline (within text) formulas | Displayed equations | Displayed and automatically numbered equations |
---|---|---|---|
Environment | math |
displaymath |
equation |
LaTeX shorthand | \(...\) |
\[...\] |
|
TeX shorthand | $...$ |
$$...$$ |
|
Comment | equation* (starred version) suppresses numbering, but requires amsmath |
Suggestion: Using the $$...$$
should be avoided, as it may cause problems, particularly with the AMS-LaTeX macros. Furthermore, should a problem occur, the error messages may not be helpful.
The equation*
and displaymath
environments are functionally equivalent.
If you are typing text normally, you are said to be in text mode, but while you are typing within one of those mathematical environments, you are said to be in math mode, that has some differences compared to the text mode:
- Most spaces and line breaks do not have any significance, as all spaces are either derived logically from the mathematical expressions or have to be specified with special commands such as
\quad
- Empty lines are not allowed. Only one paragraph per formula.
- Each letter is considered to be the name of a variable and will be typeset as such. If you want to typeset normal text within a formula (normal upright font with normal spacing), then you have to enter the text using dedicated commands.
Inserting "Displayed" maths inside blocks of text[edit | edit source]
In order for some operators, such as \lim
or \sum
, to be displayed correctly inside some math environments (read $......$
), it might be convenient to write the \displaystyle
class inside the environment. Doing so might cause the line to be taller, but will cause exponents and indices to be displayed correctly for some math operators. For example, the $\sum$
will print a smaller Σ and $\displaystyle \sum$
will print a bigger one {\displaystyle \displaystyle \sum }, like in equations (This only works with AMSMATH package). It is possible to force this behaviour for all math environments by declaring
\everymath{\displaystyle}
at the very beginning (i.e. before \begin{document}
).
Symbols[edit | edit source]
Mathematics has many symbols! The following is a set of symbols that can be accessed directly from the keyboard:
+ - = ! / ( ) [ ] < > | ' : *
Beyond those listed above, distinct commands must be issued in order to display the desired symbols. There are many examples such as Greek letters, set and relations symbols, arrows, binary operators, etc.
For example:
\forall x \in X, \quad \exists y \leq \epsilon
|
{\displaystyle \forall x\in X,\quad \exists y\leq \epsilon \,} |
Fortunately, there's a tool that can greatly simplify the search for the command for a specific symbol. Look for "Detexify" in the external links section below. Another option would be to look in "The Comprehensive LaTeX Symbol List" in the external links section below.
Greek letters[edit | edit source]
Greek letters are commonly used in mathematics, and they are very easy to type in math mode. You just have to type the name of the letter after a backslash: if the first letter is lowercase, you will get a lowercase Greek letter, if the first letter is uppercase (and only the first letter), then you will get an uppercase letter. Note that some uppercase Greek letters look like Latin ones, so they are not provided by LaTeX (e.g. uppercase Alpha and Beta are just "A" and "B", respectively). Lowercase epsilon, theta, kappa, phi, pi, rho, and sigma are provided in two different versions. The alternate, or variant, version is created by adding "var" before the name of the letter:
\alpha, \Alpha, \beta, \Beta, \gamma, \Gamma, \pi, \Pi, \phi, \varphi, \mu, \Phi
|
{\displaystyle \alpha ,\mathrm {A} ,\beta ,\mathrm {B} ,\gamma ,\Gamma ,\pi ,\Pi ,\phi ,\varphi ,\mu ,\Phi } |
Scroll down to #List of mathematical symbols for a complete list of Greek symbols.
Operators[edit | edit source]
An operator is a function that is written as a word: e.g. trigonometric functions (sin, cos, tan), logarithms and exponentials (log, exp), limits (lim), as well as trace and determinant (tr, det). LaTeX has many of these defined as commands:
\cos (2\theta) = \cos^2 \theta - \sin^2 \theta
|
{\displaystyle \cos(2\theta )=\cos ^{2}\theta -\sin ^{2}\theta \,} |
For certain operators such as limits, the subscript is placed underneath the operator:
\lim\limits_{x \to \infty} \exp(-x) = 0
|
{\displaystyle \lim _{x\to \infty }\exp(-x)=0} |
For the modular operator there are two commands: \bmod
and \pmod
:
a \bmod b
|
{\displaystyle a\,{\bmod {\,}}b\,} |
x \equiv a \pmod{b}
|
{\displaystyle x\equiv a{\pmod {b}}\,} |
To use operators that are not pre-defined, such as argmax, see custom operators
Powers and indices[edit | edit source]
Powers and indices are equivalent to superscripts and subscripts in normal text mode. The caret (^
; also known as the circumflex accent) character is used to raise something, and the underscore (_
) is for lowering. If an expression containing more than one character is raised or lowered, it should be grouped using curly braces ({
and }
).
k_{n+1} = n^2 + k_n^2 - k_{n-1}
|
{\displaystyle k_{n+1}=n^{2}+k_{n}^{2}-k_{n-1}\,} |
For powers with more than one digit, surround the power with {}.
n^{22}
|
{\displaystyle n^{22}\,} |
An underscore (_
) can be used with a vertical bar ({\displaystyle |}) to denote evaluation using subscript notation in mathematics:
f(n) = n^5 + 4n^2 + 2 |_{n=17}
|
{\displaystyle f(n)=n^{5}+4n^{2}+2|_{n=17}\,} |
Fractions and Binomials[edit | edit source]
A fraction is created using the \frac{numerator}{denominator}
command (for those who need their memories refreshed, that's the top and bottom respectively!). Likewise, the binomial coefficient (a.k.a, the Choose function) may be written using the \binom
command[3]:
\frac{n!}{k!(n-k)!} = \binom{n}{k}
|
{\displaystyle {\frac {n!}{k!(n-k)!}}={\binom {n}{k}}} |
You can embed fractions within fractions:
\frac{\frac{1}{x}+\frac{1}{y}}{y-z}
|
{\displaystyle {\frac {{\frac {1}{x}}+{\frac {1}{y}}}{y-z}}} |
Note that when appearing inside another fraction, or in inline text {\displaystyle {\tfrac {a}{b}}}, a fraction is noticeably smaller than in displayed mathematics. The
\tfrac
and \dfrac
commands[3] force the use of the respective styles, \textstyle
and \displaystyle
. Similarly, the \tbinom
and \dbinom
commands typeset the binomial coefficient.
For relatively simple fractions, especially within the text, it may be more aesthetically pleasing to use powers and indices instead:
^3/_7
|
{\displaystyle ^{3}/_{7}\,} |
If this looks a little "loose" (i.e., overspaced), a tightened version can be defined by inserting some negative space
%running fraction with slash - requires math mode.
\newcommand*\rfrac[2]{{}^{#1}\!/_{#2}}
\rfrac{3}{7}
|
{\displaystyle {{}^{3}\!/_{7}}} |
If you use them throughout the document, usage of xfrac package is recommended. This package provides \sfrac
command to create slanted fractions. Usage:
Take $\sfrac{1}{2}$ cup of sugar, \dots
3\times\sfrac{1}{2}=1\sfrac{1}{2}
Take ${}^1/_2$ cup of sugar, \dots
3\times{}^1/_2=1{}^1/_2
|
If fractions are used as an exponent, curly braces have to be used around the \sfrac
command:
$x^\frac{1}{2}$ % no error $x^\sfrac{1}{2}$ % error $x^{\sfrac{1}{2}}$ % no error
$x^\frac{1}{2}$ % no error
|
{\displaystyle x^{\frac {1}{2}}} |
In some cases, using the package alone will result in errors about certain font shapes not being available. In that case, the lmodern and fix-cm packages need to be added as well.
Alternatively, the nicefrac package provides the \nicefrac
command, whose usage is similar to \sfrac
.
Continued fractions[edit | edit source]
Continued fractions should be written using \cfrac
command[3]:
\begin{equation}
x = a_0 + \cfrac{1}{a_1
+ \cfrac{1}{a_2
+ \cfrac{1}{a_3 + \cfrac{1}{a_4} } } }
\end{equation}
|
{\displaystyle x=a_{0}+{\cfrac {1}{a_{1}+{\cfrac {1}{a_{2}+{\cfrac {1}{a_{3}+{\cfrac {1}{a_{4}}}}}}}}}} |
Multiplication of two numbers[edit | edit source]
To make multiplication visually similar to a fraction, a nested array can be used. For example, multiplication of numbers written one below the other can be typeset as follows:
\begin{equation}
\frac{
\begin{array}[b]{r}
\left( x_1 x_2 \right)\\
\times \left( x'_1 x'_2 \right)
\end{array}
}{
\left( y_1y_2y_3y_4 \right)
}
\end{equation}
|
{\displaystyle {\frac {\begin{array}{r}\left(x_{1}x_{2}\right)\\\times \left(x'_{1}x'_{2}\right)\end{array}}{\left(y_{1}y_{2}y_{3}y_{4}\right)}}} |
Roots[edit | edit source]
The \sqrt
command creates a square root surrounding an expression. It accepts an optional argument specified in square brackets ([
and ]
) to change magnitude:
\sqrt{\frac{a}{b}}
|
{\displaystyle {\sqrt {\frac {a}{b}}}} |
\sqrt[n]{1+x+x^2+x^3+\dots+x^n}
|
{\displaystyle {\sqrt[{n}]{1+x+x^{2}+x^{3}+\dots +x^{n}}}} |