Use amsmath’s alignat environment to give corresponding terms shared alignment columns across successive simplification steps. This guide shows how to build that layout in a LaTeX document on a Mac, including steps where a term disappears.
Choose the alignment your derivation needs
First decide what the reader should follow vertically: the equals signs, the starts of corresponding terms, or individual operators. These are different layout choices. For the worked example below, the goal is to keep the quadratic, linear and constant parts in three stable positions while simplifying an expression.
The LaTeX Project directs readers to the User’s Guide for the amsmath Package for mathematical typesetting. Its documented environments provide the following choices. LaTeX core documentation, amsmath user guide, Sections 3.3–3.7.
| Desired layout | Environment | Where it belongs |
|---|---|---|
| Align a simple chain at its equals signs | align or align* |
Directly in the document body |
| Align several corresponding term groups with explicit spacing | alignat or alignat* |
Directly in the document body |
| Put the term-aligned derivation under one equation number | alignedat inside equation |
Inside the enclosing math environment |
| Break one equation at a single alignment position | split inside equation |
Inside the enclosing display |
| Break a long equation without aligning corresponding terms | multline |
Directly in the document body |
For this task, start with alignat*. Its column pairs let you control the space between groups. By comparison, align supplies spacing between separate equation groups, which can make a term-by-term derivation look unnecessarily spread out. The star suppresses equation numbering. amsmath user guide, Section 3.6, Learn LaTeX: Mathematics.
Build a complete term-aligned simplification
The following synthetic example collects like powers of x, then abbreviates their coefficients. All letters denote ordinary commuting scalar quantities. Define A=a+b, B=c+d and C=e+f; the last row is a change of notation after the collection step.
Save this complete document as term-alignment.tex. The required package is loaded before \begin{document}.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Let $A=a+b$, $B=c+d$, and $C=e+f$.
Then
\begin{alignat*}{3}
P(x)&=a x^2+b x^2 &&{}+c x+d x &&{}+e+f\\
&=(a+b)x^2 &&{}+(c+d)x &&{}+e+f\\
&=A x^2 &&{}+B x &&{}+C
\end{alignat*}
\end{document}
Read the layout as three vertical groups. The first contains the equals sign and quadratic part. The second starts with the plus sign before the linear part. The third starts with the plus sign before the constant part. Collecting coefficients changes each group’s width, but its selected alignment position stays shared across rows.
This is an application of the documented column mechanism, rather than automatic recognition of matching algebraic terms. You assign each expression to its intended column; the alignment environment arranges those columns. The implementation uses alternating right-aligned and left-aligned fields. The amsmath package, Section 17.
Understand the ampersands before editing
In alignat*, the argument {3} declares three pairs of columns. A fully specified row therefore has six fields and five & separators. The manual’s counting rule is to add one to the maximum number of ampersands in a row and divide by two. amsmath user guide, Section 3.6.
For the first row, the field assignment is:
| Field | Contents | Purpose in this example |
|---|---|---|
| 1 | P(x) |
Right-aligned left-hand side |
| 2 | =a x^2+b x^2 |
Left-aligned equality and quadratic group |
| 3 | Empty | Unused right-aligned field |
| 4 | {}+c x+d x |
Left-aligned linear group |
| 5 | Empty | Unused right-aligned field |
| 6 | {}+e+f |
Left-aligned constant group |
The && sequences leave fields 3 and 5 empty. On later rows, the initial & leaves field 1 empty, allowing the derivation to continue with another equals sign. The \\ command ends a row.
The empty group in {}+ provides a left operand for spacing a binary plus at a field boundary; it prints no character. Keep that form when adapting these examples. The package’s alignment preamble also uses an empty group at the start of its left-aligned fields. The amsmath package, alignment preamble.
Ordinary source spaces make the code easier to read, but they do not establish horizontal positions in math mode. Alignment comes from the field structure. Learn LaTeX: Mathematics.
Compile the example on a Mac
You need a working TeX distribution before compiling. The TeXShop installation documentation describes MacTeX and BasicTeX installation and identifies /Library/TeX/texbin as the shared location through which applications find the active distribution’s programs. TeXShop: Installing TeX Live.
For a direct compilation workflow, save the file as plain text on your Desktop, open Terminal, and run these two commands in order:
cd "$HOME/Desktop"
/Library/TeX/texbin/pdflatex -interaction=nonstopmode -halt-on-error term-alignment.tex
Open the resulting term-alignment.pdf and inspect the three term groups. These instructions assume that your Desktop is available at $HOME/Desktop and the file has the exact name shown. If compilation stops, address the reported error before judging the PDF; an older PDF may still be present from an earlier successful run.
Preserve the columns when a term disappears
Cancellation is where a stable layout becomes especially useful. Suppose the linear coefficient is c-c. You may want the final constant to stay underneath the earlier constant while the linear position becomes empty.
The following complete document keeps the same six-field structure throughout. The middle row explicitly shows the zero coefficient; the final row removes that term without moving the constant into its place.
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Cancelling the linear coefficient gives
\begin{equation}
\begin{alignedat}{3}
Q(x)&=(a+b)x^2 &&{}+(c-c)x &&{}+d\\
&=(a+b)x^2 &&{}+0x &&{}+d\\
&=(a+b)x^2 && &&{}+d
\end{alignedat}
\end{equation}
\end{document}
In the last row, keep both && sequences after the quadratic expression. They preserve the intervening empty fields. Removing one sequence would place the constant in an earlier group and change what the vertical arrangement communicates.
This example also demonstrates the supported way to give the whole derivation one number: put alignedat inside equation. The inner environment occupies its content’s natural width and requires an enclosing math environment. amsmath user guide, Section 3.7.
An empty position is useful while the reader is tracing a cancellation. For a standalone final formula elsewhere in the document, write the compact expression (a+b)x^2+d. Decide whether the current display’s purpose is to show correspondence or to present the finished result.
Diagnose spacing and alignment problems
Check the structure before adjusting individual distances. The following sequence is designed for the two worked layouts above.
- Confirm the package is loaded. Keep
\usepackage{amsmath}in the preamble. - Check the outer environment. Use
alignat*directly in the document body. Usealignedatinsideequationor another appropriate math environment. - Count the separators. Each example row has five
&characters for{3}. Check a newly inserted row against that pattern. - Check group membership. If only one term moves sideways, compare its field position with the preceding row.
- Inspect the mathematical content. A changed sign, coefficient or exponent is an algebraic change, even when the columns line up.
Too many alignment separators can produce the package error Extra & on this line. Incorrectly nesting a display such as align inside equation also triggers an equation-structure error; use an inner building block such as alignedat for that purpose. The amsmath package, Section 17.
If one column seems too wide, inspect its longest expression. In the first example, a x^2+b x^2 establishes more width than A x^2. That extra width supports the intended correspondence. Before trying to compress it, ask whether all three groups still need separate alignment positions.
Also identify exactly what looks misaligned. These examples align the starts of the term groups, including their leading plus signs. They do not separately align every coefficient, variable and exponent within those groups. If your reader only needs to follow successive equalities, a simpler single-anchor layout may be easier to read.
Add explanations without rebuilding the derivation
When a transformation needs a sentence of explanation, mathtools provides \shortintertext, which inserts text between alignment rows while retaining the alignment points. It loads amsmath, so change the preamble to \usepackage{mathtools}. The mathtools package, Sections 2 and 3.5.
For example, this labeled fragment replaces the alignment block in the first complete document after that package change:
\begin{alignat*}{3}
P(x)&=a x^2+b x^2 &&{}+c x+d x &&{}+e+f\\
\shortintertext{Collect coefficients of matching powers of $x$.}
&=(a+b)x^2 &&{}+(c+d)x &&{}+e+f
\end{alignat*}
Use the sentence to explain the operation the reader should notice. Here, the shared columns already show where each group goes; the inserted text explains why its form changes.
For your own derivation, sketch the intended term groups first, assign each group a stable field, and preserve those fields through cancellation. Compile after adding each new transformation. That workflow makes the correspondence deliberate and keeps the source understandable when you return to edit it.
Sources
- LaTeX Documentation — The LaTeX Project.
- User’s Guide for the amsmath Package — American Mathematical Society and The LaTeX Project.
- The amsmath package — The LaTeX Project.
- Mathematics — Learn LaTeX.
- Installing TeX Live — TeXShop.
- The mathtools package — Morten Høgholm, Lars Madsen and The LaTeX3 Project.