Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to draw a tree and I don't know how to enforce the spacing between them. Some are overlapping.

This is my code:

\begin{tikzpicture}
\tikzstyle{every node}=[circle,draw]
\node {A}
child { node {B} 
child { node {E} }
child { node {F} child { node {K} } child { node {L} } child { node {M}}}
child { node {G} }}
child { node {C} 
child {node {H}}}
child { node {D} child {node {I}} child {node {J} child {node {N}} child {node {O}}}};
\end{tikzpicture}

The nodes G and H are overlapping.

I've tried using the parameter node distance but it doesn't work.

share|improve this question

3 Answers 3

up vote 17 down vote accepted

Refer to Section 18.4 Specifying Options for Trees and Children of the pgfmanual. Using sibling distance, you can solve the problem (use the values that best suit your needs):

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[every node/.style={circle,draw},level 1/.style={sibling distance=30mm},level 2/.style={sibling distance=10mm}
]
\node {A}
child { node {B} 
child { node {E} }
child { node {F} child { node {K} } child { node {L} } child { node {M}}}
child { node {G} }}
child { node {C} 
child {node {H}}}
child { node {D} child {node {I}} child {node {J} child {node {N}} child {node {O}}}}
;
\end{tikzpicture}

\end{document}

share|improve this answer
    
Thank you very much! –  andrasi May 29 '11 at 18:19

In case you want to only move a single node you can also use:

child{node [left=1.5cm] {b}

To move the node 1.5cm to the left.

share|improve this answer

If you don't want to manually adjust the spacing for each tree, you could use the tikz-qtree package. This is aimed at linguistics but can be used in other cases. (The spacing commands below are setting the minimum distances, not the actual distances. The package automatically guarantees that nodes won't overlap with a pretty straightforward layout algorithm that you can probably infer from this example.)

\documentclass{article}

\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}

\begin{tikzpicture}[every tree node/.style={draw,circle},sibling
distance=10pt, level distance=40pt]
\tikzset{edge from parent/.style={draw, edge from parent path=
    {(\tikzparentnode) -- (\tikzchildnode)}}}
\Tree [.A [.B E [.F K L M ] G ] [.C H ] [.D I [.J N O ] ] ]
\end{tikzpicture}

\end{document}

Version with lines going from south to north anchors:

original image

Edit: I'm finally at the right computer to update this example to show how to deal with @Alan's question. I left the old image for comparison above (to get this, remove the \tikzset command from the above code), but here's the one with lines emerging from the center of the nodes:

updated image

share|improve this answer
1  
One thing that bothers some people with this tree is that the branches extend from the bottom of the circles rather than the implicit centre. (As in the tree in the other answer.) I couldn't figure out a way to change that with tikz-qtree. –  Alan Munn May 30 '11 at 21:24
2  
@Alan, you can reset to the default by doing the following: \tikzset{edge from parent/.style={draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}}} –  kgr May 30 '11 at 22:08
    
Thanks. I'll add this to a previous answer of mine that produced a similar tree. –  Alan Munn May 30 '11 at 22:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.