# How to link different trees with TikZ

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}


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?

• +1 Thanks for bringing this up. :) I've also updated my answer accordingly. – Adam Liter Sep 28 '17 at 21:59
• It would be simpler if you wanted to draw a line from h to where e would be if e's root was where h's root is. That Forest can do by itself. I think it would be good if there was a more integrated way of doing this, but \subnode is the best I can think of right now. – cfr Sep 30 '17 at 2:40

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}


• Wow, that is very impressive! Thank you, Ulrike! – Paulo Cereda Sep 28 '17 at 10:45
• Do you understand in detail why \subnode works? (Unfortunately your nice trick no longer works very smoothly once the nodes become more involved, like going over two lines.) – user121799 Jun 29 '18 at 20:12

No idea what is broken, but is this workaround OK?

Essentially, use one forest for both trees, with the root as a phantom node. The s sep determines the distance between the two trees.

I don't know if/how nodes are named in forest, so I gave the e and h nodes an alias that I used in the \draw at the end.

\documentclass[12pt]{article}

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

\begin{document}
\begin{forest}
[,phantom,s sep=3cm
[a
[b
[c]
[d]
]
[e,draw,dashed,alias=e]
]
[f
[g]
[,phantom]
[h,alias=h,tikz={\node [draw,dashed,fit=() (!1) (!l)] {};}
[i]
[j]
]
]
]
\draw [-stealth](e) to[bend right] (h.west);
\end{forest}
\end{document}

• +1 It is indeed a nice workaround! :) I am interested in seeing why my original attempt with tikzmark (as well as some answers in the site that make use of it) had failed. For now, thank you very much! Let us wait a bit. :) – Paulo Cereda Sep 28 '17 at 10:16