# Aligned underbraces between amsmath equations

I have an \amsmath aligned environment containing some equations aligned with &=. I want to be able to place underbraces between the equations in such a way that that both the contents and “point” of the brace can be easily specified, with the rest of the equation aligned/shifted accordingly. The spacing between the brace and the equations should be uniform. Here's a mockup of what I'd like:.

I need to be able to stack an arbitrary number of such equations/braces. The next line would be an &= 3 with an underbrace indicating that the 1+2 was reduced to the 3. Basic code for the equations with no braces is:

\documentclass[a4paper]{article}
\usepackage{amsmath}

\begin{document}
\begin{aligned} 1 + 1 + 1 &= 1 + 1 + 1 \\ &= 1 + 2 \end{aligned}
\end{document}


Here is an alternative view on the grouping, which might be of interest:

\documentclass{article}
\usepackage{mathtools,calc}
\begin{document}
%\setlength{\jot}{.5\jot} Adjust to bring the equations closer vertically \begin{aligned} 1 + 1 + 1 &= 1 + \underbrace{1 + 1} \\ &= \mathrlap{\underbrace{\phantom{1+\hspace{1.9em}}}}1 + \makebox[\widthof{1+1}]{2} \\ &= \makebox[\widthof{1+\hspace{1.9em}}]{3} \end{aligned}
\end{document}


The line-spacing within align-and-friends are defined in terms of \jot. So, you could consider adjusting it to suit your needs.

• Very nice. Is there a way to determine the value for the \hspace apart from trial and error? Apr 24, 2015 at 5:19
• @Texman: Yes. You can place stuff in boxes, measure them and then use that to determine the exact width of the boxes. It's a lot of work for very little reward, since you may have to measure numerous boxes for a single entry. If the structure is predictable, then one can wrap it in a macro. Something that might be arbitrary in nature could be difficult to automate via a macro.
– Werner
Apr 24, 2015 at 6:16

As long as we add not too many one-cipher numbers, we can use the effect, that all ciphers have the same width. It gives a little bit more general solution, then the one of the predecessor.

\documentclass[a4paper]{article}
\usepackage{amsmath}

\begin{document}

$\begin{array}{r@{{}={}}c} 1 + 1 + 1 +1& 1 + 1+ \underbrace{1 + 1} \\ &1+\underbrace{1 + 2}\\ &\underbrace{1+3}\\ &4 \end{array}$

\end{document}


As long as you are always grouping things on the right, you can make this work by making the right-hand side right-aligned (using an alignat environment), and then adding an appropriate amount of space on the right to get the alignment under the brace. This technique also works equally well for grouping on the left.

\documentclass[a4paper]{article}
\usepackage{amsmath}
\begin{document}\noindent
Grouping on the right:
\begin{alignat*}{2}
6!
&={} & 6 \times 5 \times 4 \times 3 \times \underbrace{2 \times 1}
\\&={} & 6 \times 5 \times 4 \times \underbrace{3 \times 2}      \mspace{16mu}
\\&={} & 6 \times 5 \times \underbrace{4 \times 6}               \mspace{32mu}
\\&={} & 6 \times \underbrace{5 \times 24}                       \mspace{44mu}
\\&={} & \underbrace{6 \times 120}                               \mspace{56mu}
\\&={} & 720                                                     \mspace{72mu}
\end{alignat*}
Grouping on the left:
\begin{align*}
6!
&=                 \underbrace{6 \times 5} \times 4 \times 3 \times 2 \times 1
\\&= \mspace{10mu} \underbrace{30 \times 4} \times 3 \times 2 \times 1
\\&= \mspace{20mu} \underbrace{120 \times 3} \times 2 \times 1
\\&= \mspace{35mu} \underbrace{360 \times 2} \times 1
\\&= \mspace{50mu} \underbrace{720 \times 1}
\\&= \mspace{65mu} 720
\end{align*}
\end{document}


You can use array instead of aligned if this this what you require:

    \documentclass[a4paper]{article}
\usepackage{amsmath}

\begin{document}
$$\begin{array}{rcc} 1 + 1 + 1 &= &1 + 1 + 1 \\ &= &\underbrace{1 + 2}\\ &=&3 \end{array}$$
\end{document}


• The spacing will not quite be right, but worse still, it doesn't allow the OP to underbrace and align only a part of the expression as described in the question. Apr 23, 2015 at 3:33
• Number of columns could always be increased here as necessary to get the exact output. Apr 23, 2015 at 3:38
• This doesn't really work in general if the things being grouped don't happen to have the right length for alignment to happen by accident. Apr 23, 2015 at 8:44