# How to have overlapping under-braces and over-braces

I am trying to typeset an equation that has overlapping over and under braces as per the image below:

I have managed to typeset it using a sort of a hack, by typing the equation twice, once using \phantom commands and then raising it. Is there an easier way, perhaps a macro? MWE for the image above is shown below.

\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
$a+b+\overbrace{c+d+e+f+g}^{x}+h+i+k+l=e^2$
\vspace{-35pt}
$\phantom{+b+c+d+}\underbrace{\phantom{e+f+g+h+i}}_{y}\phantom{+k+=e^2}$
\end{document}

-

\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
$a+b+\rlap{\overbrace{\phantom{c+d+e+f+g}}^x}c+d +\underbrace{e+f+g+h+i}_y +k+l=e^2$
\end{document}


-
Yes, it is better. – Leo Liu Mar 8 '11 at 16:49
Nice solution. I think there should be a {} before +k to fix the spacing though. – TH. Mar 19 '11 at 11:29
It should be +\>k to get the right spacing for binary operators. – Michael Ummels Mar 19 '11 at 17:44
\mathrel{+} should do the trick – Herbert Mar 20 '11 at 8:19
@Herbert: With \mathrel, the spaces are slightly too large. – Hendrik Vogt Jan 29 '13 at 10:33

In Herbert's Mathmode 63.2 Overlapping braces, he uses a similar method with align, \hphantom and \-11pt]. I think it is better to use LaTeX's \ooalign: \[ \ooalign{ a+b+\overbrace{c+d+e+f+g}^{x}+h+i+k+l=e^2\cr \phantom{a+b+c+d+{}} {\underbrace{\phantom{e+f+g+h+i}}_{y}} \cr }


Note: \ooalign is defined in LaTeX kernel, and is used in definition of some special text accents and math symbols. It is defined with primitive \halign. Similar approach is widely used in LaTeX kernel to define overlapping symbols — like \angle, \cong, \notin, \overrightarrow and \overbrace, etc.

-
I think it should read: "Plain's \ooalign" – morbusg Mar 8 '11 at 16:48
@morbusg: There're some macros defined both in LaTeX and Plain TeX format. Although Plain TeX is earlier, it is still a LaTeX command, right? – Leo Liu Mar 8 '11 at 16:52
Well, sure, if you want to diss Plain. – morbusg Mar 8 '11 at 16:59
@morsburg @Leo Liu, thanks. I looked this one up and is in the ltxplain.dtx, where all the Plain commands live in LaTeX. Nice solution. – Yiannis Lazarides Mar 8 '11 at 18:45

Looking at the definitions of the \overbrace and \underbrace macros reveals that the are using \ialign which \halign with a zero \tabskip and empty \everycr.

So the braces are on there own "table rows". It is possible to define a macro which does take three arguments: the first part only under the overbrace, the middle part between both and the last part only over the underbrace. Then place this into a similar \ialign structure with three cells and three rows. The braces are then put into the plainTeX equivalent of \multicolumn{2} (\span\omit as I understand it).

However, the super- and subscript doesn't work as normal and must be read manual and placed at the correct positions. Getting them to the correct vertical position was the most difficult thing to get right.

The code:

\documentclass{article}

\iffalse
% The definitions of the existing macros for study:

\overbrace:
macro:#1->\mathop {\vbox {\m@th \ialign {##\crcr
\noalign {\kern 3\p@ }\downbracefill \crcr \noalign {\kern 3\p@ \nointerlineskip }$\hfil \displaystyle {#1}\hfil$\crcr
}}}\limits

\underbrace:
macro:#1->\mathop {\vtop {\m@th \ialign {##\crcr
$\hfil \displaystyle {#1}\hfil$\crcr \noalign {\kern 3\p@ \nointerlineskip }\upbracefill \crcr \noalign {\kern 3\p@ }%
}}}\limits

\fi

\makeatletter
\def\overunderbrace#1#2#3{%
\begingroup
\let\overunderbrace@sup\empty
\let\overunderbrace@sub\empty
\@ifnextchar^%
{\@overunderbracesup{#1}{#2}{#3}}%
{\@ifnextchar_%
{\@overunderbracesub{#1}{#2}{#3}}%
{\@overunderbrace{#1}{#2}{#3}}%
}%
}

\def\@overunderbracesup#1#2#3^#4{%
\def\overunderbrace@sup{#4}%
\@ifnextchar_%
{\@overunderbracesub{#1}{#2}{#3}}%
{\@overunderbrace{#1}{#2}{#3}}%
}

\def\@overunderbracesub#1#2#3_#4{%
\def\overunderbrace@sub{#4}%
\@ifnextchar^%
{\@overunderbracesup{#1}{#2}{#3}}%
{\@overunderbrace{#1}{#2}{#3}}%
}

\def\@overunderbrace#1#2#3{%
\mathop {\vcenter {\m@th \ialign {##&##&##\crcr
\noalign {\kern 3\p@}%
\span\omit\hfil\hbox{\vbox to 0pt{\vss\hbox{\vbox{\hbox{$\m@th\scriptstyle\overunderbrace@sup$}\vspace{0pt}}}}}\hfil
&%
\crcr \noalign {\kern 5\p@\nointerlineskip}%
\span\omit\downbracefill&%
\crcr \noalign {\kern 3\p@\nointerlineskip}%
$\hfil \displaystyle {#1}\hfil$&%
$\hfil \displaystyle {#2}\hfil$&%
$\hfil \displaystyle {#3}\hfil$%
\crcr \noalign {\kern 3\p@\nointerlineskip}%
& \span\omit  \upbracefill
\crcr \noalign {\kern 5\p@\nointerlineskip}%
&\span\omit\hfil\hbox{\vbox to 0pt{\hbox{\vbox{\vspace{0pt}\hbox{$\m@th\scriptstyle\overunderbrace@sub$}}}\vss}}\hfil
\crcr \noalign {\kern 3\p@ }%
}}}%
\endgroup
}

\makeatother

\begin{document}

% The extra \mathord{} is used to get the spacing right, otherwise
% the +'s before f and h think they are signs not operators.
% This is content specific and can't be added to the \overunderbracemacro.
$a+b+\overunderbrace{c+d+\mathord{}}{e+f+g}{\mathord{}+h+i}^{x}_{y} =e^2$

\end{document}


## Result

-
Ok, I added the final working code. Could the person which down-voted the original unfinished suggestion please undo the vote. Thanks. – Martin Scharrer Mar 18 '11 at 23:34

Use the package oubraces, it provides the command \overunderbraces{upper_braces}{main_formula}{lower_braces}.

In the main_formula use the token & to split the formula in blocks pointing where start and end the braces; in the other arguments use \br{n}{label} where n is the number of blocks spanned by the brace and separate them with &.

For this example the code looks

\documentclass{article}
\usepackage{amsmath}
\usepackage{oubraces}
\begin{document}
$x=\overunderbraces{&\br{2}{x}}% upper brace {a+b+&c+d+&e+f+g&+h+i}% main formula {&&\br{2}{y}}=e^2 % lower brace$
\end{document}


and the result

-
Thanks. Will take a look at the package. – Yiannis Lazarides Aug 4 '14 at 15:38