# How to get the spacing right with multiple underbraces within parentheses?

This question is a generalization of

How to nicely lay out a parentheses-enclosed overbrace/underbrace?

Now we have parentheses with some parts of the content, of different heights, having an underbrace. Example:

\documentclass{minimal}
\begin{document}
$\left( 1 \cdot 2 \cdot \frac{3}{4} \cdot 5^2 \right)$
$\left( \underbrace{1}_{x} \cdot 2 \cdot \underbrace{\frac{3}{4}}_{\int y} \cdot 5^2 \right)$
\end{document}


produces:

which is very ugly. How can I make the underbraces ignored for the purposes of sizing the parentheses, but count for the space to the next line of text? The solution must allow me to write something like

$\magicparens{% underbrace{1}_{x} \cdot 2 \cdot \underbrace{\frac{3}{4}}_{\int y} \cdot 5^2% }$


and have it work. No manually specifying any sizes or lengths.

Bonus points for a second version which make all the underbraces vertically aligned.

• Well, the easiest answer is “don’t use \left and \right for the parentheses”… Do you have some special reason for requiring auto-resizing parentheses? – GuM Oct 3 '17 at 17:51
• @GuM: But I want to, and need to. Why shouldn't I use them? – einpoklum - reinstate Monica Oct 3 '17 at 18:12
• because this gives better result (probably what you expected): $\biggl(1 \cdot 2 \cdot \frac{3}{4} \cdot 5^2 \biggr) \biggl(\underbrace{1}_{x} \cdot 2 \cdot \underbrace{\frac{3}{4}}_{\int y} \cdot 5^2 \biggr)$ – Zarko Oct 3 '17 at 18:13
• You also might want to throw in a {\textstyle\frac{3}{4}}. – John Kormylo Oct 3 '17 at 18:53
• @JohnKormylo: In my opinion, \tfrac is much too small when the rest of formulae is in \displaystyle. But maybe \mfracwould look fine. – Bernard Oct 3 '17 at 19:36

Here is a version of \magicparens that sets both \left( and \right) using a \overbrace- and \underbrace-free makeup:

\documentclass{article}

\usepackage{amsmath}

\newcommand{\noubob}[1]{{
\def\underbrace##1_##2{##1}% Remove subscript associated with \underbrace
\def\overbrace##1^##2{##1}% Remove superscript associated with \overbrace
#1}}
\newcommand{\magicparens}[1]{%
\left(\vphantom{\noubob{#1}}\right.\kern-\nulldelimiterspace
#1
\kern-\nulldelimiterspace\left.\vphantom{\noubob{#1}}\right)
}

\begin{document}

$\left( 1 \cdot 2 \cdot \frac{3}{4} \cdot 5^2 \right)$

$\left( \underbrace{1}_{x} \cdot 2 \cdot \underbrace{\frac{3}{4}}_{\int y} \cdot 5^2 \right)$

$\magicparens{ \underbrace{1}_{x} \cdot 2 \cdot \underbrace{\dfrac{3}{4}}_{\int y} \cdot 5^2% }$

\end{document}


Since $\underbrace{\frac{3}{4}}_{x}$ prints the \fraction in \displaystyle while $\frac{3}{4}$ sets it in \textstyle, I've added amsmath and forced a \dfrac.

Also, the redefinition of \underbrace/\overbrace inside \magicparens requires you to necessarily use _ and ^ with the respective definitions, even though \underbrace/\overbrace can naturally be used without them. Of course, one can improve on the redefinition and peek ahead to accommodate for those situations as well.

• No, this doesn't quite work: Your magicparens does not take the height of the \frac{3}{4} into account, thus producing parentheses which are too short. – einpoklum - reinstate Monica Oct 3 '17 at 22:43
• Also, I doubt this supports nested overbraces and underbraces. – einpoklum - reinstate Monica Oct 3 '17 at 22:43
• But +1 already. – einpoklum - reinstate Monica Oct 3 '17 at 22:44
• @einpoklum: \underbrace and \overbrace puts the inner \frac in \displaystyle even though it should not be in in-line math. So, if you want to possible force everything to be in \displaystyle, use \displaystyle #1 in the definition of \noubob or force the fraction to be set the way it should be in the output (say, by adding \usepackage{amsmath} and then \dfrac{3}{4}). – Werner Oct 3 '17 at 22:47
• @einpoklum: Have you tried it with nested \overbraces/\underbraces? – Werner Oct 3 '17 at 22:51