# Multiline \underbrace in math mode

This refers to the following sophisticated solution by @David Carlisle for applying \underbrace to multiline expressions:

1. How can I still use \underbrace on a single-line expression? All I get from the common use of \underbrace with the proposed solution is

1. How can I get the brace under an expression that spans only two lines with the tip of the brace below the second line's part of the brace? All I get is

1. What is the most flexible approach to multiline '\underbrace' apart from obviously avoiding such notation when possible?

Here is (for convenience) David Carlisle's code with the problematic equations added:

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\def\underbracex#1#2{\mathop{\vtop{\m@th\ialign{##\crcr
$\hfil\displaystyle{#2}\hfil$\crcr
\noalign{\kern3\p@\nointerlineskip}%
#1\crcr\noalign{\kern3\p@}}}}\limits}

\def\underbracea{\underbracex\upbracefilla}

\def\upbracefilla{$\m@th \setbox\z@\hbox{$\braceld$}% \bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill \kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@$}

\def\underbraceb{\underbracex\upbracefillb}

\def\upbracefillb{$\m@th \setbox\z@\hbox{$\braceld$}% \vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@ \leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd \braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill \kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@$}

\def\underbracec{\underbracex\upbracefillc}

\def\upbracefillc{$\m@th \setbox\z@\hbox{$\braceld$}% \vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@ \leaders\vrule \@height\ht\z@ \@depth\z@\hfill \kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@$}

\def\underbraced{\underbracex\upbracefill}
\def\upbracefill{$\m@th \setbox\z@\hbox{$\braceld$}% \vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@ \leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru$}

\makeatother
\begin{document}

\begin{align}
\underbrace{a+b}_{=c} \\
\underbracea{d + e} \\
\underbraced{f+g}_{=h}
\end{align}

\begin{align*}
A ={}& B + \underbracea{C + D + E} \\
&\underbraceb{{} + F + G}_{=:M} \\
&\underbracec{{}+ H + I} \\
&\underbraced{{} + J + K} + L
\end{align*}

\end{document}


There is an error in the old answer: the last clause was supposed to be variant d but it was written with no final letter so redefined the standard \upbrace macro to only make half a brace which was not the intention. Fixed below.

here I add one more variant bd that is a combination of b that had the "spike" and d which is the extending right half of a brace.

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\def\underbracex#1#2{\mathop{\vtop{\m@th\ialign{##\crcr
$\hfil\displaystyle{#2}\hfil$\crcr
\noalign{\kern3\p@\nointerlineskip}%
#1\crcr\noalign{\kern3\p@}}}}\limits}

\def\underbracea{\underbracex\upbracefilla}

\def\upbracefilla{$\m@th \setbox\z@\hbox{$\braceld$}% \bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill \kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@$}

\def\underbraceb{\underbracex\upbracefillb}

\def\upbracefillb{$\m@th \setbox\z@\hbox{$\braceld$}% \vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@ \leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd \braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill \kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@$}

\def\underbracec{\underbracex\upbracefillc}

\def\upbracefillc{$\m@th \setbox\z@\hbox{$\braceld$}% \vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@ \leaders\vrule \@height\ht\z@ \@depth\z@\hfill \kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@$}

\def\underbraced{\underbracex\upbracefilld}
\def\upbracefilld{$\m@th \setbox\z@\hbox{$\braceld$}% \vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@ \leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru$}

\def\underbracebd{\underbracex\upbracefillbd}
\def\upbracefillbd{$\m@th \setbox\z@\hbox{$\braceld$}% \vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@\vrule \@width\p@\kern\p@ \bracerd\braceld \leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru$}

\makeatother
\begin{document}

\begin{align}
\underbrace{a+b}_{=c} \\
\underbracea{d + e} \\
\underbracebd{f+g}_{=h}
\end{align}

\begin{align*}
A ={}& B + \underbracea{C + D + E} \\
&\underbraceb{{} + F + G}_{=:M} \\
&\underbracec{{}+ H + I} \\
&\underbraced{{} + J + K} + L
\end{align*}

\end{document}