# TikZ: How to define the level spacing between node children?

In this MWE, is there anyway to automatically set the spacing between the second parent children instead of this manual approach

first/.style={level distance=6ex},
second/.style={level distance=12ex},
third/.style={level distance=18ex},


?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning,calc,trees}

\begin{document}
\pagestyle{empty}
\def\gap{10mm}

\begin{tikzpicture}[
node distance = 18mm and \textwidth/3,
on grid,
font = \sffamily,
line/.style = {draw, -Latex,black},
rbox/.style = {draw=blue!80!black, fill=blue!20,
rounded corners, inner sep=2mm,
text width=(\textwidth-6*\gap)/3, align=center},
grandchild/.style={grow=down,xshift=+2em,anchor=west,edge from parent path={ [line] ([xshift=0,yshift=0em]\tikzparentnode.south) |- (\tikzchildnode.west)}},
first/.style={level distance=6ex},
second/.style={level distance=12ex},
third/.style={level distance=18ex},
]

\node (Grandparent)   [rbox] {Grandparent};

\node (1stparent)  [rbox,below  left=of Grandparent] {1st parent};

\node (2ndparent)  [rbox,below=of Grandparent]{2nd parent}
child[grandchild,first] {node[rbox]{1st child}}
child[grandchild,second] {node[rbox]{2nd child}}
child[grandchild,third] {node[rbox]{3rd child}};

\node (3rdparent)  [rbox,below right=of Grandparent] {3rd parent};

\path [line] (Grandparent) -- coordinate (a) (2ndparent);
\path [line] (a) -| (1stparent);
\path [line] (a) -| (3rdparent);
\end{tikzpicture}

\end{document}


## Edit

Following cfr answer forest approach, how can I edit this code to make the children at level 3 (e.g. He-Ne, I, CO2, etc) be folder-like?

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\begin{forest}
forked edges,
for tree={
draw=blue!80!darkgray,
fill=blue!80!darkgray!25,
rounded corners,
text width=(\textwidth-6*\gap)/3,
edge={-Latex},
font=\sffamily,
text centered,
},
%  where level=0{%
parent anchor=children,
%  }{%
if level=2{%
folder,
grow'=0,
l sep+=10pt,
before typesetting nodes={child anchor=north},
edge path'={%
(!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
},
}{},
%  }
[Active Media
[Gases
[Atoms
[He-Ne]
[I]
]
[Molecules
[CO2]
[CO]
]
[Ions
[Ar]
[Kr]
]
[Excimers
[ArF]
]
]
[Liquids
[Dyes
[Rhodamines]
]
]
[Solids
[Insulators
[Nd:YAG]
]
[Semiconductors
[GaAs]
]
]
]
\end{forest}
\end{document}


• I don't get the same output as you. Some lines are missing. – cfr Oct 26 '16 at 22:45
• @cfr I fixed it. – Diaa Oct 26 '16 at 22:59
• – cfr Oct 26 '16 at 23:18

If you are happy to use Forest, the package will layout the tree automatically for you. The folder option from the edges library makes this much easier with Forest version 2 than version 1. However, there is a slight glitch in that it is not possible to combine folder with forked edges easily. So, for the first edges, we set the edge path manually and override child anchor.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\begin{forest}
forked edges,
for tree={
draw=blue!80!darkgray,
fill=blue!80!darkgray!25,
rounded corners,
text width=(\textwidth-6*\gap)/3,
edge={-Latex},
font=\sffamily,
text centered,
},
where level=0{%
parent anchor=children,
}{%
folder,
grow'=0,
l sep+=10pt,
if level=1{%
before typesetting nodes={child anchor=north},
edge path'={%
(!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
},
}{},
}
[Grandparent
[1\textsuperscript{st} parent
]
[2\textsuperscript{nd} parent
[1\textsuperscript{st} child]
[2\textsuperscript{nd} child]
[3\textsuperscript{rd} child]
]
[3\textsuperscript{rd} parent
]
]
\end{forest}
\end{document}


# EDIT

For the tree added in the edited question, you might adjust the code as follows.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\begin{forest}
forked edges,
for tree={
draw=blue!80!darkgray,
fill=blue!80!darkgray!25,
rounded corners,
minimum width=(\textwidth-6*\gap)/3,
minimum height=4ex,
edge={-Latex},
font=\sffamily,
text centered,
},
where={level()<=1}{%
parent anchor=children,
}{%
folder,
grow'=0,
l sep+=5pt,
if level=2{%
before typesetting nodes={child anchor=north},
!u.s sep'+=10pt,
edge path'={%
(!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
},
}{},
}
[Active Media
[Gases
[Atoms
[He-Ne]
[I]
]
[Molecules
[CO2]
[CO]
]
[Ions
[Ar]
[Kr]
]
[Excimers
[ArF]
]
]
[Liquids
[Dyes
[Rhodamines]
]
]
[Solids
[Insulators
[Nd:YAG]
]
[Semiconductors
[GaAs]
]
]
]
\end{forest}
\end{document}


Note that the key level here is the one before the one where siblings are stacked vertically. If that is level 3, the crucial level is level 2. Everything before that level gets styled in one way, everything after gets styled another. Nodes at that level need some tweaking.

If you say

if level=2 ...


in the tree's preamble, it will have no effect as it is only applied to the root node, where the conditional fails. Likewise, options put here simpliciter only apply to the root. This is the difference between if and where: where issues for tree; if does not. See the manual for details and further options.

• When I tried to extend your answer, I failed to get the folder-like level as desired. I would be grateful if you could take a look at my question edit. – Diaa Oct 27 '16 at 7:27
• @DiaaAbidou Please see edit above. You should probably take a look at the manual if you have not yet done so e.g. the difference between if and where is important and so is the use of for tree etc. – cfr Oct 27 '16 at 22:58
• Many thanks for your clarification. I would be thankful if you could help me with another issue I couldn't figure out in this question. – Diaa Oct 28 '16 at 16:08