Use amsmath’s alignat* environment to align equals signs and place explanations in a separate column beside each step. This guide shows a complete document for editing on a Mac, then explains how to change numbering, spacing, and longer annotations.
Start with a complete annotated derivation
You need a working LaTeX installation and an editor that saves plain-text .tex files. For macOS, the LaTeX Project points to MacTeX, which includes a TeX system and editors. If you already compile LaTeX documents successfully, use your existing setup. Getting LaTeX
Create a file named derivation.tex and paste the following complete document. The example uses abstract elements of a ring: multiplication distributes over addition, addition is associative, and multiplication need not be commutative.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Let $a,b,c,d$ be elements of a ring.
\begin{alignat*}{2}
a(b+c)+ad
&= (ab+ac)+ad
&\qquad& \text{distributivity} \\
&= ab+(ac+ad)
&& \text{associativity of addition} \\
&= ab+a(c+d)
&& \text{distributivity}
\end{alignat*}
\end{document}
Compile the file using your editor’s LaTeX-to-PDF workflow. In the resulting display, the three equals signs should share one vertical position, and the three explanations should share another. The second and third lines deliberately omit the original left-hand expression.
The amsmath user guide explicitly demonstrates line-by-line annotations and an alignat variant with a controlled gap. The package also loads amstext, which supplies \text for words inside mathematics. User’s Guide for the amsmath Package, §3.6, CTAN: amsmath
These examples were inspected against the documentation but were not compiled here because this environment has no LaTeX executable.
How do the alignment markers create the explanation column?
Read each row as four cells: the left-hand expression, the relation and its right-hand expression, a spacing cell, and the explanation. The argument {2} specifies two pairs of right-aligned and left-aligned columns. Put the first & before the equals sign. User’s Guide for the amsmath Package, §§3.1 and 3.6
Here is the first row as a labeled fragment from the complete document:
a(b+c)+ad % cell 1: original expression
&= (ab+ac)+ad % cell 2: relation and next expression
&\qquad % cell 3: explicit gap
& \text{distributivity} % cell 4: explanation
The continuation rows begin with &= because their first cell is empty. Their && after the algebra leaves the spacing cell empty and advances into the explanation cell. All three rows still follow the same four-cell structure.
In this example, the first row’s \qquad establishes the gap for the shared spacing column. The explanations therefore line up even though the algebraic expressions have different lengths. This is also why adding spaces to the source text is unnecessary: the column structure determines the layout.
Consider an alternative fragment:
&= ab+(ac+ad) \qquad \text{associativity of addition}
Here the explanation remains in the expression cell. Its position follows that particular expression’s width, so it does not establish a shared explanation column. Use this arrangement only when that following-text appearance is what you want.
For your own derivation, first replace the algebra while preserving every alignment marker. Then replace the explanatory words. Change the gap last, after checking the resulting PDF. This sequence makes it easier to identify whether a problem comes from the mathematics, the column structure, or the available width.
Which environment should you choose?
Choose the layout according to the numbering and spacing you need. The following decision aid combines the documented environment distinctions with practical recommendations for this task. User’s Guide for the amsmath Package, §§3.6–3.7
| Your intended result | Environment | Package |
|---|---|---|
| Aligned steps and explanations, with automatic spacing between column pairs | align or align* |
amsmath |
| Explicit control of the gap before explanations | alignat or alignat* |
amsmath |
| One equation number for the whole annotated derivation | equation containing alignedat |
amsmath |
| A compact inner alignment using automatic pair spacing | aligned inside a math environment |
amsmath |
| A short prose interruption between steps | \shortintertext inside a suitable alignment |
mathtools |
For the opening example, alignat* is a useful starting choice because the gap is visible in the source and the display needs no numbers. Use align* when you prefer its automatic spacing; remove {2} when making that change. The current implementation documents these as distinct environments and defines their starred forms separately. The amsmath package, §17.6
Give the whole derivation one number
For a single reference to the complete argument, use the following complete document. The outer equation supplies the number; the inner alignedat supplies the columns.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Let $a,b,c,d$ be elements of a ring.
\begin{equation}
\label{eq:regroup}
\begin{alignedat}{2}
a(b+c)+ad
&= (ab+ac)+ad
&\qquad& \text{distributivity} \\
&= ab+(ac+ad)
&& \text{associativity of addition} \\
&= ab+a(c+d)
&& \text{distributivity}
\end{alignedat}
\end{equation}
The regrouping is shown in \eqref{eq:regroup}.
\end{document}
Use alignedat inside this wrapper. Nesting the top-level alignat environment inside equation is an erroneous equation structure. The implementation documents both the inner environment and recovery from incorrect nesting. The amsmath package, §§17.4 and 17.6
If individual steps need numbers instead, use the unstarred alignat environment. Place a separate \label on each row you want to reference; \notag suppresses an unwanted row number. User’s Guide for the amsmath Package, §§3.1 and 3.11
What should you do when an explanation is too long?
Keep the side column for brief reasons such as “distributivity” or “definition.” When a reason becomes a sentence, an editorially useful choice is to move it between the steps. That gives the explanation room and keeps the algebra easy to scan.
The mathtools package loads amsmath and supplies \shortintertext, which interrupts an alignment with less vertical spacing than \intertext. Both preserve the alignment across the prose interruption. The mathtools package, §§2 and 3.5
\documentclass{article}
\usepackage{mathtools}
\begin{document}
Let $a,b,c,d$ be elements of a ring.
\begin{align*}
a(b+c)+ad &= (ab+ac)+ad \\
\shortintertext{Regroup the sum before factoring the last two terms.}
&= ab+(ac+ad) \\
&= ab+a(c+d)
\end{align*}
\end{document}
This alternative deliberately changes the presentation: one sentence explains the transition between rows, while the shared equals-sign alignment remains. For a larger prose interruption, \intertext is available through amsmath; the compact command shown above requires mathtools. The mathtools package, §3.5
How can you diagnose a misaligned result?
Compare your document with the smallest complete example before making spacing adjustments. The following checks target the structure used here; the implementation documents column-count errors and incorrect nesting. The amsmath package, §17
- Count the separators. Each algebra row in the opening example has three
&markers and uses{2}. - Check the explanation cell. Preserve
&&on continuation rows so the explanation stays in the fourth cell. - Check row endings. Keep
\\between steps, as shown in the complete examples. - Check the wrapper. Use standalone
alignat*, or usealignedatinsideequation. - Review the longest row. If it crowds the page, try a shorter explanation or move the sentence between steps before reducing the gap.
Treat the displayed algebra and its explanations as one layout. Start with the four-cell pattern, choose numbering according to how readers will reference the derivation, and reserve the side column for concise reasons. A longer explanation can then move between steps without obscuring the mathematical sequence.
Sources
- Getting LaTeX — The LaTeX Project.
- Amsmath — AMS mathematical facilities for LaTeX — CTAN.
- User’s Guide for the amsmath Package — American Mathematical Society and The LaTeX Project, via CTAN.
- The amsmath package — The LaTeX Project, via CTAN.
- The mathtools package — Morten Høgholm, Lars Madsen, and The LaTeX3 Project, via CTAN.