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:

        \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');

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

Two equations with arrows between elements, but odd spacing.

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.

\begin{tikzpicture}[overlay,remember picture]
 \foreach \X in {A,B,C}
 {\draw[<->,shorten >=1pt,shorten <=1pt] (\X1) -- (\X2);}

enter image description here

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

\begin{tikzpicture}[remember picture]
 \node (upper) {$\subnode{A1}{A_1}=\subnode{B1}{B_1}+\subnode{C1}{C_1}$};
 \node[below=2em of upper]
 \foreach \X in {A,B,C}
 {\draw[<->,shorten >=1pt,shorten <=1pt,overlay] (\X1) -- (\X2);}

enter image description here

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. – Bromind Nov 26 '19 at 15:59
  • 2
    @Bromind OK, then you are probably looking for \subnode, see my updated answer. – user194703 Nov 26 '19 at 16:18
  • That's it, thanks. The compilation part sometimes causes me problem, but I ended up with what I needed. Thank. – Bromind Nov 26 '19 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 '19 at 23:50
  • 1
    @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 '19 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.