# Underbrace in LaTeX

1. Oct 16, 2008

### rsq_a

I'd like to typeset an expression like,

$$a + b + c + d = e$$

The problem is I'd like to use multiple underbraces (or perhaps overbraces) which may also overlap. For example,

$$\underbrace{a + b}_\textrm{brace 1} + c + d = e$$

is the first brace. But I'd also like to have a second brace which underlines b, c, and d (and lies below the first brace).

How would I do that?

2. Oct 16, 2008

### CRGreathouse

$$\underbrace{\underbrace{a + b}_\textrm{brace 1} + c + d}_\textrm{brace 2}= e$$

3. Oct 16, 2008

### cristo

Staff Emeritus
I originally did that (but then deleted it). He is asking for the brace to start under b and end at d.

4. Oct 16, 2008

### alphysicist

Hi rsq_a,

$$a + \underbrace{b \makebox[0pt][r]{\displaystyle{\underbrace{\phantom{a+b}}_\textrm{brace 1}}} + c + d}_\textrm{brace 2} = e$$

$$\underbrace{a + \makebox[0pt][l]{\displaystyle{\underbrace{\phantom{b+c+d}}_\textrm{ brace 2}}} b}_\textrm{brace 1} + c + d = e$$

$$\underbrace{a + \makebox[0pt][l]{\displaystyle{\overbrace{\phantom{b+c+d}}^\textrm{ brace 2}}} b}_\textrm{brace 1} + c + d = e$$

You can say that the makebox command here is causing the inner brace to have a width of zero, for the purpose of calculating the outer brace. Let me show you the steps:

{\rm neutrons} + {\rm protons} + {\rm electrons}

which if you Latex it gives the output:

$${\rm neutrons} + {\rm protons} + {\rm electrons}$$

Now put in the first underbrace:

{\rm neutrons} + \underbrace{ {\rm protons} + {\rm electrons} }_{\rm charged}

output:

$${\rm neutrons} + \underbrace{ {\rm protons} + {\rm electrons} }_{\rm charged}$$

So that's good. Now create a zero width box at the point where one end of the second underbrace goes, like this (with the new stuff in bold):

{\rm neutrons} + \underbrace{ {\rm protons}
\makebox[0pt][r]{$\underbrace{ {\rm neutrons} + {\rm protons} }_{\rm nucleons} }+ {\rm electrons} }_{\rm charged} output: $${\rm neutrons} + \underbrace{ {\rm protons} \makebox[0pt][r]{\underbrace{ {\rm neutrons} + {\rm protons} }_{\rm nucleons} } + {\rm electrons} }_{\rm charged}$$ I noticed that this last version looks fine on the forum, but what's really happening is that there are two copies of {\rm neutrons} + {\rm protons}; they are just right on top of each other. Sometimes they won't line up perfectly, so to make sure that you never see the duplicate contents, you can use the phantom command: {\rm neutrons} + \underbrace{ {\rm protons} \makebox[0pt][r]{$\underbrace{ \phantom{ {\rm neutrons} + {\rm protons} } }_{\rm nucleons} }+ {\rm electrons} }_{\rm charged}

output:

$${\rm neutrons} + \underbrace{ {\rm protons} \makebox[0pt][r]{\underbrace{ \phantom{ {\rm neutrons} + {\rm protons}} }_{\rm nucleons} } + {\rm electrons} }_{\rm charged}$$

7. Mar 7, 2010

### zslevi

And I get the following error message for
$${\rm neutrons} + \underbrace{ {\rm protons} \makebox[0pt][r]{\underbrace{ {\rm neutrons} + {\rm protons} }_{\rm nucleons} }+ {\rm electrons} }_{\rm charged}$$

! Missing $inserted. <inserted text>$
l.14 + {\rm electrons} }
_{\rm charged}
?

8. Mar 7, 2010

### zslevi

Of course I was using my local LaTex installation.

9. Mar 7, 2010

### alphysicist

Hi zslevi,

There is a typo in my post; I left out a \$ that was needed. (If you look at the first three examples at the beginning of my post you'll see matching single dollar signs the way they should be.) It seems that the LaTeX engine here is lenient and allowed my mistake to go through.

If you use this for your example:

$${\rm neutrons} + \underbrace{ {\rm protons} \makebox[0pt][r]{\underbrace{ {\rm neutrons} + {\rm protons} }_{\rm nucleons} }+ {\rm electrons} }_{\rm charged}$$

I believe it will work fine.

10. Mar 8, 2010

### zslevi

Now it works fine, thanks.
However I still don't get what those single dollar signs are doing: are you nesting equation environments, or what?

11. Mar 8, 2010

### alphysicist

My understanding is that the makebox argument is in text mode; for example if you look at this:

$$xyz\makebox{abc}mno$$

you can see abc is treated as normal text, not equation symbols. But I wanted my box to contain equation components, so I had to get in (inline) equation mode and used the dollar signs (just like you would if you wanted math inside a paragraph of text).

12. Mar 9, 2010

### zslevi

Thanks for the explanation.