Formatting of align
and aligned
Environments
When writing a multiline expressions in LaTeX using align
or aligned
environments, use well-placed line breaks and alignment and proper spacing to improve readability.
The following example is an example of a poorly formatted equation:
\begin{align} f(x) &:= \sum (a + b + c) + \\ & \iiint f(x + y + z) dx\, dy\, dz \\ &- x + y + z + \sin\Big(x + y \\ &+ x^2 - y^2 \Big) \\ \end{align}Output:
The above snippet contains the following formatting errors:
-
In (\ref{eq:operator at end}), the addition symbol is placed at the end of the line, instead of after the line break. Although this is a matter of taste, I prefer placing plus and minus signs (and other binary operators) so that they are all aligned, allowing the reader to easily identify, for example, if all of the terms are added or if, instead, some terms are subtracted.
- In (\ref{eq:not aligned with rhs of equal sign})-(\ref{eq:line break in middle of parentheses}), the start of the line is aligned with the left side of the equal sign. Instead, the entire expression of the right-hand side of the equality should be aligned to the right of the equal sign.
To achieve this, replace
&:=
in the first line with:=&
. (Alternatively, you can use&\quad
in all of the subsequent lines, but this approach is more verbose and may result in worse alignment.) -
In (\ref{eq:plus sign with unary operator spacing}), the line of code starts with
&-
. In this expression, LaTeX interprets “-
” as a unary operator instead of a binary operator. Since LaTeX places space on both sides of a binary operator but not a unary operator, the minus sign will be displayed with insufficient space (the online display of this equation does not show a much different spacing, but in some LaTeX documents, the difference is dramatic). To force LaTeX to recognize-
or+
as a binary operator, place “{}
” on the left side, like&{}-
and&{}+
. - In the line break between (\ref{eq:plus sign with unary operator spacing}) and (\ref{eq:line break in middle of parentheses}), the break is placed in the middle of a parenthetical grouping.
Line breaks should be placed between the highest level of group possible.
When a break is placed inside parentheses, the start of the new line should align with the start of the parenthetical group. Among other reasons to avoid line breaks between parentheses is that
\left( ... \\ ... \right)
does not work to make the parentheses match the height of the contents—instead, you must manually set the sizes of the parentheses, using, e.g.,\Big( ... \\ ... \Big)
.
Fixing the above problems results in the following aligned equation:
\begin{align} f(x) :={} & \sum (a + b + c) \\ &{} + \iiint f(x + y + z) dx\, dy\, dz \\ &{} - x + y + z \\ &{} + \sin\left(x + y - x^2 - y^2 \right) \end{align}Output:
If the expression inside the parentheses of the \sin
function were longer, and needed to be placed on multiple lines, then the contents should be aligned using manual spacing via the \hspace
command:
\begin{align} f(x) :={} & \sum (a + b + c) \\ &{} + \iiint f(x + y + z) dx\, dy\, dz \\ &{} - x + y + z \\ &{} + \sin\big(x + y \\ &\hspace{3em} {} - x^2 - y^2 \big) \end{align}Output:
Alternatively, alignment can be achieved using \phantom
to insert space equal to the space needed for its argument if its argument was rendered (which it is not).
\begin{align} f(x) :={} & \sum (a + b + c) \\ &{} + \iiint f(x + y + z) dx\, dy\, dz \\ &{} - x + y + z \\ &{} + \sin\big(x + y \\ &{} \phantom{{} + \sin\big(} - x^2 - y^2 \big) % <- Copy a portion of the previous line in \phantom{} \end{align}Output: