# Forest: Exclude subtree from packing algorithm

Consider the following code:

\documentclass[12pt, border=2mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={}
[A
[B
[C ]
[C ]
]
[1[2][3]]
[B
[C ]
[C ]
]
]
\end{forest}
\end{document}


Is it possible to

ignore the nodes containing numbers to draw the first part of the tree and then append the "subtree" containing the numbers to the root.

in a way that the output is like this:

EDIT My goal is to put a small 'unimportant' tree into a bigger one. I was only able to make the 'unimportant' tree small enough by changing the y coordinates before the tree is drawn. But this leaves some emty space in the tree:

\documentclass[12pt, border=2mm]{standalone}
\usepackage{forest}
\forestset{
unimportant/.style={
fit=rectangle,
for tree={
delay={content/.process={Ow {content}{\tiny ##1}}},
},
before drawing tree={
for tree={
delay={
inner sep=2pt,
typeset node,
y+/.process={Ow+n {level}{##1*20}},
},
},
},
},
}

\begin{document}
\begin{forest}
for tree={}
[A
[B
[C [D][D]]
[C [D][D]]
]
[1, unimportant[2[3][4]]]
[B
[C [D][D]]
[C [D][D]]
]
]
\end{forest}
\end{document}


Whereas I want to achieve this:

• Why do you want that overlapping of 2 and C / 3 and C? Or do you want to make a cycle in a tree? Jul 25, 2017 at 17:52
• @dexteritas I want to put a "tiny" tree into a bigger tree (see above). Jul 25, 2017 at 18:17
• I'd add the sub-tree after the main tree is packed.
– cfr
Jul 25, 2017 at 18:50
• @cfr how would you achieve that? It does not seem to be possible usinginsert after={[1[2[3][4]]]}. Jul 25, 2017 at 19:21
• @Imperaton No. I'd remove the sub-tree while the rest is packed and the xy values are computed. Then put it back. You could delay it and use something like insert after. It was just the remove and replace strategy I had in mind. That way the sub-tree is specified but it isn't part of the tree when the tree is packed.
– cfr
Jul 26, 2017 at 0:22

If you don't mind naming the node, it is relatively easy. It is perfectly possible otherwise, but a bit less straightforward.

\documentclass[border=10pt,12pt]{standalone}
\usepackage{forest}
\forestset{
unimportant/.style={
name=#1,
for tree={
font=\tiny,
},
before packing={pack, remove},
!u.before drawing tree={
append=#1,
delay={
for nodewalk={name=#1}{for tree={l*=.5, inner sep=2pt, delay=compute xy, delay n=2{y-/.option=l}}},
}
},
},
}

\begin{document}
\begin{forest}
for tree={}
[A
[B
[C [D][D]]
[C [D][D]]
]
[1, unimportant=this one [2[3][4]]]
[B
[C [D][D]]
[C [D][D]]
]
]
\end{forest}
\end{document}


• Your forest-fu always impresses me. :) Jul 25, 2017 at 19:55
• @AlanMunn The manipulation of y feels a bit hacksh. But I couldn't find another way.
– cfr
Jul 26, 2017 at 0:23

This question interests me. Here is my try to draw this tree using the istgame package.

\documentclass{standalone}

\usepackage{istgame}

\begin{document}

\begin{istgame}
% bigger tree
\tikzset{oval node/.style={ellipse node,draw=none}}
\xtdistance{12mm}{40mm}
\istrooto(0){A} \istb \istb \endist
\foreach \n in {1,2}
{
\xtdistance{12mm}{20mm}
\istrooto(B\n)(0-\n){B} \istb \istb \endist
\xtdistance{12mm}{10mm}
\istrooto()(B\n-1){C} \istb{}{\mbox{D}} \istb{}{\mbox{D}} \endist
\istrooto()(B\n-2){C} \istb{}{\mbox{D}} \istb{}{\mbox{D}} \endist
}
% tiny tree
\begin{scope}[font=\scriptsize]
\tikzset{oval node/.style={ellipse node,draw=none,minimum size=5pt,inner sep=0pt,outer sep=1.5pt}}
\xtdistance{3mm}{4mm}
\istrooto(00)([yshift=-2mm]0)[plain node] \istb \endist
\istrooto(1)(00-1){1} \istb<level distance=4mm> \endist
\istrooto(2)(1-1){2} \istb{}{3\ }[below,yshift=3pt] \istb{}{\ 4}[below,yshift=3pt] \endist
\end{scope}
\end{istgame}
\end{document}