# Proper syntax for z-levels inside Forest

After reading "Z-level" in TikZ I tried to implement z-layers in my Forest chart, but unsuccessfully:

\documentclass{standalone}
\usepackage{forest}
\usetikzlibrary{backgrounds}

\tikzset{zlevel/.style={%
execute at begin scope={\pgfonlayer{#1}},
execute at end scope={\endpgfonlayer}
}}

\begin{document}

\begin{forest}
for tree={
draw=black, align=center, l sep=4ex, parent anchor=south, child anchor=north,
node options={font=\footnotesize, minimum width=14em, minimum height=10ex, rounded corners=1ex},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-2ex) -| (.child anchor)\forestoption{edge label};
}
}
[Parent,name=Parent
[SubParent,name=SubParent
[Child1,name=Child1
[Child11]
[Child12,name=Child12]
]
[Child2
[Child21]
[Child22,name=Child22,node options={dashed}]
[Child23,name=Child23]
]
[Child3
[Child31,name=Child31]
[Child32]
]
]
]
%
\tikzset{every node/.style={font=\footnotesize, draw=green, minimum width=14em, minimum height=10ex, rounded corners=1ex}}
%
\begin{scope}[zlevel=1]
\draw[->,dashed] (Child22) to (SubParent);
\draw[->,dashed] (Child31) to[out=north east,in=south east] (SubParent);
\end{scope}
\end{forest}
\end{document}


The goal is to draw the dashed arrows behind the forest nodes they are crossing. What changes would be required to achieve that?

• Do you just want \begin{scope}[on background layer]... \end{scope}? If you need more layers, you can define them but, as the explanation you linked explains, you have to declare and set them first. – cfr Jul 7 '14 at 22:08

The easiest way to put stuff in the background is to use the background layer rather than defining additional layers. If you need more complex layering, you need to use \pgfdeclarelayer and \pgfsetlayers as mentioned in the answer you linked to. For the background layer, however, this is already done when you load the backgrounds library.

To demonstrate the effect, I've added a pale blue fill to your nodes at 75% opacity so that the background lines show through the front layer of the tree:

\documentclass{standalone}
\usepackage{forest}
\usetikzlibrary{backgrounds}

\begin{document}

\begin{forest}
for tree={
draw=black, align=center, l sep=4ex, parent anchor=south, child anchor=north,
node options={font=\footnotesize, minimum width=14em, minimum height=10ex, rounded corners=1ex},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-2ex) -| (.child anchor)\forestoption{edge label};
},
fill=blue!25,
fill opacity=.75,
}
[Parent,name=Parent
[SubParent,name=SubParent
[Child1,name=Child1
[Child11]
[Child12,name=Child12]
]
[Child2
[Child21]
[Child22,name=Child22,node options={dashed}]
[Child23,name=Child23]
]
[Child3
[Child31,name=Child31]
[Child32]
]
]
]
%
\tikzset{every node/.style={font=\footnotesize, draw=green, minimum width=14em, minimum height=10ex, rounded corners=1ex}}
%
\begin{scope}[on background layer]
\draw[->,dashed] (Child22) to (SubParent);
\draw[->,dashed] (Child31) to[out=north east,in=south east] (SubParent);
\end{scope}
\end{forest}
\end{document}


• Oddly enough, I cannot reproduce the semi-transparent fill that works fine in your example. Asked the new question here: tex.stackexchange.com/q/191107/37570 – ajeh Jul 11 '14 at 18:45
• @ajeh Is your installation of TeX up to date? Which distribution are you using? – cfr Jul 11 '14 at 20:37
• It's a current MikTex 2.9 – ajeh Jul 11 '14 at 21:01
• @ajeh I see the other question has shown the issue is not related to an outdated version of tikz in any case so hopefully you'll get an answer there after refining your question. – cfr Jul 11 '14 at 21:38
• @ajeh Did this solution answer your (this) question? If so, please consider accepting it so that others can see the question has an answer. (Disclaimer: I will gain points if you do so. Bonus: So will you.) – cfr Nov 16 '14 at 0:59