5

I have problems with remembering the coordinates of names defined in nested tikz pictures. Look at the following minimal working example:

\documentclass{article}
\usepackage{tikz-qtree}
\newcommand{\inlinenode}[2]{
There are\\
several line\\
breaks\\
#2\tikz[remember picture]\coordinate (#1);\\
and #1 is not\\
in the center}
\begin{document}
\tikzset{every tree node/.style={align=center,anchor=north}}
\begin{tikzpicture}
\Tree [.{Root}
        [.\node{\inlinenode{x1}{$x_1$}}; ]
        [.\node (x2) {$x_2$}; ]
      ]
\path[thick,color=red] (x1) edge (x2);
\end{tikzpicture}
\end{document}

output of my minimal working example

I want to connect x1 with x2 using the red line, which works fine for x2, but the other end is pointing in a weird place instead of to x1.

I have tried running pdflatex multiple times, but then the position of x1 gets even weirder.

What I want to achieve should be visible in the picture above, I want a multiline node with a special word/graphic inside the text and then connecting this word to another node (or even another special word inside another multiline text of a node). Feel free to modify my approach.

I also tried replacing the #2\tikz...\coordinate(#1) with \subnode{#1}{#2} (as in this similar question), but this has the same result.

  • Nesting tikzpictures is a Bad Idea. This sort of thing is what subnode is for since it is meant to define things as if there were nesting but without doing the actual nesting. However, tikz-qtree actually does some nesting internally and this may be messing with how tikzmark and subnode work. If no-one comes up with a better solution in the meantime, I'll do some digging and see if I can figure it out. – Andrew Stacey Jan 13 '14 at 7:57
3

In tikz-qtree, each child is actually a separate pgfpicture. This means that when drawing between children in the way that you do, you are drawing between two different pgfpictures and so both need to have the remember picture key applied.

However, even then there are issues with the fact that you are nesting a tikzpicture inside the pgfpicture (which is itself nested in the main tikzpicture) and so just putting remember picture on the outermost tikzpicture does not work. But this is exactly what the \subnode command from the tikzmark library was designed to deal with: it defines a subnode of a node without the rigmarole of setting up a whole new tikzpicture.

So using \subnode and remember picture we get:

\documentclass{article}
%\url{http://tex.stackexchange.com/q/153756/86}
\usepackage{tikz-qtree}
\usetikzlibrary{tikzmark}

\newcommand{\inlinenode}[2]{
There are\\
several line\\
breaks\\
\subnode{#1}{#2} \\
and #1 is not\\
in the center}
\begin{document}
\tikzset{every tree node/.style={align=center,anchor=north}}
\begin{tikzpicture}[remember picture]
\Tree [.{Root}
        [.\node {\inlinenode{x1}{$x_1$}}; ]
        [.\node (x2) {$x_2$}; ]
      ]
\path[thick,color=red] (x1) edge (x2);
\end{tikzpicture}

\end{document}

tikz-qtree with subnode

|improve this answer|||||

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.