121

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}
4
  • 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, 2012 at 19:57
  • 1
    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. Feb 13, 2012 at 20:17
  • 1
  • 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, 2016 at 7:14

8 Answers 8

75

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 mathtools 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.

5
  • 5
    oops! forgot a couple of ampersands. updated version coming up. Feb 13, 2012 at 21:26
  • 1
    only works if the left hand sides have the same lengths? May 21, 2013 at 11:45
  • 1
    @user1834164 -- you're correct about the lengths of the left-hand elements. but it can be salvaged. adding that ... May 21, 2013 at 12:22
  • 1
    why do we need \phantom{i + j + k}.. I could not understand.. Apr 28, 2019 at 5:01
  • 2
    @PraphullaKoushik -- the \phantom is needed so that the width of the left-hand side of the group of equations is included in the width of the whole group, so that the group will be centered. Apr 28, 2019 at 20:20
95

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.

5
  • 5
    The nice thing about thing about this solution is that there is no need to mess around with \phantom or \mathllap.
    – cebewee
    Jul 9, 2015 at 10:19
  • 4
    Wow! In fact, it seems that you don't even need the splits, replacing &= with ={}& does the trick! Aug 6, 2015 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, 2016 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. Jun 8, 2017 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, 2018 at 20:09
30

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.

2
  • 2
    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, 2012 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, {}. Feb 13, 2012 at 22:11
27

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.

0
12

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}
3
  • The relevant part is about \qquad, which I've used, but that doesn't work well enough. Aug 6, 2015 at 15:40
  • (+1) Ah! :) I was looking for such a thing for a while. Many thanks. :) Dec 13, 2016 at 20:12
  • In case of paragraphs splitting the equations to be aligned, what is the best solution?
    – Diaa
    Jun 27, 2018 at 20:07
7

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

1
\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

1
  • 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, 2015 at 7:24
1

This answer works when you use the fleqn package. The example below defines two LaTeX macros \mymidline and \mylastline. Both macros essentially expand to their first argument within align* environments. The \mymidline macro centers it and the \mylastline macro right-aligns it. Thereby, the width of the stuff in the second argument is subtracted from the available "display width". For align* environments that should just be the (longest) left-hand side of the equation. The example below shows how you can do that most efficiently with a macro \LHS.

\documentclass{article}
\usepackage[DIV15]{typearea}
\usepackage{amsmath,amsfonts}
\usepackage{fleqn}
\usepackage{ulem}
\makeatletter
\newdimen\@tzadima
\newdimen\@tzadimb
\newbox\@tzaboxa
\def\mylinemeasures#1#2{%
  \@tzadima\displaywidth%
  \advance\@tzadima-\tagwidth@%
  \advance\@tzadima-\alignsep@%
  \setbox\@tzaboxa\hbox{$\displaystyle#1$}%
  \@tzadimb\wd\@tzaboxa%
  \advance\@tzadima-\@tzadimb%
  \setbox\@tzaboxa\hbox{$\displaystyle#2$}%
  \@tzadimb\wd\@tzaboxa%
  \advance\@tzadima-\@tzadimb%
}
\def\mymidline#1#2{%
  \mylinemeasures{#1}{#2}%
  \divide\@tzadima2%
  \hbox to \@tzadima{}#1\notag
}
\def\mylastline#1#2{%
  \mylinemeasures{#1}{#2}%
  \hbox to \@tzadima{}#1%
}
\makeatother
\begin{document}
\begin{align*}
  \gdef\LHS{(L\cdot R)^{(i)}[i+1:n,i+1:n]}\LHS
  &= \underbrace{L^{(i-1)}[i+1:n,1:i-1]\cdot R^{(i-1)}[1:i-1,i+1:n]}_{\text{untouched}}+\\
  &\mymidline{+ L^{(i)}[i+1:n,i] \underbrace{R^{(i)}[i,i+1:n]}_{\text{untouched pivot row}}+}\LHS\\
  &\mylastline{+ \underbrace{L^{(i)}[i+1:n,i+1:n]}_{=1_{n-i-1}}\cdot R^{(i)}[i+1:n,i+1:n]}\LHS\\
  &= L^{(i-1)}[i+1:n,1:i-1]\cdot R^{(i-1)}[1:i-1,i+1:n]
    +\\
  &\mymidline{+ \uwave{L^{(i)}[i+1:n,i]\cdot R^{(i-1)}[i,i+1:n]}+}\LHS\\
  &\mylastline{+R^{(i-1)}[i+1:n,i+1:n]\uwave{\strut- L^{(i)}[i+1:n,i]\cdot R^{(i-1)}[i,i+1:n]}}\LHS\\
  &=L^{(i-1)}[i+1:n,1:i-1]\cdot R^{(i-1)}[1:i-1,i+1:n] +\\
  &\mymidline{+ \underbrace{L^{(i-1)}[i+1:n,i]}_{=0}\cdot R^{(i-1)}[i,i+1:n] +}\LHS\\
  &\mylastline{+ \underbrace{L^{(i-1)}[i+1:n,i+1:n]}_{=1_{n-i}}\cdot R^{(i-1)}[i+1:n,i+1:n]}\LHS\\
  &= A[i+1:n,i+1:n].
\end{align*}
\end{document}

Multline like formatting within align*

If you have the numbered version align* you should also consider the width of the equation label and the label separator in the second argument of \mymidline and \mylastline. I didn't find a predefined measure for the label width. After some tests it turned out that \quad\quad(1) is an appropriate placeholder for the label.

\documentclass{article}
\usepackage[DIV15]{typearea}
\usepackage{amsmath,amsfonts}
\usepackage{fleqn}
\usepackage{ulem}
\makeatletter
\newdimen\@tzadima
\newdimen\@tzadimb
\newbox\@tzaboxa
\def\mylinemeasures#1#2{%
  \@tzadima\displaywidth%
  \advance\@tzadima-\tagwidth@%
  \advance\@tzadima-\alignsep@%
  \setbox\@tzaboxa\hbox{$\displaystyle#1$}%
  \@tzadimb\wd\@tzaboxa%
  \advance\@tzadima-\@tzadimb%
  \setbox\@tzaboxa\hbox{$\displaystyle#2$}%
  \@tzadimb\wd\@tzaboxa%
  \advance\@tzadima-\@tzadimb%
}
\def\mymidline#1#2{%
  \mylinemeasures{#1}{#2}%
  \divide\@tzadima2%
  \hbox to \@tzadima{}#1\notag
}
\def\mylastline#1#2{%
  \mylinemeasures{#1}{#2}%
  \hbox to \@tzadima{}#1%
}
\makeatother
\begin{document}
\begin{align}
  \gdef\LHS{(L\cdot R)^{(i)}[i+1:n,i+1:n]}\LHS
  &= \underbrace{L^{(i-1)}[i+1:n,1:i-1]\cdot R^{(i-1)}[1:i-1,i+1:n]}_{\text{untouched}}+\notag\\
  &\mymidline{+ L^{(i)}[i+1:n,i] \underbrace{R^{(i)}[i,i+1:n]}_{\text{untouched pivot row}}+}\LHS\\
  &\mylastline{+ \underbrace{L^{(i)}[i+1:n,i+1:n]}_{=1_{n-i-1}}\cdot R^{(i)}[i+1:n,i+1:n]}{\LHS\quad\quad(1)}\\
  &= L^{(i-1)}[i+1:n,1:i-1]\cdot R^{(i-1)}[1:i-1,i+1:n] +\notag\\
  &\mymidline{+ \uwave{L^{(i)}[i+1:n,i]\cdot R^{(i-1)}[i,i+1:n]}+}{(L\cdot R)^{(i)}[i+1:n,i+1:n]}\\
  &\mylastline{+R^{(i-1)}[i+1:n,i+1:n]\uwave{\strut- L^{(i)}[i+1:n,i]\cdot R^{(i-1)}[i,i+1:n]}}{\LHS\quad\quad(1)}\\
  &=L^{(i-1)}[i+1:n,1:i-1]\cdot R^{(i-1)}[1:i-1,i+1:n] +\notag\\
  &\mymidline{+ \underbrace{L^{(i-1)}[i+1:n,i]}_{=0}\cdot R^{(i-1)}[i,i+1:n] +}{(L\cdot R)^{(i)}[i+1:n,i+1:n]}\\
  &\mylastline{+ \underbrace{L^{(i-1)}[i+1:n,i+1:n]}_{=1_{n-i}}\cdot R^{(i-1)}[i+1:n,i+1:n]}{\LHS\quad\quad(1)}\\
  &= A[i+1:n,i+1:n].
\end{align}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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