9

The following output seems to be difficult to look up an item in the upper sequence with its corresponding item in the lower sequence as the commas are not vertically aligned.

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{amsmath}
\begin{document}
\abovedisplayskip=0pt\relax
\begin{align*}
\{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{align*}
\end{document}

enter image description here

I want to align each comma in the upper and lower sequence. How to do it elegantly (that consumes less keystrokes)?

Moriambar
  • 11,466

4 Answers4

14

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}
10

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

Qrrbrbirlbel
  • 119,821
7

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

egreg
  • 1,121,712
4

Using the (experimental) tabstackengine package given at Measuring align

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{amsmath}
\usepackage{tabstackengine}
\stackMath
\begin{document}
\renewcommand\stackalignment{l}
\tabbedstackunder{%
\{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{document}

enter image description here

To put all 3 alignment choices into a single macro, with the inter-equation gap set to 10pt, and the inter-line gap set to 3pt:

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{amsmath}
\usepackage{tabstackengine}
\stackMath
\begin{document}
\setstackgap{S}{10pt}
\renewcommand\stackalignment{l}
\Shortstack{
\tabbedstackunder[3pt]{%
\{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\}}
\\
\renewcommand\stackalignment{c}
\tabbedstackunder[3pt]{%
\{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\}}
\\
\renewcommand\stackalignment{r}
\tabbedstackunder[3pt]{%
\{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{document}

enter image description here