# Draw a graph using graphdrawing and sublayouts, wherein nodes are properly aligned among each other

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]
\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] (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(z) edge[->] (out);
\end{tikzpicture}
\end{document}


It produces following figure:

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}

\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] (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(z) edge[->] (out);
\end{tikzpicture}
\end{document}


The resulting graph looks like this:

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

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_0) {left3};
\node[draw] (A_1) {left4};

\end{scope}

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

\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_t_0) {right1};
\node[draw] (C_t_1) {right2};

\end{scope}
\end{scope}

\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);