1

I would like to create a tree in TikZ whose nodes are trees themselves.

I have the following piece of code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree} 

\tikzset{% 
    every picture/.append style={
      grow'=up,
      sibling distance=.5em
    }
}

\begin{document}

\begin{tikzpicture}[baseline=-.3em]
  \node[draw] {
    \begin{tikzpicture}
      \Tree 
      [. $+$ 
      \node[draw]{
      \begin{tikzpicture}
       \Tree
       [. $+$ [. $+$ $a$ $b$ ]
           [. $+$ $c$ $d$ ] ]
      \end{tikzpicture}
      }; 
      \node[draw]{
      \begin{tikzpicture}
       \Tree
       [. $+$ $e$
           $f$ ]
      \end{tikzpicture}
      }; 
      ]
    \end{tikzpicture}
  };
\end{tikzpicture}

\end{document}

The problem is that the nodes of the outer tree are not connected right:

enter image description here

I think the reason is that qtree somehow "still sees" the inner nodes. Is there a way to let TikZ "unsee" the inner nodes? Something like the opposite of "remember picture"?

I can always export the inner nodes as a separate pdf and then reinclude it, but I was looking for a more elegant option, if possible.

  • 2
    You are nesting tikzpictures which should be avoided. – user121799 Feb 22 at 15:20
2

You are nesting tikzpictures, which should be avoided. Here you could store the inner tikzpictures in \saveboxes.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree} 

\tikzset{% 
    every picture/.append style={
      grow'=up,
      sibling distance=.5em
    }
}
\newsavebox\TreeA
\newsavebox\TreeB
\begin{document}
\savebox\TreeA{\begin{tikzpicture}
       \Tree
       [. $+$ [. $+$ $a$ $b$ ]
           [. $+$ $c$ $d$ ] ]
      \end{tikzpicture}
}
\savebox\TreeB{\begin{tikzpicture}
       \Tree
       [. $+$ $e$
           $f$ ]
      \end{tikzpicture}
}
\begin{tikzpicture}[baseline=-.3em]
  \node[draw] {
    \begin{tikzpicture}
      \Tree 
      [. $+$ 
      \node[draw]{\usebox\TreeA}; 
      \node[draw]{\usebox\TreeB}; 
      ]
    \end{tikzpicture}
  };
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Thank you so much. This is exactly what I needed. – geodude Feb 22 at 16:43
  • If nesting them should be avoided, why are you still nesting them? – cfr Oct 5 at 22:46

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.