# Align different equations on different equal signs

I am using the \begin{align*} command to structure equations such that they align along the first equal sign. I want to be able to change from one equal sign to another, such that I centre equations a to w on the first equal sign, and then centre the z equation on the second equal sign. Attached is the output I'm getting. Any pointers?

\begin{align*}
a&=b\\
c&=d\\
e&=f+g+h+j=w\\
x&=y\\
z&=0\\
\end{align*} I would like to be able to align the first three equations on the first equal sign and the last two equations on the second equal sign like so:

a=b
c=d
e=f+g+h+j=w
x=y
z=0


Two variants:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{alignat*}{2}
a &= b\\
c &= d\\
e &= f+g+h &{}+j &= w \\
&        &   x &= y\\
&        &   z &= 0
\end{alignat*}

\begin{align*}
a &= b\\
c &= d\\
e &= f+g+h\begin{aligned}[t]{}+j &= w \\
x &= y\\
z &= 0
\end{aligned}
\end{align*}

\end{document}


The {} in front of + is needed to get the spacing right, because TeX doesn't consider + a unary symbol. • Umm, that {} in the second example gives more space than actually required. – AboAmmar Feb 9 '17 at 23:25
• @AboAmmar Really? – egreg Feb 9 '17 at 23:33
• Yes, the first one is exact but the second has more space between h and +. To make sure, add a third line e &= f+g+h+j=w \\% and compare. – AboAmmar Feb 9 '17 at 23:35
• @AboAmmar Not in my output. If you get more space, it's time to update your TeX distribution. Quite recently, amsmath has been updated not to add \, in this situation. Ensure to have version 2.16a, released 2016/11/05. – egreg Feb 9 '17 at 23:38
• @egreg excellent update to amsmath do you know where it came from? – David Carlisle Feb 9 '17 at 23:55

Like this? Using two nested aligned environments; the outer aligned deals with the first three =s and the inner one deals with the last three =s. The negative space of \! is a correction.

Edit

Thanks to @egreg, this negative space \! is no longer needed as long as you have the latest version of amsmath installed (version 2.16a, released 2016/11/05).

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{equation*}
\begin{aligned}
a &= b\\
c &= d\\
e &= \!\begin{aligned}[t]f+g+h+j
&= w\\
x&=y\\
z&=0
\end{aligned}
\end{aligned}
\end{equation*}

\end{document} \documentclass{article}

\usepackage{amsmath}

\begin{document}
ths
\begin{alignat*}{2}
a&=b\\
c&=d\\
e&=f+g+h+j&&=w\\
&        &x&=y\\
&        &z&=0\\
\end{alignat*}

or to get rid of the space
\begin{alignat*}{2}
a&=b\\
c&=d\\
e&=f+g+h+j&&=w\\
&        &\llap{$x$}&=y\\
&        &\llap{$z$}&=0\\
\end{alignat*}

\end{document}


With aligned and a horizontal correction:

\documentclass{article}
\usepackage[utf8]{inputenc}%
\usepackage{xcolor}%
\usepackage{mathtools, amssymb}
\begin{document}

\begin{align*}
a&=b\\
c&=d\\
e & =f+g+h+\mspace{-\medmuskip}\begin{aligned}[t] j & =w \\
x&=y\\
z&=0
\end{aligned}
\end{align*}

\end{document} • as noted in discussion under egreg's answer the negative space is only needed for older amsmath installations not a current one. – David Carlisle Feb 9 '17 at 23:57
• @David Carlisle: my version is 2.16a, and I observed a difference of 0.7 pt between the spacings of + g and of + j. – Bernard Feb 10 '17 at 0:24