# How can I get an underbrace and an overbrace to partially overlap in an equation?

Sometimes I want to describe portions of an equation using \underbrace and \overbrace in such a way that the portions are not completely distinct, for example:

        /-------\
x = A + B + C + D + E
\-------/   \---/


What is the best way to do this?

One can use commands \phantom and \lefteqn.

Simple example: $\lefteqn{\overbrace{\phantom{1+2+3}}}1+\underbrace{2+3+4}$.

More complicated example:

$0+\lefteqn{\overbrace{\phantom{1+2+3}}}1+ \underbrace{2+3+\overbrace{x+y+z}+ \lefteqn{\overbrace{\phantom{4+5}}}4}+5$


Caveat: \lefteqn switches font to \displaystyle so one has to use something like \lefteqn{\textstyle... in inline formulas.

P.S. The recipe is taken from the excellent book by S.Lvovski (http://www.mccme.ru/free-books/llang/newllang.pdf, in Russian).

• This does help, but as far as I can tell only in the case where you want the first brace to start at the beginning of the line and you only want two braces that overlap. Am I correct, or is there a way to generalize this more? Jul 27, 2010 at 13:41
• @Michael No, why? Anything like $$0+ \lefteqn{\overbrace{\phantom{1+2+3}}}1+ \underbrace{2+3+\overbrace{x+y+z}+ \lefteqn{\overbrace{\phantom{4+5}}}4}+5$$ works just fine. Jul 27, 2010 at 16:31
• The only caveat is that one should be careful when using it in inline math: \lefteqn switches font to \displaystyle so one has to use \lefteqn{\textstyle ...}. Jul 27, 2010 at 16:33
• Thanks, Grigory! Could you please edit your answer to include an example such as this, and a short explanation of how to construct it? I think that would be really helpful here. Jul 27, 2010 at 16:44
• @Michael I've updated the answer to include some useful info from comments. As for explanation of how to construct such things (it seems I can't make it short and easy to understand so) I hope that everything will be clear from examples. Jul 27, 2010 at 17:07

This may not be the best solution, due to the overhead, but this is quite simple using TikZ. You insert invisible nodes at the points where you want the under and over braces to anchor to and then draw the braces between them. In my preamble, I have:

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,decorations.pathmorphing,calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}


(Obviously, the name can be shortened if this is to be used a lot. I'm not sure off the top of my head which of the decorations library is needed; the calc one is optional but extremely useful.) Then, I would typeset your example using:

$x = \tikzmark{ubl} A + \tikzmark{obl} B + C \tikzmark{ubr} + \tikzmark{ubll} D \tikzmark{obr} + E \tikzmark{ubrr}$

\begin{tikzpicture}[overlay, remember picture]
\draw[decoration={brace,amplitude=10},decorate,thick] ($(ubl)+(-0.25,-1)$) -- ($(ubr)+(-0.25,1.2)$);
...
\end{tikzpicture}


Reversing the order of the nodes flips the brace, so if that's the wrong way up then simply swap them. The stuff with the dollars is from the calc tikz library. What the above does is specify an offset from the node, so that if you find the default position of the braces a little off (which it probably is), it is simple to adjust them.

I use this a lot in lectures: for adding strike-outs to things, for better-looking brackets on huge matrices, for all sorts of things where you want to add a little graphical decoration afterwards.

For more details, search for 'remember picture' in the TikZ manual, and browse the examples at http://texample.net.

• Thanks Andrew, I hadn't actually seen tikz before so that's a neat introduction to something I probably want to look into more now. It is a little cumbersome though for this particular application, since you'd then also have to place the text next to the brace manually. Also, the braces seem quite a bit taller than the ones generated in math mode by \underbrace and just don't look quite 'right' -- although that's subjective, and possibly just because I'm already used to the other ones. Jul 27, 2010 at 16:57
• You can change the amplitude and thickness to suit, though I don't know if it's possible to get them exactly the same as LaTeX would do. When you say "place the text next to the brace manually", do you mean that you want to be able to add text at the point of the brace? (I didn't spot that this was in the spec) If so, that's really easy. Would you like me to add that to my answer? Jul 27, 2010 at 17:52

Use the package oubraces. You should get

from

\documentclass{article}
\usepackage{amsmath}
\usepackage{oubraces}
\begin{document}
$x=\overunderbraces{&&\br{3}{}}% {&A+&B+C&+&D&+E}% {&\br{2}{}& &\br{2}{}}$
\end{document}


Check out "LaTeX Mathmode" by Herbert Voß. The manual can be found on CTAN (PDF).

Skip down to section 63.2 "Overlapping braces". He also gives examples of other tricks you may want to pull using braces.

For future reference: this overlaps with How to have overlapping under-braces and over-braces both in the question above and in the answers below ;-)

(I would have posted this as a comment, but don't have enough reputation.)

There is also the underoverlap package. An example from that package:

$a + \UOLoverbrace{b +}[c + d]ˆx \UOLunderbrace{+ e}_y + f$


This is an answer for KaTeX and LaTeX(also for MathJax, but should remove inline-math delimeters since the parameter in \[lcr]lap is recognized as in math mode).

The following code is rendered using KaTeX.

Firstly, we should have a basic knowledge of \[lcr]lap. See the examples below.

|e\llap{abc}d|\\
|e\clap{abc}d|\\
|e\rlap{abc}d|


We can notice that they both set the width of its parameter to 0. And l c r lay the content left, center or right separately.

Based on this feature, we could have our answer.

First, create the first underbrace using \rlap, which will display with 0 width. And we use \phantom to let the underbrace have correct width and height.

\rlap{$\underbrace{ \phantom{A + B + C} }$}


Though there's been an underbrace, its width is 0. So we could add the following content directly.

A +


Then, we should create the overbrace. Similarly, using \rlap and \phantom to make the overbrace, and filling the width literally.

\rlap{$\overbrace{ \phantom{B + C + D} }$}

B + C +


Finally, do the same thing for the last underbrace.

\rlap{$\underbrace{ \phantom{D + E} }$}

D + E


That's it!

Full code:

\rlap{$\underbrace{ \phantom{A + B + C} }$}

A +

\rlap{$\overbrace{ \phantom{B + C + D} }$}

B + C +

\rlap{$\underbrace{ \phantom{D + E} }$}

D + E