12

I want to modify the distance between a parent node and a child node when drawing a tree using TiKZ package. I looked at the example at TiKZ site, and add a property called sibling distance. However, no matter where I added (parent and child), it points to a wrong place.

enter image description here

As we can see, if I add more children to node 2, it will overlap with 5. Adding a sibling distance yield even a worse result:

enter image description here

And I couldn't figure out what caused this issue. Any idea? Thank you.

Minimal Example

\documentclass[a4paper,landscape]{scrartcl}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[auto]
    \node [circle,draw] (z){0}
        child { [sibling distance=10mm]
            node[circle,draw] (a) {1}
            child { 
                node[circle,draw] (c) {5} 
            }
            child { 
                node[circle,draw] (d) {5} 
            }   
            child { 
                node[circle,draw] (e) {5} 
            }   
            child { 
                node[circle,draw] (f) {5} 
            }   
        }
        child {
            node[circle,draw] (b) {2}
        }
        ;

    \path (z) -- (a);
    \path (z) -- (b);
    \path (a) -- (c);
    \end{tikzpicture}
\end{document}
4
  • sibling distance sets the distance between siblings. I think you are looking for level distance.
    – Seamus
    Commented Aug 15, 2011 at 11:44
  • I'm not sure exactly what you want. Maybe you could clarify your question? Does adding e.g. [sibling distance=50mm] to the end of the line \node [circle,draw] (z){0} achieve what you want?
    – N.N.
    Commented Aug 15, 2011 at 11:48
  • @Seamus: I set level distance but it was still the same. Thank you though.
    – roxrook
    Commented Aug 15, 2011 at 12:01
  • @Jake: Thanks for pointing that out. Edited.
    – roxrook
    Commented Aug 15, 2011 at 12:28

3 Answers 3

12

You can change the sibling distance for each level of the tree individually, using level <number>/.style={sibling distance=<value>}:

\documentclass[a4paper,landscape]{scrartcl}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
    auto,
    level 1/.style={sibling distance=40mm},
    level 2/.style={sibling distance=10mm}]
    \node [circle,draw] (z){0}
        child {
            node[circle,draw] (a) {1}
            child { 
                node[circle,draw] (c) {5} 
            }
            child { 
                node[circle,draw] (d) {5} 
            }   
            child { 
                node[circle,draw] (e) {5} 
            }   
            child { 
                node[circle,draw] (f) {5} 
            }   
        }
        child {
            node[circle,draw] (b) {2}
        }
        ;
    \end{tikzpicture}
\end{document}
0
13

Rather than setting this all manually, you can use tikz-qtree to construct these kinds of trees very easily, and it will adjust the sibling distance automatically. (I've enlarged the default sibling distance slightly; but no other manual changes are required.)

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
 \tikzset{edge from parent/.style=
     {draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}}}
\begin{tikzpicture}[every tree node/.style={draw,circle},sibling distance=.25cm]
\matrix{
\Tree
 [.0 
    [.1 5 5 5 5  ] 
    [.2 ]
 ]
 &
 \Tree [.0 
    [.1 5 5 5 5 5 5 ] 
    [.2 6 6 ]
 ]
\\};
\end{tikzpicture}

\end{document}

output of code

1
  • 1
    I'm amazed how powerful TiKZ is. Thanks a lot for your elegant solution.
    – roxrook
    Commented Aug 15, 2011 at 17:00
1

With the istgame package, you can use \xtdistance{<lev-dist>}{<sib-dist>} to change level distances and sibling distances.

enter image description here

\documentclass{standalone}

\usepackage{istgame}

\begin{document}   

\begin{istgame}
\setistOvalNodeStyle{5mm}
\xtShowEndPoints[oval node]
\xtdistance{10mm}{20mm}
\istrooto(0){0}
  \istb
  \istb{}{2}[center] 
  \endist
\xtdistance{10mm}{7mm}
\istrooto(1)(0-1){1}
  \istb{}{5}[center] 
  \istb{}{5}[center] 
  \istb{}{5}[center] 
  \istb{}{5}[center] 
  \endist
\end{istgame}

\end{document}

You must log in to answer this question.

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