# How to make underbrace label text wrap at brace width?

Is there a way to make underbrace label text automatically wrap at the underbraces width, preferably even with automatic syllabication?

Example where brace label text exceeds underbrace width:

$\underbrace{f(x) = a^2 + 2ab + b^2}_\text{This is some pretty long label for a rather short formula}$

• You'll have to do that manually by setting a fixed width... – Werner Jul 24 '15 at 18:18
• @Werner -- It doesn't have to be completely manual: see my answer ;) – Ruben Jul 24 '15 at 18:31

If you want to change the behaviour of \underbrace globally you could say

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\let\ams@underbrace=\underbrace
\def\underbrace#1_#2{%
\setbox0=\hbox{$\displaystyle#1$}%
\ams@underbrace{#1}_{\parbox[t]{\the\wd0}{#2}}%
}
\makeatother

\begin{document}
$\underbrace{f(x) = a^2 + 2ab + b^2}_{This is some pretty long label for a rather short formula}$
\end{document}


Anyways, you can also create your custom underbrace-macro by using the same argument parsing and nearly the same replacement (in this case there is no need to store the original macro first) -- i.e. \def\myunderbrace#1_#2{\setbox...\underbrace...} -- to limit the impact of this hack to where you want it to actually happen.

Now, The output can be imrpoved by adding a different alignment inside the parbox (Thanks to @barbarabeeton!), i.e.

...\parbox[t]{\the\wd0}{\centering#2}...% or
%  \parbox[t]{\the\wd0}{\raggedright#2}


which yields the following result:

(First case)

(Second case)

To be more flexible you could then add an optional argument to the \underbrace command that specifies the alignment of the underbrace label as \underbrace[c]{Some maths}_{Some text}, for instance. The code below allows the following alignment specifications:

• c -- centered
• l -- left
• r -- right
• f -- fuzzy

### Complete Code

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\let\ams@underbrace=\underbrace
\def\underbrace{\kernel@ifnextchar[{\underbrace@}{\underbrace@[l]}}% default value: l
\def\underbrace@[#1]#2_#3{%
\ifx#1c\relax
\let\ubr@align\centering%
\else
\ifx#1l\relax
\let\ubr@align\raggedright%
\else
\ifx#1r\relax
\let\ubr@align\raggedleft%
\else
\ifx#1f\relax
\let\ubr@align\relax%
\else
\message{#1' isn't a valid alignment specification for the underbrace command}%
\fi
\fi
\fi
\fi
\setbox0=\hbox{$\displaystyle#2$}%
\ams@underbrace{#2}_{\parbox[t]{\the\wd0}{\ubr@align#3}}%
}
\let\ubr@align\relax
\makeatother

\begin{document}
$\underbrace[c]{f(x) = a^2 + 2ab + b^2}_{This is some pretty long label for a rather short formula}$
\end{document}

• The next step would be to wrap that process into the original \underbrace macro. But, for that I need latex to \show me the original definition in the ams package bundle. Stay tuned... (Maybe I can't make it today as I expect a visitor tonight.) – Ruben Jul 24 '15 at 18:30
• Thanks for the quick quick fix. This solution already works for me (when I apply manuall syllabication). Staying tuned for your improved answer. Maybe someone also comes up with a suggestion on how to enable syllabication inside math mode. Working with a german text here where the longer words tend to destroy the blockset layout, example: \setbox0=\hbox{$\displaystyle\mathcal{P}(Q \times \Gamma \times \lbrace L,R \rbrace)$} $\underbrace{\mathcal{P}(Q \times \Gamma \times \lbrace L,R \rbrace)}_\text{\parbox{\the\wd0}{Potenzmenge von Folgezuständen Wahlmöglichkeiten}}$ – norritt Jul 24 '15 at 19:02
• spacing would look better if \centering or \raggedright were used in the \parbox – barbara beeton Jul 25 '15 at 10:05
• @norritt -- Your other example looks ok to me with the alternative alignment inside the parbox. What do you think? – Ruben Jul 25 '15 at 11:54
• Hey Ruben, yes the macro you wrote works like a charm - also for the alternative example, thanks a lot for your help! – norritt Jul 30 '15 at 15:25

In this code I define a \parunderbrace command, with two arguments, which adjusts automatically to the width of the formula:

\documentclass{article}
\usepackage{mathtools}
\usepackage{ragged2e}
\newlength\ubwidth
\newcommand\parunderbrace[2]{\settowidth\ubwidth{$#1$}\underbrace{#1}_{\parbox{\ubwidth}{\scriptsize\RaggedRight#2}}}

\begin{document}

$\parunderbrace{f(x) = a^2 + 2ab + b^2}{This is some pretty long label for a rather short formula}$

\end{document}
`