2

I found the solution to draw single-colored rooted trees (Butcher trees as used in numerical analysis) in Butcher Trees in TikZ

I would like to adapt this to colored trees with white and black nodes. So far, I am using the following adaptation of the code from the link above.

\documentclass{standalone}

% Butcher trees, cf. https://tex.stackexchange.com/questions/283343/butcher-trees-in-tikz
\usepackage{forest}
\forestset{
  */.style={
    delay+={append={[]},}
  },
  rooted tree/.style={
    for tree={
      grow'=90,
      parent anchor=center,
      child anchor=center,
      s sep=2.5pt,
      if level=0{
        baseline
      }{},
      delay={
        if content={*}{
          content=,
          append={[]}
        }{}
      }
    },
    before typesetting nodes={
      for tree={
        circle,
        fill,
        minimum width=3pt,
        inner sep=0pt,
        child anchor=center,
      },
    },
    before computing xy={
      for tree={
        l=5pt,
      }
    }
  },
  white node/.style={
    circle, fill=white, draw=black, thick,
  },
}
\DeclareDocumentCommand\rootedtree{o}{\Forest{rooted tree [#1]}}

\begin{document}

\rootedtree[]

\rootedtree[[]]

\rootedtree[.,white node]

\rootedtree[.,white node[]]

\rootedtree[[.,white node]]

\end{document}

This gives

enter image description here

I have two questions:

  1. How can I get rid of the small black dots in the "white nodes"?
  2. How can I let the edges end at the outer circles?

2 Answers 2

3

Here is another option. Define a \NewDocumentCommand called rootedtree as you did, but the nodes are either . or o for the filled or open nodes. Some examples:

enter image description here

are created with the code

\rootedtree[.]\rootedtree[.[.]]\rootedtree[o]\rootedtree[o[.]]\rootedtree[.[o][o, colornode]]

There is an additional option for a filled node with a different color, namely [o, colornode=<color>], with the default set as red.

\documentclass{article}

\usepackage{forest}
\forestset{
    whitenode/.style={draw, circle, minimum size=2mm, inner sep=0pt},
    blacknode/.style={draw, fill=black, circle, minimum size=2mm, inner sep=0pt},
    colornode/.style={draw, fill=#1, circle, minimum size=2mm, inner sep=0pt},
    colornode/.default={red}
}

\newcommand{\blank}{\rule{0pt}{0pt}}
\NewDocumentCommand\rootedtree{o}{\begin{forest}
    for tree={grow'=90, very thick, edge=very thick, l sep=2mm, l=2mm},
    delay={where content={}{for children={no edge, before drawing tree={for tree={y-=5mm}}}}
    {where content={o}{content={\blank}, whitenode}{where content={.}{content={\blank}, blacknode}{}}}}
    [#1]
\end{forest}}

\begin{document}

\rootedtree[.]\qquad
\rootedtree[.[.]]\qquad
\rootedtree[o]\qquad
\rootedtree[o[.]]\qquad
\rootedtree[.[o][o, colornode]]\qquad
\rootedtree[[.][.][.[.][.[.][.][.]]][.][.[.][.][.[.][.]]][.[.]]]

\end{document}

It is also possible to have a forest as a single rooted tree—just leave the root node empty.

enter image description here

\rootedtree[[.][.][.[.][.[.][.][.]]][.][.[.][.][.[.][.]]][.[.]]]
4
  • 1
    This is the way to go to have a systematic drawing of nodes with fixed size. Nice and clean. Jan 31 at 8:53
  • Thank you very much! This looks really good. I just have a follow-up question: How can I specify a colornode with another color, not the default one?
    – Hendrik
    Feb 2 at 4:42
  • 1
    [o, colornode=green] or any color.
    – Sandy G
    Feb 2 at 5:00
  • Great, thanks a lot!
    – Hendrik
    Feb 2 at 6:47
4

You are specifying center as parent/child node anchors and this determines how nodes are connected. From the forest manual:

The parent anchor and child anchor options tell where the parent’s and child’s endpoint of the edge between them should be, respectively

Furthermore, you are using as "trick" to hide the node content by filling it in black (by default). However, in white node, you fill in white and the content is then visible again. Since your content is . you see what you call small black dots.

So, to your questions:

  1. Keeping the "hacky" style, you can add use white text in your white node style.
  2. Specify north and south anchors, plus a zero outer separation.

Here your MWE modified:

\documentclass{standalone}
\usepackage{forest}
\forestset{
    */.style={
        delay+={append={[]},}
    },
    rooted tree/.style={
        for tree={
            grow'=90,
            parent anchor=north,
            child anchor=south,
            s sep=2.5pt,
            if level=0{
                baseline
            }{},
            delay={
                if content={*}{
                    content=,
                    append={[]}
                }{}
            }
        },
        before typesetting nodes={
            for tree={
                circle,
                fill,
                minimum width=3pt,
                inner sep=0pt,
                outer sep=0pt,
            },
        },
        before computing xy={
            for tree={
                l=5pt,
            }
        }
    },
    white node/.style={
        circle, fill=white, draw=black, thick, text=white
    },
    old white node/.style={% For demonstration purposes
        circle, fill=white, draw=black, thick
    }
}
\DeclareDocumentCommand\rootedtree{o}{\Forest{rooted tree [#1]}}
\begin{document}
    \rootedtree[]
    
    \rootedtree[[]]
    
    \rootedtree[.,white node]
    
    \rootedtree[.,white node[]]
    
    \rootedtree[[.,white node]]
    
    \rootedtree[[x,old white node]] % For demonstration purposes
\end{document}
MWE

The right-most tree is just to concretely show what was happening with the small black dots.

By the way, your approach is producing nodes with different dimensions, because you do specify a minimum size, but this is smaller than what gets determined by having a . as node content. Finally, you do not generally draw the contour of nodes, but you do it for white nodes.

1
  • Thanks you very much!
    – Hendrik
    Feb 2 at 4:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .