[Tex/LaTex] How to link different trees with TikZ

foresttikz-pgf

This is my current scenario: I have two trees with defined with forest, side by side, each one inside a minipage:

\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{fit}
\usepackage{forest}

\begin{document}
\begin{minipage}{0.3\textwidth}
\begin{forest}
[a
  [b
    [c]
    [d]
  ]
   [e,draw,dashed]
]
\end{forest}
\end{minipage}%
\begin{minipage}{0.3\textwidth}
\begin{forest}
[f
  [g]
  [,phantom]
  [h,tikz={\node [draw,dashed,fit=() (!1) (!l)] {};}
    [i]
    [j]
  ]
]
\end{forest}
\end{minipage}
\end{document}

Quack

So far, so good!

Now I want to link node e from tree 1 with node h from tree 2. Now things started to get frustrating. I tried to give these nodes a proper name and apply remember picture, but it seems the names are not persisted between environments (it's worth mentioning that Alan provided an alternative solution by using only one tree mimicking two trees here; it solves my immediate problem, but I am still interested in seeing how a proper link would be).

My searches in TeX.sx indicated tikzmark as the easiest, most direct solution, so here we go. I found a promising solution by Adam Liter here:

Using tikz overlay / remember picture option with forest trees

Sadly, it seems there were some updates in perhaps everything in the TeX world that made my trees collapse into a accident of nightmarish proportions and whatnot:

Package tikz Error: Cannot parse this coordinate.

The answer provided in the linked question does not work anymore, and neither does my code. So I beseech your wisdom! Tying a yellow ribbon 'round the old forest tree seems simple enough, however, my plans of creating a potential hammock between two independent forest trees have been shattered. :)

Does anybody have an idea of what is broken?

Best Answer

You can use \subnode:

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{forest}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{minipage}{0.3\textwidth}
\begin{forest}
[a
  [b
    [c,name=c]
    [d,name=d]
  ]
   [\subnode{marke}{e},draw,dashed]
]
\end{forest}
\end{minipage}%
\begin{minipage}{0.3\textwidth}
\begin{forest}
[f
  [g]
  [,phantom]
  [\subnode{markh}{h},name=h,tikz={\node [draw,dashed,fit=() (!1) (!l)] {};}
    [i]
    [j]
  ]
]
\end{forest}
\end{minipage}
\begin{tikzpicture}[overlay,remember picture]
\draw[green](marke.east)--(markh.west);
\end{tikzpicture}
\end{document}

enter image description here