9

I'm having trouble connecting a node inside of a forest tree diagram with some arbitrary text outside it.


MWE:

\documentclass[12pt,paper=a4]{scrartcl}
\usepackage[linguistics]{forest}
\usetikzlibrary{positioning}

% cf. http://tex.stackexchange.com/a/1912/60686
\tikzstyle{na} = [shape=rectangle,inner sep=0pt,text depth=0pt]

\begin{document}

\begin{forest}
[A, name=A
    [B]
    [C
        [X, name=X]
        [Y]
    ]
]
\draw[-latex,dotted] (A) to[out=west,in=west] (X);
\end{forest}

\tikzstyle{every picture}+=[remember picture]
\tikz\node[na](lorem){Lorem}; ipsum \tikz\node[na](abc){dolor}; sit \tikz\node[na](amet){amet};.

\begin{tikzpicture}[overlay]
    \draw (X) to (abc);
    \draw[-latex] (lorem) to[out=south,in=south] (amet);
\end{tikzpicture}

\end{document}

The result of the above code unfortunately looks like this:

enter image description here

In other words, tikz can't seem to find the node X, so the line just trails off to nowhere instead of connecting the X node to the abc node. In order to demonstrate that connecting things works inside of either the diagram or the text, I drew a bunch of arrows there as well.

Putting the line \tikzstyle{every picture}+=[remember picture] above \begin{forest} causes the various content boxes and drawings to appear all on different pages.

4
  • 2
    See if tex.stackexchange.com/questions/174398/… helps. (And by the way, \tikzstyle is considered deprecated in favor of \tikzset{stylename/.style={...}}.) – Torbjørn T. Nov 18 '16 at 13:21
  • Thanks for the suggestion. TeX insists on drawing the path from the top node in the tree in that case, though, and I can't get the path to be drawn right in the MWE of the solution to that question either – the two sigmas at the top are errorneously connected instead. – Jipí Nov 18 '16 at 13:44
  • I've just realized it works that way when compiling with pdfLaTeX, but not with XeLaTeX. I need the latter for Unicode support and everything, though. – Jipí Nov 18 '16 at 14:26
  • Actually, I just tested as well, and for me it doesn't work at all. I get ! Package tikz Error: Cannot parse this coordinate. I have an up-to-date TeX Live 2016, perhaps there have been some changes in forest in the time since that question was asked and answered. Anyway, I know next to nothing about forest, but there are certainly other users around here that know quite a lot, so there's probably a good chance of getting an answer. – Torbjørn T. Nov 18 '16 at 14:41
8

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.

node-to-node text-to-text node-to-text

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}
8
  • This indeed compiles with the desired result (thank you!), but only in pdflatex, not in xelatex :( I'll be looking further into that, e.g. tex.stackexchange.com/q/229500/60686 – Jipí Nov 19 '16 at 9:51
  • @Jipí It works with lualatex. – cfr Nov 19 '16 at 13:58
  • Did you try the workaround described in the comments? Might be worth looking up the bug report and trying to improve it. – cfr Nov 19 '16 at 14:00
  • Yes, that works. In the bug report (sourceforge.net/p/pgf/bugs/354), Stefan Müller writes that he would contact the author of tikzmark directly. Apparently nothing has happened since? – Jipí Nov 19 '16 at 16:22
  • @Jipí Isn't it a PGF bug? – cfr Nov 19 '16 at 17:00
6

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:

Compilation of MWE with workaround definition

1
  • Very useful! (+1) – cfr Nov 19 '16 at 17:06

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.