# Coordinates of a subpart of label (or multiple nodes that preserve text spacing)

I have two equations (one on top of the other) and I'd like to relate elements of the first one to elements of the second one by drawing an arrow between them.

I though of two ways to solve that:

1. Place each element in a separate node, and then draw arrows between corresponding nodes.
2. Have a single node for each equation (the full equation is put in the label), and find a way to get the coordinate of each element in the label.

I tried the first approach, but the horizontal spacing is odd: the node distance is obviously not the text spacing (see image below).

The second approach seems better to me (main reason: we keep the equation as a single node, which makes more sens imo), but I have no idea if it is possible to get the coordinate of a subpart of a label (and something tells me nodes should be considered as a single unsplittable element, so the solution would not be easy).

So far, using the 1st approach, I have:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (A) {$$A_1$$};
\node[anchor=west] at(A.east) (B) {$$= B_1 +$$};
\node[anchor=west] at(B.east) (C) {$$C_1$$};
\node[below of=A] (A') {$$A_2$$};
\node[anchor=west] at(A'.east) (B') {$$=B_2 +$$};
\node[anchor=west] at(B'.east) (C') {$$C_2$$};

\path (A) edge[<->] (A');
\path (B) edge[<->] (B');
\path (C) edge[<->] (C');
\end{tikzpicture}
\end{document}


which gives the following result (notice, for instance, the spacing on each side of the "+"):

More details:

• Vertical alignment of the equation is not a problem (i.e. it's fine if the arrows are not "vertical").
• Vertical alignment of the baseline of each element within an equation is not a problem (I know there is a relevant section in Tikz manual for this problem, so it's fine here).
• The equations should remain in the tikz picture. They are part of a kind of commutative diagram such as in here. An other way to look at the problem, based on this example is to be able to draw an arrow from below both the F and the x for instance.
• A solution without magic constants would be prefered: the example here is just a MWE.

The tikzmark library allows you to make elements of an equation nodes without changing the spacing.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{align*}
\tikzmarknode{A1}{A_1}&=\tikzmarknode{B1}{B_1}+\tikzmarknode{C1}{C_1}\\[2em]
\tikzmarknode{A2}{A_2}&=\tikzmarknode{B2}{B_2}+\tikzmarknode{C2}{C_2}
\begin{tikzpicture}[overlay,remember picture]
\foreach \X in {A,B,C}
{\draw[<->,shorten >=1pt,shorten <=1pt] (\X1) -- (\X2);}
\end{tikzpicture}
\end{align*}
\end{document


If you want to mark parts of a node, still tikzmark is your friend, you may want to use \subnode then.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark,positioning}
\begin{document}
\begin{tikzpicture}[remember picture]
\node (upper) {$\subnode{A1}{A_1}=\subnode{B1}{B_1}+\subnode{C1}{C_1}$};
\node[below=2em of upper]
(lower){$\subnode{A2}{A_2}=\subnode{B2}{B_2}+\subnode{C2}{C_2}$};
\foreach \X in {A,B,C}
{\draw[<->,shorten >=1pt,shorten <=1pt,overlay] (\X1) -- (\X2);}
\end{tikzpicture}
\end{document}


BTW, it is arguably better to use positioning for relative positioning.

• Thanks, but I forgot to mention (mea culpa) that the equation is itself part of a bigger tikzpicture, hence I can not take it out of the picture as you do (and as far as I tried, tikzmarknode can not be nested in a tikzpicture). I'll update the question to precise that. Nov 26, 2019 at 15:59
• @Bromind OK, then you are probably looking for \subnode, see my updated answer.
– user194703
Nov 26, 2019 at 16:18
• That's it, thanks. The compilation part sometimes causes me problem, but I ended up with what I needed. Thank. Nov 26, 2019 at 23:46
• @Bromind Glad to hear! Please note that tikzmark got repeatedly updated and extended, so you may want to update your TeX installation.
– user194703
Nov 26, 2019 at 23:50
• @BlackMild tikzmark is not mentioned in the pgf manual. It is a library written and maintained by LoopSpace, which is independent. There are quite a few such libraries. Apart from other master pieces by LoopSpace like knots, the cd library, which underlies tikz-cd, is also not mentioned in the manual.
– user194703
Nov 27, 2019 at 14:56