6

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:

enter image description here

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}
Adam Liter
  • 12,567
  • 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. – Sašo Živanović Dec 02 '14 at 07: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 – Adam Liter Dec 02 '14 at 15:06
  • Nope ;-( But I'll add it to my todo list as well! – Sašo Živanović Dec 02 '14 at 16:14
  • @SašoŽivanović Sorry to make more work for you! :P – Adam Liter Dec 02 '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. – Adam Liter Dec 09 '14 at 05:33
  • Found it! I forgot to save&restore pgf's transformation matrix in some calculation, so, in your example, essentially x and y coordinates got switched. However, as a major new version (1.1) should be out very soon, I don't think it makes much sense for me to publish a bugfix version, especially as there are workarounds for the problem. Unless you really want it, of course! – Sašo Živanović Dec 19 '14 at 14:18
  • @SašoŽivanović It's not a big deal to me if it's not fixed in the next release. Do you want to add an answer either now or when you do fix it in a future release, though? – Adam Liter Dec 19 '14 at 16:22

2 Answers2

7

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:

enter image description here

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}
Gonzalo Medina
  • 505,128
4

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}

circled trees

cfr
  • 198,882