# TikZ, tree, some childs without arrows

I wanted to create a tree structure and I'm almost done. Note that it is my first "project" with TikZ. My Problem: The childs of the first and part of the second level aren't connected with arrows. Can anyone help me? I don't see a difference compared to the particular last level.

\begin{tikzpicture}[->,>=stealth',
edge from parent/.style={thick,draw=black!70,-latex},
level 1/.style={sibling distance=8.5cm},
,level distance =3.5cm,auto]

\tikzstyle{and}=[rectangle, rounded corners,thick,draw=black!75,top color=blue!10, bottom color=white,minimum size=5mm]
\tikzstyle{or}=[rectangle,rounded corners,thick,draw=black!75,top color=red!10, bottom color=white,minimum size=5mm]
\tikzstyle{terminal}=[rectangle, rounded corners, thick,draw=black!75, top color=green!5, bottom color=white,minimum size=5mm]
\tikzstyle{relation}=[rectangle,rounded corners,inner sep=2pt, thick,draw=black!75, fill=gray!20,minimum size=5mm, font =\scriptsize]

\tikzstyle{every node}=[font=\footnotesize]

\node[and]  {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
child { [sibling distance = 2cm] node [and] (appr) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
child{ node [terminal] (car) {2} }
child{ node [relation] (rel) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }
child{ node [terminal] (zebra) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }  }
child { [sibling distance = 6cm] node [or] (obstacle)  {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
child{ [sibling distance = 2cm]  node [and] (ped_on_zebra) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
child{ node [terminal] (ped) {1} }
child{ node [relation] (rel_1) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }
child{  node [terminal] (zebra_2) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }  }
child { [sibling distance = 2cm]  node [and] (dog_on_zebra)  {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
child{ node [terminal] (dog) {3} }
child{ node [relation] (rel_2) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }
child{ node [terminal] (zebra_1) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  } }}
;

%Draw Relations
\path [dashed,-]
(zebra) edge node [left] {} (rel)
(rel) edge node [right] {} (car)

(rel_1) edge node [left] {} (ped)
(rel_1) edge node [right] {} (zebra_2)

(rel_2) edge node [left] {} (dog)
(rel_2) edge node [right] {} (zebra_1);

%create legend
\node [relation] (terminal)at (7,-2) {};
\node [right=0.2cm of terminal] {= Relation};
\node [terminal] (terminal)at (7,-1) {};
\node [right=0.2cm of terminal] {= Terminal-Node};
\node [and] (and) at (7,0) {};
\node [right=0.2cm of and] {= And-Node};
\node [or] (or) at (7,1) {} ;
\node [right=0.2cm of or] {= Or-Node};

\end{tikzpicture}

• I would post a picture but, I have not enough reputations. – Stefan Jul 9 '13 at 7:13
• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. – texenthusiast Jul 9 '13 at 7:17

The arrows are there but hidden behind the nodes.

The reason for this is wrong syntactical input. If there isn’t a node at the first place in a child TikZ doesn’t see a node as a child but whatever there is (you could put arbitrary stuff in there). Specify the sibling distance directly after the node (which is the parent of the siblings of which you specify the distance).

I took the liberty to clean-up the code a little bit and changes the process of the legend, it uses the chains library (which in turn uses the positioning library too).

By the way, tree nodes get named automatically, so there maybe a more automatic way to connect the last children, but that’s another story.

I haven’t added the child anchor=north option to the first level at it looks a little bit weird to me, but you might change that. In fact, you could just add child anchor=north to TikZ picture instead of the level styles.

## Code

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{arrows,chains}
\begin{document}
\begin{tikzpicture}[
->,
>=stealth',
edge from parent/.style={thick,draw=black!70,-latex},
level 1/.style={sibling distance=8.5cm},
level 2/.style={child anchor=north},
level 3/.style={child anchor=north,sibling distance=2cm},
level distance=3.5cm,
auto,
tree nodes/.style={
shape=rectangle,
rounded corners,
thick,
draw=black!75,
minimum size=+5mm
},
and/.style     ={tree nodes, top color=blue!10, bottom color=white},
or/.style      ={tree nodes, top color=red!10,  bottom color=white},
terminal/.style={tree nodes, top color=green!5, bottom color=white},
relation/.style={tree nodes, inner sep=2pt, fill=gray!20, font=\scriptsize},
font=\footnotesize,
every node/.append style={align=center}
]
\node              [and]                     {1 \\ 2 \\ 3}
child {     node [and]      (appr)         {1 \\ 2 \\ 3}
[sibling distance = 2cm]
child {   node [terminal] (car)          {2}          }
child {   node [relation] (rel)          {1 \\ 2 \\ 3}}
child {   node [terminal] (zebra)        {1 \\ 2 \\ 3}}
}
child {     node [or]       (obstacle)     {1 \\ 2 \\ 3}
[sibling distance = 6cm]
child {   node [and]      (ped_on_zebra) {1 \\ 2 \\ 3}
child { node [terminal] (ped)          {1}          }
child { node [relation] (rel_1)        {1 \\ 2 \\ 3}}
child { node [terminal] (zebra_2)      {1 \\ 2 \\ 3}}
}
child   { node [and]      (dog_on_zebra) {1 \\ 2 \\ 3}
child { node [terminal] (dog)          {3}          }
child { node [relation] (rel_2)        {1 \\ 2 \\ 3}}
child { node [terminal] (zebra_1)      {1 \\ 2 \\ 3}}
}
};

\path [dashed,-]
(rel) edge (zebra)
edge (car)

(rel_1) edge (ped)
edge (zebra_2)

(rel_2) edge (dog)
edge (zebra_1);

\begin{scope}[shift={(7,1)}, node distance=+1cm, on grid, label position=right, start chain=ch going above]
\foreach \sStyle/\tText in {relation/Relation, terminal/Terminal-Node, and/And-Node, or/Or-Node}
\node[
on chain=ch,
\sStyle,
label={= \tText}
] {};
\end{scope}
\end{tikzpicture}
\end{document}


## Output

The arrows are there, but hidden behind the nodes. Shorten the edges or change the anchor, so that it's not the center, such as by node [and,anchor=north east]:

\node[and]  {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
child { [sibling distance = 2cm] node [and,anchor=north east] (appr)
{\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
child{ node [terminal] (car) {2} }
child{ node [relation] (rel) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }
child{ node [terminal] (zebra) {\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}  }  }
child { [sibling distance = 6cm] node [or,anchor=north west] (obstacle)
{\begin{tabular}{l} 1 \\ 2 \\ 3\end{tabular}}
...


• Tanks, works great! – Stefan Jul 9 '13 at 8:30
• Now there occured another proble, if I use [anchor = north] (becaus I want the arrow to target at the top), the lowest level is not on the same level anymore, because I did not use anchor=north. but if i use anchor=north, the nodes are not centered anymore, cause the tip of the arrows are all on the same height – Stefan Jul 9 '13 at 9:01
• @Stefan Perhaps try to isolate the problem in a new and possible even smaller example and post it as a new separate question, as the original issue of missing arrow tips is solved. This site doesn't work with chronologic threads, it's pure: one specific question, some answers. Another specific question (can be related, add a link), other answers to that one. – Stefan Kottwitz Jul 9 '13 at 9:37
• I did open a new Question, thanks.tex.stackexchange.com/questions/123244/… – Stefan Jul 9 '13 at 10:52
• @Stefan The [sibling distance=<dim>] is at the wrong position syntactically. Position it after the child or after the first node (aka in front of the siblings) Then the edge will be connected with the node and not simply the child coordinate. – Qrrbrbirlbel Jul 9 '13 at 11:30