I have a script that takes some input data and outputs tikz-code that creates a visual representation of this data in the form of a node graph. The ouput of the current script using example data looks as follow at the moment:




    \begin{tikzpicture}[every node/.style={{inner sep=0.7em}}]
        \node at (6em, 0em) (L1N1) [anchor=north, text width=4em]{\begin{center}N1\end{center}};
        \node at (0em, -9em) (L2N1) [anchor=north, text width=4em]{\begin{center}N1\end{center}};
        \node at (6em, -9em) (L2N2) [anchor=north, text width=4em]{\begin{center}N2\end{center}};
        \node at (12em, -9em) (L2N3) [anchor=north, text width=4em]{\begin{center}N3\\L1\\L2\\L3\\L4\\L5\\L6 \end{center}};
        \node at (0em, -18em) (L3N1) [anchor=north, text width=4em]{\begin{center}N1\end{center}};
        \node at (12em, -18em) (L3N2) [anchor=north, text width=4em]{\begin{center}N2\end{center}};
        \node at (6em, -27em) (L4N1) [anchor=north, text width=4em]{\begin{center}N1\end{center}};
        \path [-] (L1N1) edge (6em, -7.0em);
        \path [-] (6em, -7.0em) edge (6em, -7.0em);
        \path [->] (6em, -7.0em) edge (L2N2);
        \path [-] (L2N1) edge (0em, -16.0em);
        \path [-] (0em, -16.0em) edge (0em, -16.0em);
        \path [-] (L2N2) edge (6em, -16.0em);
        \path [-] (6em, -16.0em) edge (12em, -16.0em);
        \path [-] (L2N3) edge (12em, -16.0em);
        \path [-] (12em, -16.0em) edge (12em, -16.0em);
        \path [->] (0em, -16.0em) edge (L3N1);
        \path [->] (12em, -16.0em) edge (L3N2);
        \path [->] (12em, -16.0em) edge (L3N2);
        \path [-] (L3N1) edge (0em, -25.0em);
        \path [-] (0em, -25.0em) edge (6em, -25.0em);
        \path [-] (L3N2) edge (12em, -25.0em);
        \path [-] (12em, -25.0em) edge (6em, -25.0em);
        \path [->] (6em, -25.0em) edge (L4N1);
        \path [->] (6em, -25.0em) edge (L4N1);


The generated graph looks like this:

Graph Exmaple

The image illustrates the problem I have. As can be seen from the graph, I visualize it as several rows of Nodes with connections between nodes in different rows. Currently my node positions are computed entirely in the script but I do think that I need to go away from this, because I have a problem with not knowing the size of the nodes and the rightmost node in the second row is far too tall for the coordinates given, making the result a mess.

Is it possible to compute the Y-coordinate of each row automatically in tikz (as an offset from the bottom of the previous row)? I do believe I can use the "calc" tikzlibrary to get the bottom-most Y-coordinate of a given node but I have not been able to find a way to get the bottom-most Y-coordinate of several Nodes. Is there a way to do that or is there another approach I should take to solve this problem?

Here is a rough sketch on how I would like the end result to look, with the third line being offset based on the lower most point of the nodes of the second line:

Desired result sketch

I cannot generate the final representation of the graph externally, as I will need to put page-references both from and to the contents of some of the nodes.

Thank you for any help!

  • 1
    could you upload a handdrawn sketch of your expected result – js bibra Jun 28 '20 at 12:44
  • In what language is the script that generates this tikz code written? In my opinion, it's the logic of your script that needs to be reviewed because the node text should be anchored to the south and not to the north as you do to get the result you want. But this then changes the graph. So it's difficult to answer you without knowing how this code is created and what are the possibilities of your script. – AndréC Jun 28 '20 at 14:58

Edit: Some remarks:

  • in my proposition I follow to your MWE, which is quite close to desired result
  • main differences between them are use of the relative positioning (by employ positioning library) of nodes and added two auxiliary coordinates which are served for node connections and positions
  • for nodes are used common style defined as every node
  • number commands for arrows are reduced to only to shown on images (five)
\documentclass[tikz, margin=3mm]{standalone}

    node distance = 4mm and 12mm,
every node/.style = {minimum size=1.2em, align=center, outer sep=0pt},
\node (L1N1)    {N1};
\node (L2N1)    [below  left=of L1N1]   {N1};
\node (L2N2)    [below=of L1N1]         {N2};
\node (L2N3)    [below right=of L1N1]   {N3\\L1\\L2\\L3\\L4\\L5\\L6 };
\coordinate[below=of L2N3] (aux1);
\node (L3N1)    [below=of L2N1 |- aux1] {N1};
\node (L3N2)    [below=of aux1]         {N2};
\coordinate[below=of L1N1 |- L3N1.south]    (aux2);
\node (L4N1)    [below=of aux2]   {N1};
\draw[->]   (L1N1) -- (L2N2);
\draw[->]   (L2N1) -- (L3N1);
\draw[->]   (L2N3) -- (L3N2);
\draw[->]   (aux2) -- (L4N1);
\draw       (L2N2) |- (aux1);
\draw       (L3N1) |- (aux2) -| (L3N2);

enter image description here

  • This solution assumes that I know which node of a given row will be the largest, am I assuming correctly? None the less I may be able to heuristically estimate which one is correct so thank you for this suggestion! – OnePie Jun 28 '20 at 18:16
  • @OnePie,see added explanation to my answer. If you liked answer, you my consider to accept them :-) – Zarko Jun 28 '20 at 20:51

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.