# Creating tree of trees in TikZ

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:

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.

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

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}


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