# Curly brace to insert something into an equation? Like an inverted underbrace

I have a generic equation, into which I want to "insert" something so that the reader can see what I inserted into the equation and at which point.

E.g.

\documentclass{plain}

\begin{document}
$f = < x | R y >$

Insert $\int d^3 p |p> <p|$ = 1 before y.
\end{document}


What I imagine is a horizontal curly brace, which extends underneath the equation, and which has its tip touching a spot just slightly to the left of the $y$, while in the brace there is written $\int d^3 p |p> <p|$. It would look something like an inverted \underbrace{foo}_{bar}, but I have no idea how to achieve something like that.

In ASCII art this would maybe look like this:

f = <x | R y >            (1)
__|__
____|     |____
| Insert here! |

-

Here's a variant of Gonzalo's code that uses \mathop{}\limits instead of \raisebox. The output is almost the same.

\documentclass{article}
\usepackage{mathtools}
\begin{document}
$f = \langle x | R\mathclap{\mathop{}\limits_{\overbrace{ \textstyle\int d^3 p |p\rangle \langle p|}}}y \rangle$
\end{document}


If you want something more flexible, use the \braceinsert macro from the code below. It takes one optional argument (the amount by which the brace should be lowered, with default 0ex) and one mandatory argument (the stuff you want under the brace). Moreover, it takes care of the case that the \braceinsert is surrounded by \left...\right delimiters, like this:

To be precise, you'll have to use \bileft...\biright instead of \left...\right (like braceinsert left and right). After the outermost \biright, only explicit superscripts will work, so instead of ' you'd have to use ^\prime. (Subscripts won't work properly, but I don't know when one would want one.)

\documentclass{article}
\usepackage{mathtools}
\setlength{\textwidth}{11cm}
\makeatletter
\newcounter{left@right}
\newcommand*\bileft[1]{\left#1\stepcounter{left@right}}
\newcommand*\biright[1]{\right#1%
\@ifnextchar^{\with@superscript}{\without@superscript}}
\def\with@superscript^#1{^{#1}\without@superscript}
\def\without@superscript{%
\ifnum\theleft@right=0
\vphantom{\brace@insert@strut}
\gdef\brace@insert@strut{}
\fi
}
\def\brace@insert@strut{}
\newcommand{\braceinsert}[2][0ex]{%
\def\insert@material{%
\mathop{\rule[-#1]{0pt}{0pt}}\limits_{\overbrace{#2}}%
}%
\expandafter{\insert@material}
\ifnum\theleft@right>0
\smash{\mathclap{\insert@material}}
\else
\mathclap{\insert@material}
\fi
}
\makeatother
\begin{document}
In the formula
$f = 5 \bileft( b^2 + \langle x | R \braceinsert[0.2ex]{\textstyle\int d^3 p |p\rangle \langle p|} y \rangle \biright)^2,$
the depth of the stuff under the brace is added only \emph{after}
\verb|\biright)^2|.
\end{document}


(Maybe this is wayyy too complicated?)

-
Thanks for the addition. However, with \mathop{}\limits you run into the problem that you cannot change the vertical placement of the overbrace. That makes it protrude into the actual equation (that, of course, depends on the equation; in the example though it happens). Do you maybe know how to adjust the placement? – mSSM Jun 16 '12 at 17:31
@mSSM: See my edit. – Hendrik Vogt Jun 20 '12 at 8:11
Ha, fun. Thanks for that solution. Just curious - why did you come up with it? Were you not satisfied with the raisebox solution given above? Btw, one minor downside of both solutions is that it "blows up" the vertical size of the content inside the braces. E.g., if I wanted to do \left\langle ... \right\rangle, LaTeX would make those huuuuuge. The obvious work-around would be to use manual sizes from the \big{l,r} family. – mSSM Jun 20 '12 at 11:15
@mSSM: The reason why I was not quite satisfied with Gonzalo's nice answer was that the 4ex in \raisebox{-4ex} has to be guessed. In my solution, the vertical placement is almost OK, which makes the guessing at least simpler. You're right with the \left...\right downside; it was not so easy to come up with a solution. See my edit! (Maybe it's way too complicated, I don't know.) – Hendrik Vogt Jun 20 '12 at 14:55
@mSSM: OK, now it works with amsmath, but you'll have to use \bileft...\biright around \braceinserts (brace insert left and right). – Hendrik Vogt Jun 20 '12 at 18:09

Ok, before people wonder what happened to the tikz solution, here is an alternate method, using, well... tikz. \tikzmark is used to name the location in the equation that you want to refer to. This name is then given to \Insert along with various options to control the behavior of the brace and the line connecting them.

The parameters passed to \Insert are:

#1. Vertical shift to be applied to the brace

#2. Options to control the behavior of the connecting line.

#3. Name of the marker in the equation.

#4. Text to be braced, which is to be inserted.

## Notes:

• This does require two runs. First one to determine the locations, and the second to do the drawing.
• Probably will have issues if the text begin inserted is not on the first line as the arrow will probably overlap other text.

## Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newcommand*{\BraceAmplitude}{0.5em}%
\newcommand*{\Insert}[5][0.0ex]{%
\tikzmark{a}#5\tikzmark{b}%
\begin{tikzpicture}[overlay,remember picture]
\draw [decoration={brace,amplitude=\BraceAmplitude},decorate,ultra thick,blue,#2]
($(a.north)+(0,#1)$) -- ($(b.north)+(0,#1)$);
\draw [thick, blue, #3] ($(a.north)!0.5!(b.north)+(0,#1)+(0,\BraceAmplitude+2pt)$) to (#4);
\end{tikzpicture}%
}

\begin{document}
$f = < x | R \tikzmark{MyMarker} y >$

\bigskip
In the above equation, we need to insert \Insert[1.5ex]{orange}{out=90, in=-90,->,shorten >=-1pt}{MyMarker}{$\int d^3 p |p> <p| = 1$} before $y$.

$f = < x | R \tikzmark{MyMarkerA} y \tikzmark{MyMarkerB}>$

\bigskip
Insert \Insert[1.5ex]{blue}{out=90, in=-90,->,shorten >=-1pt,red}{MyMarkerA}{$\int d^3 p |p> <p| = 1$} before $y$, and some more
\Insert[1.0ex]{brown}{out=135, in=-70,->,shorten >=-2pt}{MyMarkerB}{\textbf{stuff}} after $y$.
\end{document}

-
How do those \tikz commands work with the externalize library? – mSSM Jun 16 '12 at 17:34
@mSSM: Hmmm... Had not considered that. If you can compose a MWE that shows the problem that would be an excellent question to post considering how widely \tikzmark is used. If it is a problem you'd probably have to disable externalization for the pictures that use \tikzmark, unless some came up with a better solution. – Peter Grill Jun 16 '12 at 17:59

Here's one possible solution (in LaTeX2e) using an \overbrace and \mathclap (from the mathtools package); the vertical position was adjusted using \raisebox:

\documentclass{article}
\usepackage{mathtools}

\begin{document}

$f = < x | R\mathclap{\raisebox{-4ex}{\overbrace{\textstyle\int d^3 p |p> <p|}}} y >$

\end{document}


I also would suggest you to use \langle, \rangle instead of the relational symbols <, >a:

\documentclass{report}
\usepackage{mathtools}

\begin{document}

$f = \langle x | R\mathclap{\raisebox{-4ex}{\overbrace{\textstyle\int d^3 p |p\rangle \langle p|}}} y \rangle$

\end{document}


-
You were too fast. – Marco Daniel Mar 6 '12 at 18:08
That \raisebox trick is pretty cool. Thanks alot! And yeah - I am of course using \langle and \rangle, and all that other stuff that is out there (like \lvert). I just did not want to clutter the example. :-) – mSSM Mar 6 '12 at 18:16
I have changed my accepted answer to the one given by Hendrik Vogt. The code is a tad more elegant as it removes a lot of logic from the document itself, and also has a better default placement (as compared to the raisebox method). See the comments below the answer. – mSSM Jun 21 '12 at 9:07