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.

geodude
  • 123

1 Answers1

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