Draw connection between node in forest tree diagram and something outside it
The problem with using \tikzmark
inside a forest
tree is that you are nesting tikzpicture
environments, which occasionally works but should never be relied on and should always be avoided (unless you really know what you're doing and are happy to keep all the itsy-bitsy pieces when it breaks).
Fortunately, tikzmark
provides \subnode{}
for such cases.
[A, name=A
[B]
[C
[\subnode{x}{X}, name=X]
[Y]
]
]
This solves the problem.
One thing to remember here is that Forest uses a special coordinate system forest cs
. However, you can only access this within a forest
environment or \Forest
macro. This seems to be true even if the starred macro, \Forest*
, is used which allows you to access nodes outside the current group e.g. from within another forest
environment or Forest[*]
macro.
Complete code:
\documentclass{article}
\usepackage[linguistics]{forest}
\usetikzlibrary{tikzmark,arrows.meta}
\begin{document}
\begin{forest}
[A, name=A
[B]
[C
[\subnode{x}{X}, name=X]
[Y]
]
]
\draw [-Latex,dotted] (A) to[out=west,in=west] (X);
\end{forest}
Lo\tikzmark{lorem}rem ipsum do\tikzmark{abc}lor sit am\tikzmark{amet}et.
\begin{tikzpicture}[remember picture, overlay]
\draw [-Latex, red] (x) [out=-75, in=110]to ([yshift=1.5ex]{pic cs:abc});
\draw [-Latex] ({pic cs:lorem}) to[out=south,in=south] ({pic cs:amet});
\end{tikzpicture}
\end{document}
OK, so for completeness' sake, this is my own improvement over cfr's helpful answer, taking into account the workaround in the comments to a previous question on basically the same topic by Stefan Müller. The workaround mentioned there and employed here is unfortunately only alluded to, basically, but not given an example of.
\documentclass{scrartcl}
\usepackage[linguistics]{forest}
\usetikzlibrary{positioning, tikzmark, arrows.meta}
% WORKAROUND:
% Definition copied from /usr/share/texlive/texmf-dist/tex/generic/pgf/systemlayer/pgfsys-common-pdf-via-dvi.def
% Compare https://tex.stackexchange.com/q/229500 and comments!
\makeatletter
\def\pgfsys@hboxsynced#1{%
{%
\pgfsys@beginscope%
\setbox\pgf@hbox=\hbox{%
\hskip\pgf@pt@x%
\raise\pgf@pt@y\hbox{%
\pgf@pt@x=0pt%
\pgf@pt@y=0pt%
\special{pdf: content q}%
\pgflowlevelsynccm%
\pgfsys@invoke{q -1 0 0 -1 0 0 cm}%
\special{pdf: content -1 0 0 -1 0 0 cm q}% translate to original coordinate system
\pgfsys@invoke{0 J [] 0 d}% reset line cap and dash
\wd#1=0pt%
\ht#1=0pt%
\dp#1=0pt%
\box#1%
\pgfsys@invoke{n Q Q Q}%
}%
\hss%
}%
\wd\pgf@hbox=0pt%
\ht\pgf@hbox=0pt%
\dp\pgf@hbox=0pt%
\pgfsys@hbox\pgf@hbox%
\pgfsys@endscope%
}%
}
\makeatother
\begin{document}
\begin{forest}
[A, name=A
[B]
[C
[\subnode{x}{X}, name=X]
[Y]
]
]
\draw [-Latex,dotted] (A) to[out=west,in=west] (X);
\end{forest}
Lo\tikzmark{lorem}rem ipsum do\tikzmark{abc}lor sit am\tikzmark{amet}et.
\begin{tikzpicture}[remember picture, overlay]
\draw [-Latex, red] (x) [out=-75, in=110]to ([yshift=1.5ex]{pic cs:abc});
\draw [-Latex] ({pic cs:lorem}) to[out=south,in=south] ({pic cs:amet});
\end{tikzpicture}
\end{document}
This should successfully compile with xelatex
(for Unicode support) under TeXLive 2016: