How to align equations in LaTeX
To line several equations up on their equals signs, LaTeX has the align environment of the amsmath package: put & just before the symbol to align on, and \\ at the end of each row. This page covers align and the three environments that go with it — gather, multline and aligned — with the source to copy.
Aligning on the equals sign
(a+b)^2 &= (a+b)(a+b) \\
&= a^2 + 2ab + b^2Each & marks the point that is lined up from one row to the next. The second row has nothing on its left: it starts with &=.
In a .tex file these rows go between \begin{align} and \end{align}. In Texifine you type the rows only: insert an Equation block and choose align in the right column.
Several equations, one under the other
The same environment sets a system:
x + y &= 5 \\
2x - y &= 1A comment on each row
A second && opens another aligned column, which is where a short justification goes. \text{…} writes words inside a formula.
x &= y + z && \text{by definition} \\
&= 2z && \text{since } y = zNumbering
align numbers every row. The starred form, align*, numbers none.
In Texifine the choice is Numbered or Plain in the right column, for the whole block: every row or none. A numbered block of three rows shows its range, for example (2)–(4). See Numbering and references.
For one number on a group of aligned rows, use aligned (below).
Centred, not aligned: gather
When the formulas have nothing to line up on, gather centres each of them. No &, only \\ between rows.
a^2 + b^2 = c^2 \\
e^{i\pi} + 1 = 0One long formula over several lines: multline
multline is for a single formula too long for the line: the first line is set to the left, the last to the right, and the whole takes one number. \\ marks where it breaks.
p(x) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + a_4 x^4 \\
+ a_5 x^5 + a_6 x^6 + a_7 x^7 + a_8 x^8One number for the whole group: aligned
aligned is written inside another formula. The rows are aligned as in align, but the group is one object: it takes a single number, centred on its height.
\begin{aligned}
\nabla \cdot \mathbf{E} &= \frac{\rho}{\varepsilon_0} \\
\nabla \cdot \mathbf{B} &= 0
\end{aligned}In Texifine, keep the block's environment on equation and type the aligned group yourself.
Because it is an object, it can also sit next to something else — a brace, for a system:
\left\{
\begin{aligned}
x + y &= 5 \\
2x - y &= 1
\end{aligned}
\right.\left\{ draws a brace as tall as the group; \right. closes it with nothing.
Which one to choose
| You want | Environment |
|---|---|
Steps of a calculation, aligned on = | align |
| Several unrelated formulas, centred | gather |
| One formula too long for the line | multline |
| Aligned rows that share one number | aligned, inside equation |
| A single formula | equation |
Common mistakes
- An
&in an environment that does not align. Inequation, LaTeX stops on "Misplaced alignment tab character &"; ingatherormultline, on "Extra alignment tab has been changed to \cr". Choose align, or wrap the rows inaligned. - A missing
\\. Two rows without\\between them are read as one row. - A blank line in the source. In a
.texfile, an empty line insidealignstops the compilation on "Paragraph ended before \align was complete". Texifine removes the empty lines of a formula before compiling, so one left by accident does no harm.
When a document does not compile, the PDF tab shows the first error and its line. See Fixing compilation errors.
See also
- Equation — the block, its environments and its keyboard.
- How to write a matrix in LaTeX
- Fractions, sums, integrals and limits in LaTeX
- Writing a mathematics course or paper