Writing Math in LaTeX
Math is where LaTeX is strongest. This guide covers formulas in a sentence and on their own line, fractions, roots, brackets that grow, multi-line derivations, numbering and piecewise functions. Load amsmath first: almost everything below uses it.
\usepackage{amsmath} % environments, \text, \eqref, \dfrac
\usepackage{amssymb} % extra symbols, \mathbbIn a sentence or on its own line
The area is $A = \pi r^2$, where $r$ is the radius.
\[
A = \pi r^2
\]$...$ keeps a formula inside the paragraph and shrinks fractions and limits to fit the line. \[...\] puts it on its own centred line at full size. Avoid the old $$...$$: it gets the spacing around the formula wrong.
Fractions
\frac{a}{b} % adapts to inline or display
\dfrac{a}{b} % always full size
\tfrac{a}{b} % always small, good in exponents
\cfrac{1}{1 + \cfrac{1}{x}} % continued fractionsIn running text, a slash often reads better than a small stacked fraction: $a/b$.
Roots
\sqrt{a^2 + b^2}
\sqrt[3]{27} = 3
\sqrt[n]{x}The root sign stretches to fit whatever is under it.
Brackets that fit their content
Plain brackets stay text height, which looks cramped around a fraction. Put \left and \right in front and they grow to fit:
\left( \frac{a}{b} \right)
\left[ \sum_{i=1}^{n} x_i \right]
\left\{ x \mid x > 0 \right\}
\left. \frac{df}{dx} \right|_{x=0} % \left. is an invisible bracketCurly braces need a backslash: \{ and \}. When the automatic size is too big, or the brackets sit on different lines of an align, choose a size yourself with \big, \Big, \bigg or \Bigg, as in \Big( ... \Big).
Several lines lined up: align
\begin{align}
f(x) &= x^2 + 2x + 1 \\
&= (x + 1)^2
\end{align}& marks the point that lines up, usually just before the equals sign, and \\ ends a line. Every line gets a number; use align* for none, or \nonumber on a line to skip it.
One number for a whole derivation
\begin{equation}
\begin{split}
(a + b)^2 &= (a + b)(a + b) \\
&= a^2 + 2ab + b^2
\end{split}
\label{eq:square}
\end{equation}Other multi-line environments
gather: several equations, each centred, no alignment.multline: one long equation broken over lines, the first pushed left and the last right.
Numbering and references
\begin{equation}
E = mc^2
\label{eq:energy}
\end{equation}
Equation~\eqref{eq:energy} shows that ...equation numbers a single formula; equation* does not. \eqref prints the number in parentheses, as in (1). To print a name instead of a number, use \tag{...}: a^2 + b^2 = c^2 \tag{Pythagoras}.
Piecewise functions: cases
\[
|x| =
\begin{cases}
x & \text{if } x \geq 0 \\
-x & \text{if } x < 0
\end{cases}
\]The & separates each value from its condition. \text keeps the words upright and spaced like normal text.
Words in a formula
f(x) = x^2 \quad \text{for all } x \in \mathbb{R}
\operatorname{argmax}_{\theta} \, L(\theta) % a named operatorTyping a word directly in math mode sets each letter as a variable, in italics and badly spaced. Use \text for words and \operatorname for function names that LaTeX does not already know. It knows \sin, \log, \max and many others.
Spacing
LaTeX spaces math on its own and ignores the spaces you type. Adjust it by hand only in a few places:
| Command | Space | Typical use |
|---|---|---|
\, | Thin | Before dx in an integral, between a number and its unit |
\; | Thick | Separating parts of a condition |
\quad | One em | Between a formula and its condition |
\qquad | Two em | Between two formulas on one line |
\! | Negative thin | Pulling symbols closer |
Matrices
\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} % parentheses
\begin{bmatrix} a & b \\ c & d \end{bmatrix} % square bracketsDeterminants, augmented matrices and large matrices are in the matrices guide.
Common commands
| Command | Meaning |
|---|---|
x^{n}, x_{i} | Exponent, index |
\sum_{i=1}^{n}, \int_a^b, \lim_{x \to 0} | Sum, integral, limit |
\leq, \geq, \neq, \approx | Comparisons |
\cdot, \times | Multiplication dot, cross |
\in, \subset, \cup, \cap | Sets |
\infty, \partial, \nabla | Infinity, partial derivative, gradient |
\hat{x}, \bar{x}, \vec{v}, \dot{x} | Estimator, mean, vector, time derivative |
\mathbb{R} | Real numbers (amssymb) |