0

I have the following code producing a graph with the graphdrawing algorithms of tikz and sublayouts to properly set my nodes.

\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}

It produces following figure:

enter image description here

Now I want the "left" nodes to form a properly aligned group as well as the "right" nodes, namely the plus node should be centered under the left1 and left2 nodes and respectively for center and right nodes. Also, the left3 node should be aligned with the left1 node and so should left4 be aligned with left2.

Can someone help me with this? I tried playing around with more sublayouts, but it ended up just being heavily unreadable and I wasn't sure if I go in the right direction.


EDIT:

Here I have tried another approach using sublayouts to structure the nodes, but then the nodes are set in a quiet weird way, because I thought the algorithm tries to avoid crossing lines.

\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}

The resulting graph looks like this:

enter image description here

Maybe this addition helps to understand, why I am not sure about the way tikz sets the nodes.

  • It is either automatic positioning by graphdrawing or manual position and fine tuning. – Henri Menke Sep 27 '18 at 10:30
  • Yes, but with sublayouts it should be possible to create a sublayout of for instance the left nodes which is positioned like desired, however it doesn't work for me. – schilli Sep 27 '18 at 10:34
  • @HenriMenke: I have now posted a solution to my problem, which uses automatic positioning of graphdrawing with sublayouts to align the nodes as desired and without the hassle of manual positioning and fine tuning. – schilli Oct 21 '18 at 18:06
2

I was able to figure out a solution on my own while working on more graphs with tikz. It is in fact possible to use sub layouts to manage the alignment and ordering of the graph.

The sub layout just need to be treated like a standalone graph and contain all nodes and edges within this sublayout. Subsequently, edges from the parent graphs can be drawn using the nodes of the sublayout. The alignment of nodes within a sublayout follows the selected layout.

To achieve proper alignment of the whole graph, the size of the nodes/sublayouts matter.

The following code produces attached graph.

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.