The amsmath package
Adds the display environments align, gather and cases, the matrix environments, and commands such as \text, \dfrac and \DeclareMathOperator.
Loading the package
\usepackage{amsmath}Plain LaTeX has equation for one numbered formula and eqnarray for several lines, and eqnarray puts too much space around the relation sign. amsmath adds align for lines aligned at a point you choose, gather for centred lines, multline and split for one long formula, and the matrix environments matrix, pmatrix and bmatrix.
Inside align, & marks the alignment point and \\ ends a line. Every numbered environment has a starred form such as align* without equation numbers. The environments start display math themselves, so they do not go inside \[ ... \] or $ ... $.
Main commands
\begin{align} ... \end{align} | Several numbered formulas, aligned at &. |
\text{if } x > 0 | Upright text inside a formula, in the size of the surrounding math. |
\dfrac{a}{b} | Fraction in display size, also in inline math; \tfrac gives the small inline size. |
\begin{pmatrix} a & b \\ c & d \end{pmatrix} | Matrix in round brackets; bmatrix uses square brackets, vmatrix vertical bars. |
\begin{cases} ... \end{cases} | Case distinction with one large brace on the left. |
\DeclareMathOperator{\sgn}{sgn} | Defines an operator name that is set upright with the spacing of \sin. Goes into the preamble. |
\eqref{eq:binomial} | Reference to an equation, with the number in parentheses. |
\notag | Removes the number of one line; \tag{*} sets a label of your own instead. |
Complete example
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\sgn}{sgn}
\begin{document}
\begin{align}
(a + b)^2 &= a^2 + 2ab + b^2 \label{eq:binomial} \\
(a - b)^2 &= a^2 - 2ab + b^2 \notag
\end{align}
Equation~\eqref{eq:binomial} holds for all real $a$ and $b$.
\[
\sgn(x) =
\begin{cases}
1 & \text{if } x > 0, \\
0 & \text{if } x = 0, \\
-1 & \text{if } x < 0.
\end{cases}
\]
\end{document}Compiled with pdfLaTeX from TeX Live 2023. Tools run in this order: pdflatex → pdflatex.
Pitfalls
- A blank line inside
alignor another display environment stops the run with Paragraph ended before \label was complete. Delete the blank line or start it with%. \\after the last line ofalignadds an empty line with its own equation number.aligninside\[ ... \]or insideequationgives Erroneous nesting of equation structures. Usealignedthere, or remove the outer environment.- A matrix has at most 10 columns. An eleventh column gives Extra alignment tab has been changed to \cr; raise the limit with
\setcounter{MaxMatrixCols}{12}.
Related pages
- How-to Align equations in LaTeX
- How-to Matrices in LaTeX
- How-to Piecewise functions in LaTeX
- Package
mathtools - Error Erroneous nesting of equation structures
- How-to Fractions in LaTeX
- How-to Square root and nth root in LaTeX
- How-to Integrals in LaTeX
- How-to Sums and products in LaTeX
- How-to Limits in LaTeX