Aligning terms in equation and \underbrace

Viewed 35

I can get latex to do most of what I want, but not everything.

I am trying to write multiple equations where each term in each equation is aligned. On the last equation line, I'd like to put an \underbrace under the entire right-hand side of the equation. I can't seem to figure out how to do both at the same time.

\begin{array}{rlrrrrrrrrrrr}
    S &=& 1 &+& 2 &+& 3 &+& \cdots &+& 99 &+& 100 \\
    S &=& 100 &+& 99 &+& 98 &+& \cdots &+& 2 &+& 1 \\
    \hline
    S + S &=& 101 &+& 101 &+& 101 &+& \cdots &+& 101 &+& 101 
\end{array}
$$

\begin{alignat*}{0}
    S &= 1 + 2 + 3 + \cdots + 99 + 100 \\
    S &= 100 + 99 + 98 + \cdots + 2 + 1 \\
    \cline{2-1}
    S + S &= \underbrace{101 + 101 + 101 + \cdots + 101 + 101}_{\text{100 groups}} \\
\end{alignat*}

The top version aligns the terms in the sum to the right of the =, but if I try to use \underbrace underneath more than one array element in the last line, the compiler complains. If I wrap the whole array in an \underbrace, the brace spans the entire equation line, not just the right-hand side.

The bottom version puts the underbrace in the correct place, but I can't get the terms in each equation to line up the way I want.

Is there any way to convince LaTeX to let me do both at the same time? I've attached a picture to show what's going on, but Stack Overflow won't let me embed yet.

screenshot of LaTeX's layout of the code snippet

1 Answers

You can't use alignment inside \underbrace{}. You essentially have to overlap two things on top of each other, an under brace symbol with an empty space and the proper equation with the alignment.

I use saved box to measure the width of the equation just for a rule and used the same trick with the rule

enter image description here

\documentclass[a4paper,12pt]{article}
\usepackage{array}
\usepackage{mathtools}

\newsavebox\lasteq

\begin{document}
\sbox\lasteq{\ensuremath{101 + 101 + 101 + \cdots + 101 + 101}}

\[
  \begin{array}{rl *{11}{r}}
        S &=&   1 &+&  2 &+&    3 &+& \cdots &+& 99 &+& 100 \\
        S &=& 100 &+& 99 &+&   98 &+& \cdots &+&  2 &+&   1 \\
    \hline
    S + S &=& 101 &+& 101 &+& 101 &+& \cdots &+& 101 &+& 101
  \end{array}
\]

\begin{alignat*}{7}
  S &={}&   1 +{}&&  2 +{}&&  3 +{}&& \cdots +{}&& 99 +{}&& 100 \\
  S &={}&
      \rlap{\rule[-6pt]{\wd\lasteq}{0.4pt}}
      100 +{}&& 99 +{}&& 98 +{}&& \cdots +{}&&  2 +{}&&   1 \\
  S + S &={}&
      \mathrlap{\underbrace{\hphantom{\usebox\lasteq}}_{\textrm{100 groups}}}
      101 +{}&& 101 +{}&& 101 +{}&& \cdots +{}&& 101 +{}&& 101 \\
\end{alignat*}

\end{document}
Related