# Effectively, how to do “Underbrace” under two “Underbraces”?

Goal: I am trying to find a way to write an equation for:

However, I fail, or at least so far could not find a way, to add the last layer in the bottom for "magnetic flux" using \underbrace.

Here is what I have so far,

$$\overbrace{ \underbrace{S^1_A \times S^1_B}_{E} \times \underbrace{S^1_C \times \mathbb{R}}_{\mathbb{CP}^{N-1}} }^{\text{ABCDEFG}}$$


and my output is this:

Could you find a better way to do it? Effectively, how to do "Underbrace" under two "Underbraces"? Or maybe we can also try to use TikZ instead? (i.e. I don't mind to try other methods)

p.s. Another thing is that, in my case, the size of ABCDEFG is smaller than the S^1_A \times S^1_B \times S^1_C \times \mathbb{R} — is there a way to adjust the size of ABCDEFG and others?

\documentclass{article}
\usepackage{mathtools,amssymb}
\begin{document}

\begin{gather*}
\overbrace{\underbrace{S^1_A \times S^1_B}_E\times
\underbrace{S^1_C\times\mathbb{R}}_{\mathbb{CP}^{N-1}}}^{\text{ABCDEFG}} \$-\normalbaselineskip] \underbrace{\kern5em}_{\text{magnetic flux}} \end{gather*} \end{document}  • Your answer is the best. I like to accept it. One more question: Suppose I put this into the figure form: \begin{figure}[!h] \begin{center} \begin{gather*} ...... \end{gather*} \end{center} \caption{} \end{figure} – wonderich Dec 28 '18 at 18:53 • how do we then change/adjust the size of this figure? Can we freely zoom the size? – wonderich Dec 28 '18 at 18:54 • The size of the brace is set by the \kern5em and can be changed to any value you like. It is a width of 5 times the width of a capital M in the current font. Smaller font -- smaller width (automatically!) and vice versa for greater font. – user2478 Dec 28 '18 at 18:58 • I hope to adjust the whole thing at once, not sure how to do - is it possible? – wonderich Dec 28 '18 at 19:00 • I asked a new question 467710 regarding this issue. Thanks! – wonderich Dec 28 '18 at 19:09 Solution 1: adjust by hand. \documentclass{article} \usepackage{amsfonts} \usepackage{mathtools} \begin{document} \[ \overbrace{ \underbrace{S^1_A \times S^1_B}_{E} \makebox[7pt][c]{\underbrace{\qquad~\times~\qquad\vphantom{\underbrace{S^1_C \times \mathbb{R}}_{\mathbb{CP}^{N-1}}}}_{\text{magnetic flux}}} \underbrace{S^1_C \times \mathbb{R}}_{\mathbb{CP}^{N-1}} }^{\text{ABCDEFG}}$
\end{document}


Solution 2: TikZ.

\documentclass{article}
\usepackage{amsfonts,amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,decorations.pathreplacing}
\begin{document}
$\tikzmarknode{l}{S^1_A \times S^1_B} \times \tikzmarknode{r}{S^1_C \times \mathbb{R}}$
\begin{tikzpicture}[overlay,remember picture,decoration={brace,raise=2pt},thick]
\draw[decorate] (l.north west) -- (r.north east) node[midway,above=3pt](ABC){ABCDEFG};
\draw[decorate] (l.south east) -- (l.south west) node[midway,below=3pt](E){$E$};
\draw[decorate] (r.south east) -- (r.south west) node[midway,below=3pt](CP){$\mathbb{CP}^{N-1}$};
\draw[decorate] (CP.south east) -- (E.south west|-CP.south) node[midway,below=3pt]{magnetic flux};
\end{tikzpicture}
\end{document}


• This is amazing! I suppose I can also adjust the size of "brace" by the first or the second way? – wonderich Dec 20 '18 at 18:35
• @wonderich Yes. First way: change the \quads. Second way: e.g. \draw[decorate] ([xshift=-4pt]CP.south east) -- (E.south west|-CP.south) node[midway,below=3pt]{magnetic flux}; (Will be offline now.) – user121799 Dec 20 '18 at 18:42
• @marmot: Isn't the fact that none of the braces in the second solution increase the depth of the equation potentially problematic? – Circumscribe Dec 20 '18 at 18:45
• @marmot, actually there is a problem, I cannot compile the 2nd option... – wonderich Dec 20 '18 at 19:32
• The error says: "undefined control sequence ... tikzmarknode" – wonderich Dec 20 '18 at 19:33

# Solution 4 (or 5): measuring boxes

I decided to create a slightly more basic version of the solution below.

The macro \bracetree, which I define below, creates the output you desire. It does does this by putting some of its arguments in box registers so that their widths can be measured. It then uses these width to insert the right amount of space at the right places to create braces with the correct widths

\documentclass{article}

\usepackage{amsmath}   %% <- not explicitly needed
\usepackage{amsfonts}  %% <- for \mathbb
\usepackage{mathtools} %% <- for \mathrlap, loads amsmath

\makeatletter   %% <_ make @ usable in macro names
\newcommand*\bracetree[6]{%
\begingroup                           %% <- limit scope of assignments
\sbox0{$\m@th\displaystyle #1$}     %% <- store arguments in box registers
\sbox2{$\m@th #4$}
\sbox4{$\m@th\displaystyle #3$}
\sbox6{$\m@th #5$}
\hspace{\dimexpr.5\wd0-.5\wd2}      %% <- insert space
\underbrace{
\hspace{\dimexpr-.5\wd0+.5\wd2}   %% <- remove space
\underbrace{\copy0}_{\copy2}
#2
\underbrace{\copy4}_{\copy6}    %% <- remove space
\hspace{\dimexpr-.5\wd4+.5\wd6}
}_{\textstyle #6}
\hspace{\dimexpr.5\wd4-.5\wd6}      %% <- insert space
\endgroup
}
\makeatother    %% <- revert @

\begin{document}

\noindent Words before
$$\overbrace{ \bracetree{S^1_A \times S^1_B}{\times}{S^1_C \times \mathbb{R}} {E}{\mathbb{CP}^{\mathrlap{N-1}}} {\textnormal{magnetic fluxxxx} } }^{\textstyle\textnormal{ABCDEFG}}$$
Words after

\end{document}


### A few notes:

• ABCDEFG was smaller because the text above the overbrace is effectively like a superscript, and therefore set at the corresponding font size. This can be remedied by adding \textstyle. I'm using \textnormal instead of \text because of the reasons outlined here.

• I'm using positive and negative \hspaces that cancel out precisely to set the width of the bottom underbrace.

• I'm using \mathrlap from mathtools to negate the horizontal space taken up by {N-1} because I think it looks better if the superscript is ignored for the purposes of brace placement and placement relative to braces.

• \sbox<box register>{<contents>} stores <contents> in a box register. These boxes can be printed with \copy<box register> and their widths are given by \wd<box register>. I'm using only even box registers because these are available as scratch registers.

• \m@th sets \mathsurround (amount of horizontal space $…$) to zero. This is the default value, so it actually does nothing here. If you get rid of it you can also remove \makeatletter and \makeatother.

# Solution 3 (or 4): black magic

I made the version of \bracetree below earlier by sort of taking apart the definition of the original \underbrace. The results should mostly be the same, but it is a little harder to explain how this works (and probably for you to adapt).

\documentclass{article}

\usepackage{amsmath}   %% <- not explicitly needed
\usepackage{amsfonts}  %% <- for \mathbb
\usepackage{mathtools} %% <- for \mathrlap, loads amsmath

\makeatletter %% <- make @ usable in macro names
\newcommand\bracetree[6]{%
\vtop{\m@th
\sbox0{$\mathstrut\displaystyle #1$}%
\sbox2{$\mathstrut\displaystyle #3$}%
\sbox4{$\mathstrut #4$}%
\sbox6{$\mathstrut #5$}%
\ialign{##\crcr
$\displaystyle\copy0#2\copy2$%
\crcr
\noalign{\kern3\p@\nointerlineskip}%
\hbox to \wd0{\upbracefill}\hfil\hbox to \wd2{\upbracefill}%
\crcr
\hskip.5\wd0\hskip-.5\wd4\copy4\hfil\copy6\hskip-.5\wd6\hskip.5\wd2
\crcr
\noalign{\kern3\p@\nointerlineskip}%
\hskip.5\wd0\hskip-.5\wd4\upbracefill\hskip-.5\wd6\hskip.5\wd2
\crcr
\hskip.5\wd0\hskip-.5\wd4\hidewidth\hbox{$#6$}\hidewidth\hskip-.5\wd6\hskip.5\wd2
\crcr
}%
}%
}
\makeatother  %% <- revert @

\begin{document}

\noindent Words before.
$$X = \overbrace{ \bracetree{S^1_A \times S^1_B}{\times}{S^1_C \times \mathbb{R}} {E}{\mathbb{CP}^{\mathrlap{N-1}}} {\text{magnetic flux}} }^{\textstyle\text{ABCDEFG}}$$
Words after.

\end{document}


I'm not really sure where to begin explaining this, but here it goes.

• \vtop creates a top aligned box: the first line of its contents will share a baseline with the surrounding equation. See here.
• \ialign is sort of like tabular: it allows you to align things next to each other or on top of each other.
• \crcr more or less does what \\ does in a tabular environment
• \noalign{\kern<length>} inserts a little vertical space
• \hskip<length> inserts horizontal space
• \hfil and \hidewidth both insert a stretchable amount of space. The latter will be negative when necessary.
• \upbracefill fills the available space with an underbrace

# Solution 2.5: Tikz again

I've modified marmot's answer (with his blessing). The difference between this version and his version is that this increases the depth of the equation (so "Words after" is actually below "magnetic flux").

DISCLAIMER: This nests TikZ environments!

Nesting TikZ environments is generally a bad idea because things like key values and bounding boxes can leak from the inner environment to the outer one and vice versa. While marmot believes it should be okay in this case, one should still be careful with this. See e.g this question and several pages linked to from there for more info.

If a modified version of this code is used elsewhere it is not guaranteed to still work smoothly.

\documentclass{article}

\usepackage{amsfonts,amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,decorations.pathreplacing}

\begin{document}

\noindent Words before
$$X = \begin{tikzpicture}[baseline,remember picture,decoration={brace,raise=2pt},thick] \node[anchor=base] {\tikzmarknode{l}{S^1_A \times S^1_B} \times \tikzmarknode{r}{S^1_C \times \mathbb{R}}}; \draw[decorate] (l.north west) -- (r.north east) node[midway,above=3pt](ABC){ABCDEFG}; \draw[decorate] (l.south east) -- (l.south west) node[midway,below=3pt](E){\strut E\vphantom{^{N}}}; \draw[decorate] (r.south east) -- (r.south west) node[midway,below=3pt](CP){\mathbb{CP}^{N-1}}; \draw[decorate] (CP.south east) -- (E.south west|-CP.south) node[midway,below=3pt]{magnetic flux}; \end{tikzpicture}$$
Words after

\end{document}