看完之后TikZ 中的“Z 级别”我尝试在我的森林图中实现 z 层,但没有成功:
\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}
目标是在它们穿过的森林节点后面绘制虚线箭头。要实现这一点需要进行哪些更改?
答案1
将内容放入背景的最简单方法是使用图层,background
而不是定义其他图层。如果您需要更复杂的分层,则需要使用\pgfdeclarelayer
和,\pgfsetlayers
如您链接的答案中所述。background
但是,对于图层,这在您加载库时已经完成backgrounds
。
为了演示效果,我在节点中添加了 75% 不透明度的淡蓝色填充,以便背景线可以显示在树的前层:
\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}