0

I'm trying to draw rooted trees in TeX. I found the following example:

\begin{tikzpicture}
  \node {root}
    child {[fill] circle (2pt)}
    child {[fill] circle (2pt)};
\end{tikzpicture}

My question is : How to fill the root too ? The following doesn't work :

\begin{tikzpicture}
  \node {[fill] circle (2pt)}
    child {[fill] circle (2pt)}
    child {[fill] circle (2pt)};
\end{tikzpicture}

I'm trying to do it manually, i.e. by

\begin{tikzpicture}
  \node {$\bullet$}
    child {[fill] circle (2pt)}
    child {[fill] circle (2pt)};
\end{tikzpicture}

but it gives a break between root and edge.

How to do it in a simple way (without additional packages)?

1
  • Please always provide a complete, minimal example rather than mere fragments of code as a complete document we can copy-paste-compile to reproduce the problem is much more helpful!
    – cfr
    May 1, 2016 at 22:19

2 Answers 2

2

You're confusing a node and a simple shape. They can look similar, but behave differently.

In order to fill the root, which is a node, you need to give it before the curly brackets, which are meant to contain the text, not options.

\begin{tikzpicture}
  \node[fill, circle] {}
    child {[fill] circle (2pt)}
    child {[fill] circle (2pt)};
\end{tikzpicture}

However, I think forest will let you draw trees more easily. It's as easy as writing square brackets!

Output

enter image description here

Code

\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}

\begin{document}
  \begin{forest}
    for tree={
        circle,
        draw,
        fill,
        minimum width=2pt, % size
        inner sep=0pt,
        parent anchor=center,
        child anchor=center,
        s sep+=25pt, % distance between children
    }
[ 
    [ ]
    [ ]
]
\end{forest}
\end{document}
4
  • Ok, but it this case the circle on the root is bigger than other. Second question : How to implement [grow=up] in this case ?
    – mikis
    May 1, 2016 at 17:22
  • @mikis see edit. you can add grow=north to the options if you want the tree to go up.
    – Alenanno
    May 1, 2016 at 17:32
  • (+1) for Forest. But shouldn't the children in the non-Forest code also be altered somehow?
    – cfr
    May 1, 2016 at 22:11
  • @cfr Sorry for the delayed answer! I was intending to do that, but I see you already did it. :D
    – Alenanno
    May 2, 2016 at 16:49
4

Like Alenanno, I would recommend Forest but, if that's not an option, here's a non-Forest no-extra-packages-required solution, albeit one which requires more typing than Forest's syntax.

\documentclass[border=10pt,tikz,multi]{standalone}
\begin{document}
\begin{tikzpicture}
  [
    my circle/.style={draw, fill, circle, minimum size=2pt, inner sep=0pt}
  ]
  \node [my circle] {}
    child { node [my circle] {}}
    child { node [my circle] {}};
\end{tikzpicture}
\end{document}

not a Forest tree

You must log in to answer this question.

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