I am building a tree structure in tikz and am following this texample, which suggests adding styles for the children according to their depth in the tree (first
, second
and so on).
The purpose is that each level needs to adjust the level distance
attribute. What I would like to do is use \foreach
like so:
\foreach \x / \y in {first/1,second/2,third/3,fourth/4,fifth/5,sixth/6,seventh/7,eight/8,ninth/9}
{\tikzstyle{\x}=[level distance=\y*6 em]};
Or something of that sort, to set the distance for each level to a multiple of 6. The factor should depend on the level. I have tried to research how to use arithmetic in \foreach
and came up with \pgfmathparse{\y * 6}\pgfmathresult
to actually compute the number. So what I have is
\foreach \x / \y in {first/1,second/2,third/3,fourth/4,fifth/5,sixth/6,seventh/7,eight/8,ninth/9}
{\tikzstyle{\x}=[level distance=\pgfmathparse{\y * 6}\pgfmathresult em]};
And this compiles, but no styles are actually set. If I attempt to reference e.g. first
, I get the Error: I do not know the key '/tikz/first'
.
Maybe this has something to do with the order of macro expansion or some such magic.
Here is an MWE
\documentclass{scrartcl}
\usepackage{tikz}
\usepackage[utf8]{inputenc}
\begin{document}
\usetikzlibrary{trees,calc}
\begin{tikzpicture}[
work package/.style={draw,rectangle,text width=3cm},
grandchild/.style={grow=down,
edge from parent path={(\tikzparentnode.west) -- ++(-1em,0) |- ($(\tikzparentnode.south west) + (-1em,0)$) |- (\tikzchildnode.west)}},
first/.style={level distance=6ex},
second/.style={level distance=14ex},
third/.style={level distance=22ex},
fourth/.style={level distance=30ex},
fifth/.style={level distance=38ex},
level 1/.style={sibling distance=4cm,level distance=2cm}
]
% \foreach \x / \y in {first/1,second/2,third/3,fourth/4,fifth/5,sixth/6,seventh/7,eight/8,ninth/9}
% {\tikzstyle{\x}=[level distance=\pgfmathparse{\y * 6}\pgfmathresult em]};
% Parents
\coordinate
node[work package, text centered] {Client-Server-System für Versicherung}
[edge from parent fork down]
% Children and grandchildren
child{node[work package] {Phase 1}
child[grandchild,first] {node[work package] {A: Funktionen erarbeiten}}
child[grandchild,second] {node[work package] {B: Funktionen einteilen}}
child[grandchild,third] {node[work package] {C: Schnittstellen zu Fremdsystemen}}
child[grandchild,fourth] {node[work package] {D: Datenbankentwurf}}
child[grandchild,fifth] {node[work package] {E: GUI-Prototyp entwickeln}}
}
child{node[work package] {Phase 2}};
\end{tikzpicture}
\end{document}
\foreach
loop is executed inside a group and so outside it the styles are not set.