Formulas are the reason many people meet LaTeX for the first time, and also the reason they stay with it. Once you have learned the syntax, writing an equation is faster than building it with a graphical editor, and the result is typographically correct: proper spacing, italics for variables, upright operators.
The same syntax, moreover, is used outside LaTeX too: many note apps, wikis and documentation platforms accept formulas written this way.
Inline and displayed formulas
There are two modes:
- Inline, inside a sentence:
the area is $A = \pi r^2$. The formula is compressed to fit the height of the line. - Display, on a line of its own and centred:
\[ A = \pi r^2 \]. Fractions and sums get more room.
Avoid the old $$ ... $$ for displayed formulas: it works, but it is plain TeX syntax that in LaTeX can give inconsistent spacing. \[ ... \] is the recommended form.
The package to load every time
In the preamble add:
\usepackage{amsmath}
\usepackage{amssymb}
amsmath provides the environments for numbered equations, systems and multi-line formulas; amssymb adds many symbols, such as the number sets \mathbb{R} and \mathbb{N}. Almost every document with formulas loads them.
The basic syntax
| Written as | Result |
|---|---|
x^2, x^{10} |
Superscript; more than one character needs braces |
a_i, a_{i+1} |
Subscript |
\frac{a}{b} |
Fraction |
\sqrt{x}, \sqrt[3]{x} |
Square and cube root |
\sum_{i=1}^{n} |
Sum with limits |
\int_0^1 f(x)\,dx |
Integral with a thin space before dx |
\alpha, \beta, \Omega |
Greek letters |
\leq, \geq, \neq, \approx |
Relations |
The most common mistake is forgetting the braces: x^10 produces x to the power of 1 followed by a 0.
Functions and text inside formulas
Variables go in italics, but function names do not. That is why dedicated commands exist: \sin x, \log n, \max, \lim_{x \to 0}. Writing sin x without the backslash shows three italic variables multiplied together.
For real words inside a formula you use \text{}:
\[ v = \frac{d}{t} \quad \text{with } t > 0 \]
If you need an operator that does not exist, amsmath lets you define it in the preamble: \DeclareMathOperator{\sgn}{sgn}.
Brackets that adapt
Ordinary brackets have a fixed height and look too small around a fraction. \left( and \right) resize them automatically:
\[ \left( \frac{a+b}{2} \right)^2 \]
The same goes for square brackets, braces (\left\{ ... \right\}) and absolute value bars.
Numbered equations and references
\begin{equation}
E = m c^2
\label{eq:energy}
\end{equation}
As equation~\eqref{eq:energy} shows, ...
The equation environment numbers the formula; \label gives it a name and \eqref recalls it with the number in brackets. If you add an equation earlier, all the numbering updates itself. The tilde ~ is a space that prevents a line break between the word and the number.
For a displayed formula without a number there is the starred variant, equation*.
Multi-line formulas and systems
To align several steps on the equals sign you use align, with & as the alignment point and \\ for a new line:
\begin{align}
(a+b)^2 &= (a+b)(a+b) \\
&= a^2 + 2ab + b^2
\end{align}
For systems and piecewise functions there is cases:
\[
|x| = \begin{cases}
x & \text{if } x \geq 0 \\
-x & \text{if } x < 0
\end{cases}
\]
Matrices
amsmath offers ready-made environments with the brackets included: pmatrix for round brackets, bmatrix for square brackets, vmatrix for a determinant.
\[ A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \]
Good habits
- A formula is part of the sentence: add punctuation after displayed equations too.
- Number only the equations you refer to in the text.
- Do not use
\\to break a formula insideequation: usealignormultline. - If you forget a symbol, the websites that recognise a hand-drawn symbol and return its LaTeX command save a lot of time.
In short
$...$for formulas in the text,\[...\]for displayed ones.amsmathandamssymbbelong in almost every document with formulas.- Braces for long superscripts and subscripts, dedicated commands for functions such as
\sin. equation,\labeland\eqrefhandle automatic numbering and references.align,casesandpmatrixcover aligned steps, systems and matrices.