Can TikZ emulate PSTricks' inline \Rnodes, scattered along a complex equation?

PSTricks' \Rnode makes it easy to add annotations to a complex equation typeset by (La)TeX:

Simply add \Rnodes around existing elements of the equation, and the mathematical typesetting is preserved. How is this achieved in TikZ, with the same simplicity?

The LaTeX code which produced the above example:

\documentclass[12pt]{standalone}
\usepackage{pst-node}

\begin{document}

\raisebox{0ex}[4ex][5ex]{
$\rule{0ex}{5ex} R^{\alpha \beta} - {1 \over 2}R g^{\alpha \beta} + g^{\alpha \beta} \Lambda = \frac{8 \pi G}{c^4 \mu_0} \left( F^{\alpha}{}^{\Rnode{a}{\scriptstyle\psi}} % <---- 1st \Rnode F_{\Rnode{b}{\scriptstyle\psi}}{}^{\beta} + % <---- 2nd \Rnode {1 \over 4} g^{\alpha \beta} F_{\psi\tau} F^{\psi\tau}\right) \ncbar[linecolor=blue,angle=-90,nodesep=1pt,arm=2ex]{<->}{a}{b}$ }

\end{document}

-
Sorry, pstricks is no longer needed as you load pst-node. –  stalking isn't tolerated Sep 14 '13 at 6:30
Thanks. I edited the question to remove \usepackage{pstricks}. –  Dominic Sep 14 '13 at 6:49
Yes and it is known as \tikzmark. tex.blogoverflow.com/2013/04/beware-the-tikzmark-my-son –  percusse Sep 14 '13 at 6:56
Take a look at tex.stackexchange.com/q/83911/86 –  Loop Space Sep 14 '13 at 7:44
–  Qrrbrbirlbel Sep 14 '13 at 10:13

Actually, it is relatively easy to do with TikZ:

Place nodes with

\tikz[baseline, rememeber picture]
\node[anchor=base, inner sep=+0pt, outer sep=+0pt] (<name>) {<text>};


The combination of baseline and anchor=base puts <text> on the already established baseline and inner sep=+0pt and outer sep=+0pt makes sure that the node (and the picture) has the same size as the actual box enclosing <text>.

Then you can use those <name>s in a TikZ path (that should be overlayed):

\tikz[remember picture, overlay]
\draw (<name 1>) -- (<name 2>);


If you often simply connect two nodes and always the last defined you can make your life easier with the macros \Tnode and \TnodeC where the latter is like the first but also draws a path to the last node defined by the first.

If you don’t want to connect the last two nodes you give a coordinate/node name after the optional arguments of the edge.

An overview:

\Tnode[<optional node options>]{<node name>}{<node math>}


and

\TnodeC[<optional node options>]{<node name>}{<node math>}
[<optional edge options>]{<alternative target node for the edge>}


Of course, you can also use to path={<anything>} inside the <optional edge options> to overwrite anything (you don’t need to use \tikztarget or \tikzstart).

For unrelated edges, you can of course just use a separated \path as described earlier.

The \TnodeC has also a starred version where the vertical dimension of the path is taken into account. (In your case this would stretch \left( … \right) which is the reason why I used \vphantom in combination with \TnodeC* but you could also just use \bigl( … \bigr).)

It is also possible to implement a syntax where the node names are enclosed in ( and ) but that makes the code slightly larger.

For various approaches to implement \ncbar see Is there a TikZ equivalent to the PSTricks \ncbar command?. I have used my paths.ortho library and its du path operator/style but feel free to replace it with any solution you want. (The udlr/distance is measured between the border of the node and horizontal (middle) part of the path.)

Code

\documentclass{article}
\usepackage{tikz} \usetikzlibrary{paths.ortho}
\makeatletter
\def\tnode@#1#2#3{%
\node[inner sep=+0pt, outer sep=+0pt, anchor=base, name=#2, #1]{\m@th$#3$};}
\newcommand*{\Tnode}[3][]{%
\tikz[baseline,remember picture]{\tnode@{#1}{#2}{#3}}\xdef\@tnodeclastnode{#2}}
\def\TnodeC{%
\pgfutil@ifnextchar*%
{\let\if@tnodec@overlay\iftrue\expandafter\TnodeC@\pgfutil@gobble}
{\let\if@tnodec@overlay\iffalse\TnodeC@}}
\newcommand*{\TnodeC@}[3][]{%
\pgfutil@ifnextchar[{\TnodeC@opt@{#1}{#2}{#3}}{\TnodeC@opt@{#1}{#2}{#3}[]}}
\def\TnodeC@opt@#1#2#3[#4]{%
\pgfutil@ifnextchar\bgroup{\@@TnodeC{#1}{#2}{#3}{#4}}
{\@@TnodeC{#1}{#2}{#3}{#4}{\@tnodeclastnode}}}
\def\@@TnodeC#1#2#3#4#5{%
\tikz[baseline, remember picture]{
\tnode@{#1}{#2}{#3}%
\path[overlay] (#2) edge[TnodeC edge/.try,#4] coordinate (@tnodec@aux) (#5);
\if@tnodec@overlay
\path (#2 |- @tnodec@aux) -- (#2);
\fi}}
\makeatother
\tikzset{TnodeC edge/.style={
du, udlr/distance=2ex, shorten >=+4\pgflinewidth, shorten <=+4\pgflinewidth}}
\begin{document}
beans soufflé gingerbread. Sesame snaps apple pie chocolate bar chocolate gummies
jujubes.
$\textstyle R^{\alpha\beta} - \frac{1}{2} R g^{\alpha\beta} + g^{\alpha\beta} \Lambda = \frac{8 \pi G}{c^4 \mu_0} \left( F^{\alpha\Tnode {a}{\scriptstyle\psi}} F_{ \TnodeC {b}{\scriptstyle\psi}[blue,stealth-stealth]} {}^{\beta} + \frac{1}{4} g^{\alpha \beta} F_{\psi\tau} F^{\psi\tau} \right) \vphantom{ F^{\Tnode {@a}{\scriptstyle\psi}} F_{\TnodeC*{@b}{\scriptstyle\psi}} }$
Caramels halvah apple pie cookie pastry wafer lollipop croissant jujubes. Tart cupcake
pastry. Gingerbread candy sesame snaps pastry oat cake jelly beans. Soufflé chocolate
gummi bears.

$\Tnode {aa}{\psi} \quad\TnodeC*{bb}{\pi} [out=30, in=150, looseness=10, <->]$
\end{document}


Output

-
Wot no tikzmark? –  Loop Space Sep 14 '13 at 18:22
I see now that tikzmark has a \subnode macro, an \Rnode or \rnode equivalent. I'll try that also. Thanks. –  Dominic Sep 14 '13 at 20:15
@Dominic Actually the \subnode macro wouldn't be of use here. The \subnode macro is intended to go inside a node of a larger TikZ picture. –  Loop Space Oct 2 '13 at 8:45