How can I draw a circle around an entire tree in forest?

For some reason that I cannot discern, using tikz={\node [draw,circle,fit to tree]{};} to draw a circle around nodes in a tree works as expected if the nodes are embedded in a larger tree.

However, this does not work if you want to draw a circle around the entire tree:

How can I draw a circle around an entire tree with forest?

MWE

\documentclass{article}
\usepackage{forest}
\forestset{sn edges/.style={for tree={parent anchor=south, child anchor=north}}}
\begin{document}
\begin{forest} baseline, sn edges
[w
[x]
[y, s sep=20pt
[z]
[a,tikz={\node [draw,circle,fit to tree]{};}
[b]
[c,circle,draw]
]
]
]
\end{forest}
\vspace{5em}
\begin{forest} baseline, sn edges
[a,tikz={\node [draw,circle,fit to tree]{};}
[b]
[c,circle,draw]
]
\end{forest}
\end{document}

• This is a bug, and one can get it also in situations other than the entire tree; for example, set grow=0 for nodes w and y. I'll try to fix this asap. Dec 2 '14 at 7:36
• @SašoŽivanović Sounds great. Thanks! While I have your attention, do you have any idea what's going on in this question? :P Dec 2 '14 at 15:06
• Nope ;-( But I'll add it to my todo list as well! Dec 2 '14 at 16:14
• @SašoŽivanović Sorry to make more work for you! :P Dec 2 '14 at 19:21
• @SašoŽivanović There's an answer that might help you debug and provide some insight about what's going on. It seems that changing fonts affects what's going on, oddly enough. Dec 9 '14 at 5:33

One option is to name some special \nodes (the root and the lowest(s) one(s)) in the tree and then use the fit library with a circular \node:

The example code:

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{fit}

\begin{document}

\begin{forest}
[a,name=root
[b
[c
[d,name=lowestl]
[e]
]
[f ]
]
[g
[h]
[i
[j]
[k,name=lowestr]
]
]
]
\node[draw,circle,fit={(root) (lowestl) (lowestr)}] {};
\end{forest}

\end{document}


You can redefine the tree drawing stage to save the tree in a box and then draw the tree in a circled node.

What is nice about this is that you can define a style for this once and then just apply it to trees without having to think about their layout in order to figure out which nodes to name.

\usepackage{forest}
\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north}},
circle tree/.style={
draw tree stage/.style={
for root'={
draw tree box=0,
draw tree,
TeX={\tikz{\node [draw, circle] {\box0};}},
}}}}
\begin{document}
\begin{forest} baseline, sn edges, circle tree
[w
[x]
[y, s sep=20pt
[z]
[a
[b]
[c,circle,draw]
]
]
]
\end{forest}
\vspace{5em}
\begin{forest} baseline, sn edges, circle tree
[a
[b]
[c,circle,draw]
]
\end{forest}
\end{document}