使用图形绘制和子布局绘制图形,其中节点彼此正确对齐

使用图形绘制和子布局绘制图形,其中节点彼此正确对齐

我有以下代码,使用 tikz 和子布局的图形绘制算法生成图形,以正确设置我的节点。

\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) {left1};
          \node[draw] (A_t_1) {left2};
          \node[draw] (B_0) {center1};
          \node[draw] (B_1) {center2};
          \node[draw] (C_t_0) {right1};
          \node[draw] (C_t_1) {right2};
        \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) {left3};
          \node[draw] (A_1) {left4};
          \node[draw] (C_0) {right3};
          \node[draw] (C_1) {right4};
        \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{tikzpicture}
\end{document}

它产生下图:

在此处输入图片描述

现在我希望“左”节点和“右”节点形成正确对齐的组,即加号节点应分别位于和left1节点left2和右节点下方的中心。此外,节点left3应与left1节点对齐,因此应left4与对齐left2

有人能帮我吗?我尝试使用更多的子布局,但最终却变得难以阅读,我不确定我是否朝着正确的方向前进。


编辑:

在这里,我尝试了另一种方法,即使用子布局来构造节点,但是节点的设置方式却很奇怪,因为我认为算法会尝试避免交叉线。

\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) {left1};
          \node[draw] (A_t_1) {left2};
          \node[draw] (B_0) {center1};
          \node[draw] (B_1) {center2};
          \node[draw] (C_t_0) {right1};
          \node[draw] (C_t_1) {right2};
        \end{scope}

        \node[draw] (A_add) {+};
        \node[draw] (B_add) {+};
        \node[draw] (C_add) {+};

        \begin{scope}[tree layout]
          \node[draw] (A_0) {left3};
          \node[draw] (A_1) {left4};
        \end{scope}

        \begin{scope}[tree layout]
          \node[draw] (C_0) {right3};
          \node[draw] (C_1) {right4};
        \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{tikzpicture}
\end{document}

生成的图表如下所示:

在此处输入图片描述

也许这个补充有助于理解为什么我不确定 tikz 设置节点的方式。

答案1

在使用 tikz 处理更多图表时,我能够自己找到解决方案。实际上,可以使用子布局来管理图表的对齐和排序。

子布局只需被视为独立图,并包含此子布局内的所有节点和边。随后,可以使用子布局的节点绘制父图中的边。子布局内的节点对齐遵循所选布局。

为了实现整个图的正确对齐,节点/子布局的大小很重要。

以下代码生成附图。

\documentclass{article}
\usepackage{standalone,tikz}
%\standaloneconfig{convert}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs, backgrounds, fit, calc, shapes.misc}
\usegdlibrary{trees, layered}
\begin{document}
\begin{tikzpicture}[layered layout, level sep=0.4cm, minimum width=2cm, minimum height=0.6cm]
        \node[draw, level sep=1.1cm] (in) {};

        \begin{scope}[layered layout]
            \begin{scope}[layered layout]
              \node[draw] (A_t_0) {left1};
              \node[draw] (A_t_1) {left2};
              \node[draw] (A_add) {+};
              \node[draw] (A_0) {left3};
              \node[draw] (A_1) {left4};

              \draw (A_t_0) edge [->] (A_add);
              \draw (A_t_1) edge [->] (A_add);
              \draw (A_add) edge [->] (A_0);
              \draw (A_add) edge [->] (A_1);
            \end{scope}

            \begin{scope}[layered layout]
              \node[draw] (B_add) {+};
              \node[draw] (B_0) {center1};
              \node[draw] (B_1) {center2};
              \node[] (B_help) {};

              \draw (B_0) edge [->] (B_add);
              \draw (B_1) edge [->] (B_add);
              \draw (B_add) edge [--, shorten >= -1cm] (B_help);
            \end{scope}

            \begin{scope}[layered layout]
              \node[draw] (C_0) {right3};
              \node[draw] (C_1) {right4};
              \node[draw] (C_add) {+};
              \node[draw] (C_t_0) {right1};
              \node[draw] (C_t_1) {right2};

              \draw (C_t_0) edge [->] (C_add);
              \draw (C_t_1) edge [->] (C_add);
              \draw (C_add) edge [->] (C_0);
              \draw (C_add) edge [->] (C_1);
            \end{scope}
        \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_0) edge [->] (add);
        \draw (A_1) edge [->] (add);
        \draw (B_help) edge [->] (add);
        \draw (C_0) edge [->] (add);
        \draw (C_1) edge [->] (add);

        \draw(add) edge[->] (z);
        \draw(z) edge[->] (out);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容