我有以下带有子布局的代码示例,可以正确设置图形中的节点。问题是我似乎找不到添加适合所有内部节点的背景节点的方法。
\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs, backgrounds, fit, positioning, calc}
\usegdlibrary{trees, layered}
\begin{document}
\begin{tikzpicture}[layered layout, level sep=0.4cm]
\node[draw, level sep=1.1cm] (in) {};
\begin{scope}[tree layout]
\node[draw] (A_t_0) {a};
\node[draw] (A_t_1) {a};
\node[draw] (B_0) {b};
\node[draw] (B_1) {b};
\node[draw] (C_t_0) {c};
\node[draw] (C_t_1) {c};
\end{scope}
\begin{scope}[tree layout]
\node[draw] (A_add) {+};
\node[draw] (B_add) {+};
\node[draw] (C_add) {+};
\end{scope}
\begin{scope}[tree layout]
\node[draw] (A_0) {a};
\node[draw] (A_1) {a};
\node[draw] (C_0) {c};
\node[draw] (C_1) {c};
\end{scope}
\node[draw] (add) {+};
\node[draw] (z) {Z};
\node[draw] (out) {};
\begin{scope}[on background layer]
\node[fill=red!30, fit=(A_t_0) (C_t_1) (z), inner sep=5pt] {};
\end{scope}
\draw (in) edge [->] (A_t_0);
\draw (in) edge [->] (A_t_1);
\draw (in) edge [->] (B_0);
\draw (in) edge [->] (B_1);
\draw (in) edge [->] (C_t_0);
\draw (in) edge [->] (C_t_1);
\draw (A_t_0) edge [->] (A_add);
\draw (A_t_1) edge [->] (A_add);
\draw (B_0) edge [->] (B_add);
\draw (B_1) edge [->] (B_add);
\draw (C_t_0) edge [->] (C_add);
\draw (C_t_1) edge [->] (C_add);
\draw (A_add) edge [->] (A_0);
\draw (A_add) edge [->] (A_1);
\draw (C_add) edge [->] (C_0);
\draw (C_add) edge [->] (C_1);
\draw (A_0) edge [->] (add);
\draw (A_1) edge [->] (add);
\draw (B_add) edge [->, bend left=0] (add);
\draw (C_0) edge [->] (add);
\draw (C_1) edge [->] (add);
\draw(add) edge[->] (z);
\draw(z) edge[->] (out);
\end{tikzpicture}
\end{document}
代码生成以下图像。请注意,小红色矩形是背景节点,它应该适合我的图表的内部节点。
我还尝试了另一种方法(见下文),即使用tikz 中的remember picture
和overlay
选项。这似乎可以正确计算矩形的位置和大小,但它没有放到背景层。我尝试了该方法,并使用了on background layer
以下代码示例中的选项,还尝试了\pgfdeclarelayer
、\pgfsetlayers
命令,但也没有成功。
\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs, backgrounds, fit, positioning, calc}
\usegdlibrary{trees, layered}
\begin{document}
\begin{tikzpicture}[remember picture, layered layout, level sep=0.4cm]
\node[draw, level sep=1.1cm] (in) {};
\begin{scope}[tree layout]
\node[draw] (A_t_0) {a};
\node[draw] (A_t_1) {a};
\node[draw] (B_0) {b};
\node[draw] (B_1) {b};
\node[draw] (C_t_0) {c};
\node[draw] (C_t_1) {c};
\end{scope}
\begin{scope}[tree layout]
\node[draw] (A_add) {+};
\node[draw] (B_add) {+};
\node[draw] (C_add) {+};
\end{scope}
\begin{scope}[tree layout]
\node[draw] (A_0) {a};
\node[draw] (A_1) {a};
\node[draw] (C_0) {c};
\node[draw] (C_1) {c};
\end{scope}
\node[draw] (add) {+};
\node[draw] (z) {Z};
\node[draw] (out) {};
% \begin{scope}[on background layer]
% \node[fill=red!30, fit=(z), inner sep=5pt] {};
% \end{scope}
\draw (in) edge [->] (A_t_0);
\draw (in) edge [->] (A_t_1);
\draw (in) edge [->] (B_0);
\draw (in) edge [->] (B_1);
\draw (in) edge [->] (C_t_0);
\draw (in) edge [->] (C_t_1);
\draw (A_t_0) edge [->] (A_add);
\draw (A_t_1) edge [->] (A_add);
\draw (B_0) edge [->] (B_add);
\draw (B_1) edge [->] (B_add);
\draw (C_t_0) edge [->] (C_add);
\draw (C_t_1) edge [->] (C_add);
\draw (A_add) edge [->] (A_0);
\draw (A_add) edge [->] (A_1);
\draw (C_add) edge [->] (C_0);
\draw (C_add) edge [->] (C_1);
\draw (A_0) edge [->] (add);
\draw (A_1) edge [->] (add);
\draw (B_add) edge [->, bend left=0] (add);
\draw (C_0) edge [->] (add);
\draw (C_1) edge [->] (add);
\draw(add) edge[->] (z);
\draw(z) edge[->] (out);
\end{tikzpicture}
\begin{tikzpicture}[remember picture, overlay]
\begin{scope}[on background layer]
\node[fill=red!30, fit=(A_t_0) (C_t_1) (z), inner sep=5pt] {};
\end{scope}
\end{tikzpicture}
\end{document}
我真的很想知道为什么第一种方法无法计算大小和位置。我的猜测是,添加背景时,图形绘制算法没有设置节点。
我希望有人能帮助我找到一种合适的方法来添加背景并将其添加到背景层。
答案1
欢迎!使用范围。这样你就可以确定算法是“完成的”(或者“做得很好”?;-)。
\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs, backgrounds, fit, positioning, calc}
\usegdlibrary{trees, layered}
\begin{document}
\begin{tikzpicture}
\begin{scope}[layered layout, level sep=0.4cm]
\node[draw, level sep=1.1cm] (in) {};
\begin{scope}[tree layout]
\node[draw] (A_t_0) {a};
\node[draw] (A_t_1) {a};
\node[draw] (B_0) {b};
\node[draw] (B_1) {b};
\node[draw] (C_t_0) {c};
\node[draw] (C_t_1) {c};
\end{scope}
\begin{scope}[tree layout]
\node[draw] (A_add) {+};
\node[draw] (B_add) {+};
\node[draw] (C_add) {+};
\end{scope}
\begin{scope}[tree layout]
\node[draw] (A_0) {a};
\node[draw] (A_1) {a};
\node[draw] (C_0) {c};
\node[draw] (C_1) {c};
\end{scope}
\node[draw] (add) {+};
\node[draw] (z) {Z};
\node[draw] (out) {};
\draw (in) edge [->] (A_t_0);
\draw (in) edge [->] (A_t_1);
\draw (in) edge [->] (B_0);
\draw (in) edge [->] (B_1);
\draw (in) edge [->] (C_t_0);
\draw (in) edge [->] (C_t_1);
\draw (A_t_0) edge [->] (A_add);
\draw (A_t_1) edge [->] (A_add);
\draw (B_0) edge [->] (B_add);
\draw (B_1) edge [->] (B_add);
\draw (C_t_0) edge [->] (C_add);
\draw (C_t_1) edge [->] (C_add);
\draw (A_add) edge [->] (A_0);
\draw (A_add) edge [->] (A_1);
\draw (C_add) edge [->] (C_0);
\draw (C_add) edge [->] (C_1);
\draw (A_0) edge [->] (add);
\draw (A_1) edge [->] (add);
\draw (B_add) edge [->, bend left=0] (add);
\draw (C_0) edge [->] (add);
\draw (C_1) edge [->] (add);
\draw(add) edge[->] (z);
\draw(z) edge[->] (out);
\end{scope}
\begin{scope}[on background layer]
\node[fill=red!30, fit=(A_t_0) (C_t_1) (z), inner sep=5pt] {};
\end{scope}
\end{tikzpicture}
\end{document}