I am trying to align a set of long equations, that are themselves align environments as most of them are spreading on multiple lines.

Currently I just have a sequence of align environments, with each equation inside in order to align the pieces of each equations. I am attaching a screenshot of the result:

Unaligned

What would like to get instead is something looking more like

Aligned

which is the same set of equations after going through the copyediting office of a journal and looks much better.

Here is a MWE. I would like all three equations to align on the equal sign.

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align}
a & =  b + c + d \nonumber \\
  & \qquad + e + f + g
\label{eq:1}
\end{align}
\begin{align}
k & = l + m + n + m + n + m + n \nonumber \\
  & \qquad + o + p + q
\label{eq:2}
\end{align}
\begin{equation}
r = s + t (u + v + w)
\label{eq:3}
\end{equation}
\end{document}
  • 1
    Welcome to TeX.sx! As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. – N.N. Feb 13 '12 at 19:57
  • It would be a lot easier if you provided the code in terms of a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Feb 13 '12 at 20:17
  • I think it's better to use split for single long equations rather than align. according to the document, split is for single long equations, align is for multiple equations. The numbering should be affected. – xuhdev Jan 15 '16 at 7:14
up vote 34 down vote accepted

without an actual example, here's how i interpret what you want.

output of example code

and here is the input:

\documentclass{article}
\usepackage{mathtools}
\begin{document}
This example shows \verb|aligned| equations within
an \verb|align| environment.
\begin{align}
  \phantom{i + j + k}
  &\begin{aligned}
    \mathllap{a} &= b + c + d\\
      &\qquad + e + f + g + x + y + z
  \end{aligned}\\
  &\begin{aligned}
    \mathllap{i + j + k} &= l + m + n\\
      &\qquad + o + p + q
  \end{aligned}
\end{align}
\end{document}

the longest left-hand element is inserted at the beginning as a \phantom and the lengths of the left-hand elements of the individual aligned segments are made "invisible" by lapping them to the left using \mathllap from the thmtools package.

the original answer was (correctly) noted to align the segments properly only when the left-hand sides had the same length. this modification overcomes that problem.

  • 1
    oops! forgot a couple of ampersands. updated version coming up. – barbara beeton Feb 13 '12 at 21:26
  • Ok now it works great. Thanks! – Thomas 122 Feb 13 '12 at 21:40
  • only works if the left hand sides have the same lengths? – user1834164 May 21 '13 at 11:45
  • @user1834164 -- you're correct about the lengths of the left-hand elements. but it can be salvaged. adding that ... – barbara beeton May 21 '13 at 12:22

You can also use the split environment inside the align environment, using an ampersand (&) where you want the alignment to take place. Here is a MWE:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align}
\begin{split}\label{eq:1}
    a ={}& b + c + d\\
         & + e + f + g
\end{split}\\
\begin{split}\label{eq:2}
    k ={}& l + m + n + m + n + m + n\\
         & + o + p + q
\end{split}\\
    r ={}& s + t (u + v + w)\label{eq:3}
\end{align}
\end{document}

Notice that the last equation is not inside a split environment, but still aligns with the rest, since it's still inside the align environment.

The output looks like this:

Output of a split environment inside an align environment

Note the empty groups ({}) before the ampersands. Without these, there would be no kerning applied between the equals signs and the character afterwards, because the alignment breaks the box. While the empty groups don't do anything themselves, in math mode the symbols before them add kerning as though the empty groups were ordinary characters. This enables TeX to choose the most appropriate spacing. If the ampersands were placed before the equal signs, the align environment would kern around the equal signs as it should with no such hassle, but then the addition sign of the split equation would lie uncomfortably far back, requiring some sort of manual tweaking of its own.

  • 1
    The nice thing about thing about this solution is that there is no need to mess around with \phantom or \mathllap. – cebewee Jul 9 '15 at 10:19
  • 2
    Wow! In fact, it seems that you don't even need the splits, replacing &= with ={}& does the trick! – Blaisorblade Aug 6 '15 at 14:24
  • When I try to do that, I get the error "Extra alignment tab has been changed to \cr." I get this error when I try to use more than one ampersand within split. Any ideas? – Konstantin Dec 16 '16 at 14:50
  • @Blaisorblade -- the reason for the splits is to properly apply the equation numbers to each group. yes, \notag could be used, but it doesn't give the desired output if the equation number should be centered on the group. – barbara beeton Jun 8 '17 at 12:44
  • I tried this technique and it worked when converting to PDF, but malfunctioned when converting to HTML using the mk4ht htlatex test.tex "xhtml,mathml" workflow. I assume it's a bug in the html converter. – Mutant Bob May 9 at 20:09

As an extension to barbara's answer, you could wrap only the right-hand side of your equations into aligned subenvironments. This allows you to align the equal signs of the separate equations independent of the size of left- or right-hand sides.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
This example shows \verb|aligned| equations within
an \verb|align| environment.
\begin{align}
  a &= \begin{aligned}[t]
      &b + c + d +\\
      &c + e + f + g + h + i
       \end{aligned}\\
  k &= \begin{aligned}[t]
      &l + m + n\\
      &+ o + p + q
       \end{aligned}
\end{align}
\end{document}

The plus sign on the second line of the second equation does not exactly match up because it's a mathbin symbol. Maybe someone with more TeX knowledge could comment on how to best fix that.

  • 1
    Thanks, that works too. The difference I see is that equation numbers are on the top line of each equation, whereas when the whole equation is in the 'aligned' environment the equation numbers are vertically centred. Is there a way to control that? – Thomas 122 Feb 13 '12 at 21:50
  • 2
    @eldering -- to get the matching spacing after the first plus sign in the last line, precede it by an empty group, {}. – barbara beeton Feb 13 '12 at 22:11

Here is an align-only version of your equations:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newcommand{\myvec}[1]{\hat{\mathbf{#1}}}% Vector notation
\begin{document}
\begin{align}
  f_{\textit{P},\textit{P}}\left(\myvec{n};\myvec{m}\right) &= \frac{\omega^2}{4\pi\rho\alpha^4} \textit{AF}\left(k_\alpha\left(\myvec{n}-\myvec{m}\right)\right) \nonumber \\
    &\mathrel{\phantom{=}} \times\left\{\left(\lambda+\mu\right)^2\eta_N+\left(\lambda+\mu\right)\mu\eta_N\left(\cos 2\phi+\cos 2\theta\right)\right. \nonumber \\
    &\mathrel{\phantom{=}} \left.\kern-\nulldelimiterspace +\;\mu^2\eta_N\cos 2\phi\cos 2\theta+\mu^2\eta_T\sin 2\phi\sin 2\theta\cos\varphi\vphantom{\left(\lambda\right)^2}\right\}, \\
  f_{\textit{P},\textit{SH}}\left(\myvec{n};\myvec{m},\myvec{q}\right) &= \frac{\omega^2}{4\pi\rho\alpha\beta^3} \textit{AF}\left(k_\alpha\myvec{n}-k_\beta\myvec{m}\right) \nonumber \\
    &\mathrel{\phantom{=}} \times\left(-\mu^2\eta_T\right)\sin 2\phi\cos\theta\sin\varphi, \\
  f_{\textit{P},\textit{SV}}\left(\myvec{n};\myvec{m},\myvec{q}\right) &= \frac{\omega^2}{4\pi\rho\alpha\beta^3} \textit{AF}\left(k_\alpha\myvec{n}-k_\beta\myvec{m}\right) \nonumber \\
    &\mathrel{\phantom{=}} \times\left\{\left(\lambda+\mu\right)\mu\eta_N\sin 2\theta+\mu^2\eta_N\cos 2\phi\sin 2\theta\right. \nonumber \\
    &\mathrel{\phantom{=}} \left.\kern-\nulldelimiterspace -\;\mu^2\eta_T\sin 2\phi\cos 2\theta\cos\varphi\right\},
\end{align}
\end{document}
​

Some of the adjustments include

  • Using \mathrel for proper spacing around hidden = (included via \phantom);
  • Some negative \nulldelimiter kerning around missing \left. delimiters (otherwise there would be additional spacing introduced between operator/operand);
  • Height adjustment for multi-line \left\{ and \right\} pairs.

As a common thread, it may be useful to peruse Herbert Voß' mathmode document.

  • Link to mathmode document broken? – Frenzy Li Apr 16 at 21:11
  • @FrenzyLi: Fixed; the document has been moved into an "obsolete" state. – Werner Apr 16 at 21:52

This is a way to accomplish this for small amounts of text by using the \intertext command.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
This example shows \verb|aligned| equations within
an \verb|align| environment.
\begin{align}
  \begin{aligned}
a &= b + c + d\\
  &\qquad + e + f + g
  \end{aligned}\\
  \begin{aligned}
k &= l + m + n + m + n + m + n\\
  &\qquad + o + p + q
  \end{aligned}
\end{align}

This example shows text and  equations within
an \verb|align| environment.
\begin{align}
a &= b + c + d\\
  &\qquad + e + f + g
\intertext{A small amount of text can go here with $x=2$ inline math
 and     $$\int_a^b f(x)\,dx=F(b)-F(a)$$ (even inline math). But not a lot 
of text. }
k &= l + m + n + m + n + m + n\\
  &\qquad + o + p + q
\end{align}

\end{document}
  • The relevant part is about \qquad, which I've used, but that doesn't work well enough. – Blaisorblade Aug 6 '15 at 15:40
  • (+1) Ah! :) I was looking for such a thing for a while. Many thanks. :) – H. R. Dec 13 '16 at 20:12
  • In case of paragraphs splitting the equations to be aligned, what is the best solution? – Diaa Jun 27 at 20:07

If instead of aligning the trailing equations you wish to right-justify them (similarly to the way the \multiline environment handles trailing equations), you can use the following trick, which I picked up from this answer by Ulrike Fischer.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
a & = b + c + d + e + f + g + h \nonumber \\
  & \hspace{7cm} + i + j + k \\
a & = b + c + d + e + f + g + h \nonumber \\
  & \omit\hfill ${} + i + j + k$
\end{align}
\end{document}

omit + hfill

\begin{align}
\ni Tdij (Ti,Tj,Sk,t) & = Tdij(Ti,Tj,Sk,t) \nonumber \\
 & Tddir (Ti,Tj,Sk,t) \bigoplus \nonumber \\
& Tdrecom (Ti,Tj,Sk,t) \bigoplus \nonumber \\
& Tdiv (Ti,Tj,Sk,t)
\label{eq:1}
\end{align}

will provide following output enter image description here

  • 4
    How does this address the original post in such a way that hasn't already been addressed by the other answers? – Werner Feb 20 '15 at 7:24

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.