How to vertically align each comma in the following 2 identical sequences?

I understand that this does not answer your question in the strict sense, but in my opinion the output is clearer:

set

\documentclass[border=12pt,varwidth]{standalone}
\usepackage{amsmath}

\begin{document}
\begin{equation}
     \{a_n\} = \{\overbrace{1^2}^1,
                 \overbrace{1^2+2^2}^5,
                 \overbrace{1^2 +2^2 +3^2}^{14},
                 \overbrace{1^2 +2^2 +3^2 +4^2}^{30},\dotsc \}
\end{equation}
\end{document}

A few ideas with alignedat (since this is only one formula I am using equation) and one with array for centered aligning which otherwise would need more work to be able to be usable in one of the *align* environments.

Code

\documentclass[border=12pt,varwidth]{standalone}
\usepackage{amsmath,array}
\begin{document}

\begin{equation}
\begin{alignedat}{5}
\{a_n\} &= \{1^2 &&, 1^2 +2^2 &&, 1^2 +2^2 +3^2 &&, 1^2 +2^2 +3^2 +4^2 &&, \dotsc\} \\
        &= \{1   &&, 5        &&, 14            &&, 30                 &&, \dotsc\}
\end{alignedat}
\end{equation}

\begin{equation}
\renewcommand*\c{,{}}%
\begin{alignedat}{5}
\{a_n\} &= \{1^2 \c && 1^2 +2^2 \c && 1^2 +2^2 +3^2 \c && 1^2 +2^2 +3^2 +4^2 \c & \dotsc\} \\
        &= \{1   \c && 5        \c && 14            \c && 30                 \c & \dotsc\}
\end{alignedat}
\end{equation}


\begin{equation}
\renewcommand*\c{,{}}%
\begin{alignedat}{6}
\{a_n\} &= \{& 1^2 \c && 1^2 +2^2 \c && 1^2 +2^2 +3^2 \c && 1^2 +2^2 +3^2 +4^2 \c && \dotsc\} \\
        &= \{& 1   \c && 5        \c && 14            \c && 30                 \c && \dotsc\}
\end{alignedat}
\end{equation}

\begin{equation}
 \begin{array}{@{}r@{} *4{c@{,{}}} l@{}}
     \{a_n\} = \{ & 1^2 & 1^2 +2^2 & 1^2 +2^2 +3^2 & 1^2 +2^2 +3^2 +4^2 & \dotsc\} \\[\jot]
             = \{ & 1   & 5        & 14            & 30                 & \dotsc\}
  \end{array}
\end{equation}
\end{document}

Output

enter image description here


A contest? ;-)

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand{\alignedset}[2][l]{%
  \vcenter{
    \everymath\expandafter{\the\everymath\displaystyle}
    \m@th\let\\\@tabularcr\openup1\jot
    \ialign{\hfil$##$&${}##{}$\hfil&&
            \if#1l\hfilneg\fi\hfil
            $##{}$%
            \if#1r\hfilneg\fi\hfil
            \cr#2\crcr}
    }%
}
\makeatother
\begin{document}
\begin{equation}
\alignedset{
  \{a_n\} &= \{ & 1^2, & 1^2+2^2, & 1^2+2^2+3^2, & 1^2+2^2+3^2+4^2, & \dotsc \} \\
          &= \{ & 1,   & 5,       & 14,          & 30,              & \dotsc\}
}
\end{equation}
\begin{equation}
\alignedset[c]{
  \{a_n\} &= \{ & 1^2, & 1^2+2^2, & 1^2+2^2+3^2, & 1^2+2^2+3^2+4^2, & \dotsc \} \\
          &= \{ & 1,   & 5,       & 14,          & 30,              & \dotsc\}
}
\end{equation}
\begin{equation}
\alignedset[r]{
  \{a_n\} &= \{ & 1^2, & 1^2+2^2, & 1^2+2^2+3^2, & 1^2+2^2+3^2+4^2, & \dotsc \} \\
          &= \{ & 1,   & 5,       & 14,          & 30,              & \dotsc\}
}
\end{equation}
\end{document}

enter image description here

Tags:

Align

Amsmath