# How to align equations by = and terms by index?

I need to write something roughly like this.

S = a(1) + a(2) + a(3) + a(4) + a(5) + a(6) + a(7) + a(8) + ...
+        b(2)        + b(4)        + b(6)        + b(8) + ...
+               c(3)               + c(6)               + ...
+                      d(4)                      + d(8) + ...
+ ...

S =

S =


That is several equations aligned at =, I am used to doing this by using align and &=. Different this time is that I want to align the terms by index, i.e. a(4), b(4) and d(4) aligned in the same 'column'.

Do you have a suggestion on how to cleanly achieve such a lay-out?

One has to align the operation and relation symbols; also the letters need not have the same width, so in order to get the (n) aligned we need right alignment for those entries.

An array with 19 columns is what you need: the odd columns are right aligned, the even ones are center aligned (for the symbols), but with empty atoms around the item so TeX will provide the correct spacing by itself.

The syntax >{{}}c<{{}} means that the column is center aligned, but each item in the column is preceded by {} and followed by {}. So you get, say, ${}+{}$ which is what's wanted to get the automatic spacing.

The separation between columns is (locally) set to zero, so only those automatic spaces will be inserted.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}

\begin{document}

$\renewcommand*{\arraystretch}{1.5} \setlength{\arraycolsep}{0pt} \begin{array}{ r *{9}{ >{{}}c<{{}} r } } S &=& a(1) &+& a(2) &+& a(3) &+& a(4) &+& a(5) &+& a(6) &+& a(7) &+& a(8) &+& \dotsb \\ & & &+& b(2) & & &+& b(4) & & &+& b(6) & & &+& b(8) &+& \dotsb \\ & & & & &+& c(3) & & & & &+& c(6) & & & & &+& \dotsb \\ & & & & & & &+& d(4) & & & & & & &+& d(8) &+& \dotsb \\ S &=& \\ S &=& \\ \end{array}$

\end{document}


• Thank you, this was a great help! Jul 1 '21 at 9:20

I have to agree with campa: array does require far less typing.

But just for documentation: you can do the same using AMS's alignat environment. (alignat inserts no spaces, so you will have to add spacing yourself)

\documentclass{amsart}
\usepackage{amsmath}
\begin{document}
\begin{alignat*}{10}
S =& a(1) &+& a(2) &+& a(3) &+& a(4) &+& a(5)& +& a(6)& +& a(7) &+& a(8) &+ &\ldots\\
+&    &&    b(2)  &&      &+& b(4)    &&    &+& b(6)&&        &+& b(8) &+& \ldots \\
+&    &&      &&     c(3) &&      &&        &+& c(6)               && &&  &+& \dots\\
+&    &&      &&      &&      d(4)    &&  &&  &&              &+& d(8) &+& \dots\\
+& ...
\end{alignat*}
\end{document}


Edit As pointed out in the comment, the spacing above is rather atrocious. This was mostly due to the need to align the + sign for each row directly underneath the equals sign and NOT with the terms b(2), c(3) etc. (this can also be solved with some phantoms, but seemed more work than it is worth to me).

If one is happy with grouping the operators with the terms and not demand that the + signs line up on the left, then the following is a more natural way (at least to me [and to commenter below]) of typing and formatting the same information:

\begin{alignat*}{10}
S &= a(1) &&+ a(2) &&+ a(3) &&+ a(4) &&+ a(5)&& + a(6)&& + a(7) &&+ a(8) &&+ \ldots\\
&    && +   b(2)  &&      &&+ b(4)    &&    &&+ b(6)&&        &&+ b(8) && + \ldots \\
&    &&      &&    + c(3) &&      &&        &&+ c(6)               && &&  &&+ \dots\\
&    &&      &&      && +     d(4)    &&  &&  &&              && + d(8) && + \dots\\
& && && && &&+ \ldots
\end{alignat*}


• This looks very clean indeed. Jun 30 '21 at 13:29
• This messes up the spacing around the operators. (There re no spaces around the = signs and the spacing around the = is not even symmetric. It can be fixed by using &+ (or &&+ instead) +& or &+&. Jun 30 '21 at 13:37
• @MarcelKrüger: indeed. One small caveat with &+ however is that in the second-fourth lines one would have to force LaTeX to treat + as binary instead of as unary, since there is no following term based on the way the OP wanted the placement of the + signs. Jun 30 '21 at 15:39
• (Had OP written the desired output the way as shown in egreg's answer I would've done as you suggested... the above is just me being lazy.) Jun 30 '21 at 15:40