labelling with arrows in an automated way

In adding-labels-to-a-formula is a tikz scheme that puts rounded boxes around parts of an equation so that they can be labelled. It looks nicer than plain old \underbrace, and can be automated without too much trouble. Here is an example with latex commands to do this tikz stuff without having to worry about the details:

\documentclass{article}

\usepackage{tikz}
\usepackage{amsmath}

% formula, text, node#
\newcommand{\mathWithDescription}[3]{%
\tikz[baseline]{%
\node[draw=red,rounded corners,anchor=base] (m#3)%
{$\displaystyle#1$};%
\node[above of=m#3] (l#3) {#2};%
\draw[-,red] (l#3) -- (m#3);%
}%
}

\newcounter{mathLableNode}

\newcommand{\mathLabelBox}[2]{%
\stepcounter{mathLableNode}%
\mathWithDescription{#1}{#2}{\themathLableNode}%
}

\begin{document}

$$\boldsymbol{\nabla}^2 = \mathLabelBox{ \frac{\partial^2}{\partial r^2} + \frac{1}{r} \frac{\partial}{\partial r}{} }{\boldsymbol{\nabla}_{\mathrm{T}}^2} + \frac{1}{r^2} \frac{\partial^2}{\partial \theta^2} + \frac{\partial^2}{\partial z^2}$$

$$\mathbf{E} = \mathLabelBox{ \mathbf{E}_0 }{A vector, with a chosen polarity} \mathLabelBox{ u(r, \theta, z) }{ Slowly varying (complex) envelope } e^{i k_0 z}.$$

\end{document}


I can use this \mathLabelBox{}{} as a one-for-one replacement for \underbrace{}_{}. Here's what the example latex yields:

I think it looks quite nice for short formulas (equation (1)), but not for short ones that have long text (equation (2)).

What would look a lot better is to stuff the text off to the side and draw arrows to the elements. I see this is possible with tikz too, and copying from the beamer arrows page something like the following can be produced

This was produced with the following MWE

% from http://www.texample.net/tikz/examples/beamer-arrows/ (switching to article class)

\documentclass{article} %
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows,shapes}

\begin{document}
\tikzstyle{every picture}+=[remember picture]

\item Coriolis acceleration
\tikz \node[coordinate] (n1) {};
\end{itemize}

\begin{equation*}
\vec{a}_p = \vec{a}_o+\frac{d^2}{dt^2}\vec{r} +
\tikz[baseline]{
\node[fill=blue!20,anchor=base] (t1)
{$2\vec{\omega}_{ib}\times\frac{d}{dt}\vec{r}$};
} +
\tikz[baseline]{
\node[fill=red!20, ellipse,anchor=base] (t2)
{$\vec{\alpha}_{ib}\times\vec{r}$};
} +
\tikz[baseline]{
\node[fill=green!20,anchor=base] (t3)
{$\vec{\omega}_{ib}\times(\vec{\omega}_{ib}\times\vec{r})$};
}
\end{equation*}

\begin{itemize}
\item Transversal acceleration
\tikz\node [coordinate] (n2) {};
\item Centripetal acceleration
\tikz\node [coordinate] (n3) {};
\end{itemize}

\begin{tikzpicture}[overlay]
\path[->] (n1) edge [bend left] (t1);
\path[->] (n2) edge [bend right] (t2);
%        \path[->] (n3) edge [out=0, in=-90] (t3);
\path[->] (n3) edge [bend right] (t3);
\end{tikzpicture}

\end{document}


but wasn't something that looked easy to automate like the simpler tikz I've used above. (EDIT note: on adding the MWE above, it doesn't look as bad as I originally thought ... a lot of the complexity was actually due to the beamer specific stuff).

I'd like to have an \underbrace like command like my \mathLabelBox that incorporates the spline arrows in the beamer examples? Is there an easier way to do this than the multi-part code in example taken from the beamer arrows page above?

-
MWE including the tikz label commands above, or a copy of the beamer arrows page example (which appears to be standalone, but not minimal) that I'm wondering how to automate? If the first, I've now provided that in an edit, and updated the image to show both a "nice" looking example and the original image that was cluttered by the labels. –  Peeter Joot Dec 9 '12 at 4:24
Yes, I am lazy and didn’t want to assemble the linked solution and your code. How automated should the solution be? From a TikZ view-point, positioning and curved lines wouldn’t be a problem … –  Qrrbrbirlbel Dec 9 '12 at 4:39
I've now also added the MWE for the arrows stuff I liked (which after removing the beamer specific stuff doesn't look so bad). I'm still not sure how it could be done all within an equation enviornment. Perhaps that's the first question to answer. –  Peeter Joot Dec 9 '12 at 5:03
Well, then I have another follow-up question: In the beamer example, the descriptions are outside of the equation environment (and placement is left to LaTeX and the user and is not related to the actual equation) and the \node[coordinate] (c?) {}; are used for TikZ to remember the location of the itemized text (aka \tikzmark). The other example places the description node relative to the math content/node. Question: What do you want to achieve? –  Qrrbrbirlbel Dec 9 '12 at 5:10
Inside the equation environment was what I was thinking of... which looks like what you did below. –  Peeter Joot Dec 9 '12 at 5:36

Code

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\newif\ifclipme\clipmetrue
\tikzset{labelstyle/.style={LabelStyle/.append style={#1}},linestyle/.style={LineStyle/.append style={#1}}}
\tikzset{LabelStyle/.initial={},LineStyle/.initial={}}

\newcommand{\mathWithDescription}[4][]{{%
\tikzset{#1}%
\tikz[baseline]{
\node[draw=red,rounded corners,anchor=base] (m#4) {$\displaystyle#2$};
\ifclipme\begin{pgfinterruptboundingbox}\fi
\node[above of=m#4,font=\strut, LabelStyle] (l#4) {#3};
\draw[-,red, LineStyle] (l#4) to (m#4);
\ifclipme\end{pgfinterruptboundingbox}\fi
}%
}}
\newcommand{\mathWithDescriptionStarred}[3][]{{%
\clipmefalse%
\mathWithDescription[#1]{#2}{#3}{\themathLableNode}%
}}

\newcounter{mathLableNode}
\newcommand{\mathLabelBox}[3][]{%
\stepcounter{mathLableNode}%
\mathWithDescription[#1]{#2}{#3}{\themathLableNode}%
\vphantom{\mathWithDescriptionStarred[#1]{#2}{#3}{\themathLableNode}}%
}

\begin{document}
$$\boldsymbol{\nabla}^2 = \mathLabelBox{\frac{\partial^2}{\partial r^2} + \frac{1}{r} \frac{\partial}{\partial r}} {\boldsymbol{\nabla}_{\mathrm{T}}^2} + \frac{1}{r^2} \frac{\partial^2}{\partial \theta^2} + \frac{\partial^2}{\partial z^2}$$

$$\mathbf{E} = \mathLabelBox[ labelstyle={yshift=1.2em}, linestyle={} ]{\mathbf{E}_0}{A vector, with a chosen polarity} \cdot \mathLabelBox[ labelstyle={xshift=2cm}, linestyle={out=270,in=90, latex-} ]{u(r, \theta, z)}{Slowly varying (complex) envelope} \cdot e^{i k_0 z}.$$
\end{document}


A simple example

Code

$$\mathbf{E} = \mathLabelBox{\mathbf{E}_0}{A vector, with a chosen polarity} \cdot \mathLabelBox[labelstyle={below of=m\themathLableNode,below of=m\themathLableNode}]{u(r, \theta, z)}{Slowly varying (complex) envelope} \cdot e^{i k_0 z}.$$


Output

-
Is there supposed to be two documents in your answer above? –  Peeter Joot Dec 9 '12 at 5:23
@PeeterJoot No, that was a copy-paste error on my side. If needed (and no better answer came up), I may add more information to this solution tomorrow. –  Qrrbrbirlbel Dec 9 '12 at 5:45
This isn’t a very sophisticated solution since, well, not much is “automated”. The placement of the description nodes and the line style are still given by hand. Beware of having the description going into the margin. –  Qrrbrbirlbel Dec 9 '12 at 5:48