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

This:

\documentclass{minimal}
\begin{document}
$\left( 1 \right)$
$\left( \underbrace{1}_{456} \right)$
\end{document}


produces:

which is very ugly. There are several different things one could imagine would happen:

• The underbrace is totally ignored for sizing the parentheses (but of course it counts for spacing to the next line of text).
• The underbrace + actual line fill out the parentheses all the way, i.e. their center drops below the center of the no-underbrace parentheses and the line.
• The parentheses take the underbrace into account just partially.

I'm assuming the first would be simplest, so - how do I make that happen? Using phantoming perhaps?

Also, if you have another alternative/idiom for how to handle this situation 0 that would be appreciated.

Note: This question is about the simple case where the entire contents of the parentheses is covered by a single underbrace.

If you have to underbrace the whole subformula between \left and \right:

\documentclass{article}
\usepackage{amsmath}

\newcommand\parensunderbrace[2]{%
\vphantom{\underbrace{#1}_{#2}}%
\left(%
\vphantom{#1}%
\,\smash{\underbrace{#1}_{#2}}\,%
\right)%
}
\makeatletter
\newcommand{\slowparensunderbrace}[2]{%
\vphantom{\underbrace{#1}_{#2}}%
\mathpalette\slow@parensunderbrace{{#1}{#2}}%
}
\newcommand\slow@parensunderbrace[2]{%
\slow@@parensunderbrace#1#2%
}
\newcommand\slow@@parensunderbrace[3]{%
\left(
\sbox\z@{$\m@th#1#2$}%
\vrule height 1.1\ht\z@ depth \z@ width \z@
\,\smash{\underbrace{#2}_{#3}}\,
\right)
}
\makeatother

\begin{document}

\begin{gather*}
\parensunderbrace
{\frac{4\cos^2\theta}{4\cos^2\theta + \sin^2\theta}}
{=\;(1+3\cos^3\theta)}
\\
\slowparensunderbrace
{\frac{4\cos^2\theta}{4\cos^2\theta + \sin^2\theta}}
{=\;(1+3\cos^3\theta)}
\end{gather*}

\end{document}


A different strategy:

\documentclass{article}
\usepackage{amsmath}
\usepackage{lipsum}

\makeatletter
\newcommand{\parenswithub}[1]{%
\mathpalette\parens@withub{#1}%
}
\newcommand\parens@withub[2]{%
\sbox\z@{$\m@th#1#2$}%
\vphantom{\copy\z@}%
\left(
\vrule height 1.1\ht\z@ depth \z@ width \z@
\smash{#2}%
\right)
}
\makeatother

\begin{document}

\lipsum*[3]
\begin{equation*}
\parenswithub{
1+{\underbrace{\frac{4\cos^2\theta}{4\cos^2\theta + \sin^2\theta}}_{=\;(1+3\cos^3\theta)}}+2
}
\end{equation*}
\lipsum[4]

\end{document}


The additional braces around \underbrace{...}_{...} are always necessary to get the spacing right.

Here's what you get with

$\Biggl( 1+{\underbrace{\frac{4\cos^2\theta}{4\cos^2\theta + \sin^2\theta}}_{=\;(1+3\cos^3\theta)}}+2 \Biggr)$


Simpler and more efficient.

• Actually, I'm not underbracing the whole thing, but I'll start working based on this. – einpoklum - reinstate Monica Oct 3 '17 at 17:28
• @einpoklum That's what I suspected. – egreg Oct 3 '17 at 17:28
• What's the significant benefit of the "slow" version? better spacing? – einpoklum - reinstate Monica Oct 3 '17 at 17:29
• So, I'll ask another question about the general case. – einpoklum - reinstate Monica Oct 3 '17 at 17:36
• @einpoklum The “slow” version is less efficient, but makes slightly larger parentheses. Not necessary to ask a new question. ;-) I'd more simply go with \biggl and \biggr. – egreg Oct 3 '17 at 17:41