# In a tree, how do you have a node that is a also a tree?

In general, what is a good way to have a tree where nodes are also a tree?

Here is a particular image that I'd like to produce in latex:

What is notable is that the left tree has two nodes that are themselves trees, and the right tree has one. These nodes also have a bracket around them. If you replace these nodes with, say, a letter or math object, it's straightforward to use either the forest package or tikz. In the forest's manual, it looks like there is support for having a node be a table, among other things, but I had no success having a node be a forest object.

Here is an example document of the left tree where tree nodes are replaced with A and B. First with forest, then tikzpicture.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usepackage{forest}

\begin{document}
\begin{forest}
[A
[{$(s_L,b_L)$}]
[B
[{$(s_R,b_R)$}]
]
]
\end{forest}
\begin{tikzpicture}
\node {A}
child {node {$(s_L,b_L)$} }
child {node {B}
child {node {$(s_R,b_R)$} }
};
\end{tikzpicture}
\end{document}


Approach taken: embedding tikz. Notice that the scale paramater had to be modified in the embedded nodes. Also, I was unsuccessful getting brackets around the tree-nodes and instead opted for a box.

\begin{tikzpicture}
\node[draw] { \input{temp1.tikz} }
child {node (a) {$(s_L,b_L)$} }
child {node[draw] { \input{temp2.tikz} }
child {node {$(s_R,b_R)$} }
};
\end{tikzpicture}

% temp1.tikz
\begin{tikzpicture}[scale=0.4]
\node {$K_1$}
child {node {$s_L$} }
child {node {$s_R$} };
\end{tikzpicture}

% temp2.tikz
\begin{tikzpicture}[scale=0.4]
\node {$K_2$}
child {node {$s_R$} };
\end{tikzpicture}


• A very rough way would be to include a tikzpicture containing your tree into the first node. Did you try this already? Feb 10, 2021 at 18:06
• Welcome to TeX.se! Since it's clear from your question that you've played around with this using forest, could you edit your question to include a compilable document with a sample tree for people to play with. Thanks. Feb 10, 2021 at 18:06
• @SebGlav It's generally not a good idea to embed tikzpictures Feb 10, 2021 at 18:07
• @AlanMunn, see edit. Feb 10, 2021 at 18:30
• @SebGlav, thanks for the suggestion, see edit. It is a rather rough way. To get it to work, I'd have to figure out scaling of the nodes. Perhaps with editing the outer image to have longer edges, a smaller scale could be used in the temp images. Or are there other things to mess with? Feb 10, 2021 at 19:11

Here's a way using \savebox. Thanks to Ulrike Fischer for discussion in chat about math baselines. I've put each of the tree subnodes as a separate save box. These can be reassigned for subsequent trees.

\documentclass{article}
\usepackage[linguistics]{forest}
\forestset{math tree/.style={for tree=math content},
saved tree/.style={math tree,baseline, anchor=center}}
\newsavebox{\Ki}
\newsavebox{\Kii}
\savebox{\Ki}{$\left[\raisebox{.5\depth}{\begin{forest},saved tree[K_1 [s_L ] [s_R ]]\end{forest}}\right]$}
\savebox{\Kii}{$\left[\raisebox{.5\depth}{\begin{forest},saved tree[K_2 [s_R ]]\end{forest}}\right]$}

\begin{document}

\begin{forest}math tree
[\usebox{\Ki}
[{(s_L,b_L)}]
[\usebox{\Kii}
[{(s_R,b_R)}]
]
]
\end{forest}
\end{document}


With the code above, the nodes are aligned at their centres, which leads to uneven branches. If you would like them aligned uniformly at the top, you can dispense with the saved tree style altogether, and just add anchor=north to the math tree style:

\documentclass{article}
\usepackage[linguistics]{forest}
\forestset{math tree/.style={for tree={math content,anchor=north}}}
\newsavebox{\Ki}
\newsavebox{\Kii}
\savebox{\Ki}{$\left[\raisebox{.5\depth}{\begin{forest},math tree[K_1 [s_L ] [s_R ]]\end{forest}}\right]$}
\savebox{\Kii}{$\left[\raisebox{.5\depth}{\begin{forest},math tree[K_2 [s_R ]]\end{forest}}\right]$}

\begin{document}

\begin{forest}math tree
[\usebox{\Ki}
[{(s_L,b_L)}]
[\usebox{\Kii}
[{(s_R,b_R)}]
]
]
\end{forest}
\end{document}


• Cool! Yeap, that does it. It was straight forward to produce the right tree as well. Feb 10, 2021 at 20:04