Effectively, how to do "Underbrace" under two "Underbraces"?

\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}

enter image description here


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}

enter image description here

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}

enter image description here


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
\begin{equation}
    \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}}
\end{equation}
Words after

\end{document}

output

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.
\begin{equation}
    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}}
\end{equation}
Words after.

\end{document}

output

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
\begin{equation}
    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}
\end{equation}
Words after

\end{document}

output