# TikZ tree sibling distance

I have been struggling a bit with TikZ and how to build trees. What I need now is some advice on how to improve the node placement. Right now I have these 2 trees:

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
[font=\small, edge from parent fork down,
every node/.style={top color=white, bottom color=blue!25,
rectangle,rounded corners, minimum size=6mm, draw=blue!75,
very thick, drop shadow, align=center},
edge from parent/.style={draw=blue!50,thick},
level 1/.style={sibling distance=6cm},
level 2/.style={sibling distance=2.5cm},
level 3/.style={sibling distance=2.5cm},
level distance=2cm,
]

\node {Paletizar} % root
child { node {Iniciar}
child { node {Detectar Pacote}}
child { node {Movimentar}}
}
child { node {Empacotar}
child { [sibling distance=15mm] node {Posicionar}
child { node {Pegar/Largar}}
child { node {Organizar}}
child { node {Movimentar}}
}
child { node {Agrupar}}
child { node {Padronizar}}
}
child { node {Finalizar}
child { node {Detectar estado\\da palete}}
child { node {Transmitir}}
};
\end{tikzpicture}

\vspace{2cm}

\begin{tikzpicture}
[font=\small, edge from parent fork down,
every node/.style={top color=white, bottom color=blue!25,
rectangle,rounded corners, minimum size=6mm, draw=blue!75,
very thick, drop shadow, align=center},
edge from parent/.style={draw=blue!50,thick},
level 1/.style={sibling distance=6cm},
level 2/.style={sibling distance=3cm},
level 3/.style={sibling distance=2cm},
level distance=2cm,
]

\node {Paletizador} % root
child { node {Inicialização\\de sistema}
child { node {Sensor X}}
child { node {Acção Y}}
}
child { node {Sacos}
child { node {Linha/Coluna}
child { node {Garra}}
child { node {Rodar Garra}}
child { node {Acção Y2}}
}
child { node {Euro Pallet}}
child { node {Disposição $xyz$}
child { node {$x=?$}}
child { node {$y=?$}}
child { node {$z=?$}}
}
}
child { node {Sistema de despacho}
child { node {Sensor de peso\\/contador}}
child { node {Tapete rolante\\de saída}}
};
\end{tikzpicture}

\end{document}

So basically what I want is a way to edit the code I have so that everything is perceptible which basically means that I need specific sibling distances to each child. I have tried to add a sibling distance in the child nodes but maybe it was in the wrong place or something else because it didn't change anything. I also tried to use below and all its derivations but the results were even worse because of the size of the boxes.

-
Welcome to TeX.sx! It's not necessary to sign your questions (as there is already a box with your username below it) or to begin them with a greeting. –  Torbjørn T. Nov 20 '11 at 20:34

Using tikz-qtree helps a bit with automatic spacing of siblings. It also provides a simpler input method. Here's your second tree done in that way:

\documentclass[landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[margin=1in]{geometry}
\usepackage{tikz-qtree}
\begin{document}
\tikzset{font=\small,
edge from parent fork down,
level distance=1.75cm,
every node/.style=
{top color=white,
bottom color=blue!25,
rectangle,rounded corners,
minimum height=8mm,
draw=blue!75,
very thick,
align=center,
text depth = 0pt
},
edge from parent/.style=
{draw=blue!50,
thick
}}

\centering
\begin{tikzpicture}
[.{Inicialização\\de sistema}
[.{Sensor X} ]
[.{Acção Y} ] ]
[.Sacos
[.{Linha/Coluna}
[.{Garra} ]
[.{Rodar Garra} ]
[.{Acção Y2} ] ]
[.{Euro Pallet} ]
[.{Disposição $xyz$}
[.{$x=?$} ]
[.{$y=?$} ]
[.{$z=?$} ] ] ]
[.{Sistema de despacho}
[.{Sensor de peso\\/contador} ]
[.{Tapete rolante\\de saída} ] ]
]
\end{tikzpicture}
\end{document}

-
tkiz-qtree helps a lot (I was actually learning how to use it now). However it does create a couple of blank spaces. Is there a way to compact it? Also I seem to loose some horizontal alignment, is there a way to fix that? Other than that it's perfect –  Duarte Silva Nov 20 '11 at 22:48
Do you mean the space around the "Linha/Coluna" node? That's determined by the width of its daughter nodes; I don't think there's an easy way to adjust that. As for the alignment of the "y=?" node, I have no idea why that is not aligned properly. –  Alan Munn Nov 20 '11 at 23:04
Ok, I'll leave it at that. Maybe when I have more time I'll try again. Thanks to everyone –  Duarte Silva Nov 20 '11 at 23:10
Thanks to @Jake, there's a solution to the horizontal alignment problem: I've added text depth=0pt to the node style parameters. –  Alan Munn Nov 21 '11 at 0:27

A more compact tree can be specified very concisely using the newer and powerful forest. For example:

\documentclass[a4paper,10pt,landscape]{article}
\usepackage{geometry}
\usepackage{forest}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}
\tikzset{
my node style/.style={
font=\small,
top color=white,
bottom color=blue!25,
rectangle,
rounded corners,
minimum size=6mm,
draw=blue!75,
very thick,
align=center,
}
}
\forestset{
my tree style/.style={
for tree={
my node style,
parent anchor=south,
child anchor=north,
edge={draw=blue!50, thick},
edge path={% |-| style branches from parent to child
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
},
if n children=3{% if a node has 3 children
for children={% align the middle child with its parent
if n=2{calign with current}{}
}
}{},
}
}
}
\centering

\begin{forest}
my tree style
[Paletizar
[Iniciar
[Detectar Pacote]
[Movimentar]
]
[Empacotar
[Posicionar
[Pegar/Largar]
[Organizar]
[Movimentar]
]
[Agrupar]
]
[Finalizar
[Transmitir]
]
]
\end{forest}

\vspace{2cm}

\begin{forest}
my tree style
[Inicialização\\de sistema
[Sensor X]
[Acção Y]
]
[Sacos
[Linha/Coluna
[Garra]
[Rodar Garra]
[Acção Y2]
]
[Euro Pallet]
[{Disposição $xyz$}
[{$x=?$}]
[{$y=?$}]
[{$z=?$}]
]
]
[Sistema de despacho
[Tapete rolante\\de saída]
]
]
\end{forest}

\end{document}

If you need to keep the trees in portrait and can adjust the relative alignments a little, you can do this quite easily. In this version, I also make use of the vertical space available in portrait orientation to increase the distance between levels a little. The first tree uses tier alignment using tier=<tier name> and the second uses empty nodes to adjust the alignment, setting all empty nodes to shape=coordinate by modifying the style definition.

\documentclass[a4paper,10pt]{article}
\usepackage{geometry}
\usepackage{forest}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}
\tikzset{
my node style/.style={
font=\small,
top color=white,
bottom color=blue!25,
rectangle,
rounded corners,
minimum size=6mm,
draw=blue!75,
very thick,
align=center,
}
}
\forestset{
my tree style/.style={
for tree={
parent anchor=south,
child anchor=north,
l sep+=5pt,
my node style,
edge={draw=blue!50, thick},
edge path={
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-7.5pt) -| (.child anchor)\forestoption{edge label};
},
if n children=3{
for children={
if n=2{calign with current}{}
}
}{},
delay={if content={}{shape=coordinate}{}}
}
}
}
\centering

\begin{forest}
my tree style
[Paletizar
[Iniciar
[Detectar Pacote]
[Movimentar]
]
[Empacotar
[Posicionar
[Pegar/Largar]
[Organizar]
[Movimentar]
]
[Agrupar]
]
[Finalizar, tier=A
[Transmitir]
]
]
\end{forest}

\vspace{2cm}

\begin{forest}
my tree style
[Inicialização\\de sistema
[Sensor X]
[Acção Y]
]
[Sacos
[
[
[Linha/Coluna
[Garra]
[Rodar Garra]
[Acção Y2]
]
[Euro Pallet]
[{Disposição $xyz$}
[{$x=?$}]
[{$y=?$}]
[{$z=?$}]
]
]
]
]
[Sistema de despacho
[Tapete rolante\\de saída]
]
]
\end{forest}

\end{document}

-

I am not completely sure that this is actually what you want. You can easily change your code a bit to get everything to fit though. Adding the fullpage package to ensure it fits on the page properly and slightly adjusting the sibling distances for the different levels is enough.

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usepackage{fullpage}
\begin{document}
\begin{tikzpicture}
[font=\small, edge from parent fork down,
every node/.style={top color=white, bottom color=blue!25,
rectangle,rounded corners, minimum size=6mm, draw=blue!75,
very thick, drop shadow, align=center},
edge from parent/.style={draw=blue!50,thick},
level 1/.style={sibling distance=6cm},
level 2/.style={sibling distance=2.5cm},
level 3/.style={sibling distance=2.5cm},
level distance=2cm,
]
\node {Paletizar} % root
child { node {Iniciar}
child { node {Detectar Pacote}}
child { node {Movimentar}}
}
child { node {Empacotar}
child { [sibling distance=15mm] node {Posicionar}
child { node {Pegar/Largar}}
child { node {Organizar}}
child { node {Movimentar}}
}
child { node {Agrupar}}
child { node {Padronizar}}
}
child { node {Finalizar}
child { node {Detectar estado\\da palete}}
child { node {Transmitir}}
};
\end{tikzpicture}

\vspace{2cm}
\begin{tikzpicture}
[font=\small, edge from parent fork down,
every node/.style={top color=white, bottom color=blue!25,
rectangle,rounded corners, minimum size=6mm, draw=blue!75,
very thick, drop shadow, align=center},
edge from parent/.style={draw=blue!50,thick},
level 1/.style={sibling distance=6cm},
level 2/.style={sibling distance=2.5cm},
level 3/.style={sibling distance=1.75cm},
level distance=2cm,
]
\node {Paletizador} % root
child { node {Inicialização\\de sistema}
child { node {Sensor X}}
child { node {Acção Y}}
}
child { node {Sacos}
child { node {Linha/Coluna}
child { node {Garra}}
child { node {Rodar Garra}}
child { node {Acção Y2}}
}
child { node {Euro Pallet}}
child { node {Disposição $xyz$}
child { node {$x=?$}}
child { node {$y=?$}}
child { node {$z=?$}}
}
}
child { node {Sistema de despacho}
child { node {Sensor de peso\\/contador}}
child { node {Tapete rolante\\de saída}}
};
\end{tikzpicture}
\end{document}

You might want to look into some other package, like qtree to draw your trees, if the nodes don't need to look as fancy and you want some automatic placement. If you do want the fancyness of TikZ, there is also tikz-qtree.

-
Since qtree is designed solely for linguistic trees, it has few options for changing the branch shape, so for these kinds of trees, tikz-qtree is a better choice. –  Alan Munn Nov 20 '11 at 21:16