I would like to draw a DAG (directed acyclic graph) that grows on the right, where every node is spaced from the other nodes at a fixed size, but some time nodes can be missing, so I would like to still account for that space:

For example, the distance between F and D should be the same distance from A and D.

enter image description here

At the same time, I would like to plot behind it, help lines that help understand how they are correctly spaced

enter image description here

In the first example I only found a way to specify where exactly the nodes should be (while instead it would be ideal for them to autoposition)

level distance=200mm,
sibling distance=105mm]
    \node[shape=circle,draw=black] (A) at (0, 0){A};
    \node[shape=circle,draw=black] (B) at (1,-1) {B};
    \node[shape=circle,draw=black] (C) at (1,1) {C};
    \node[shape=circle,draw=black] (D) at (2,0) {D};
    \node[shape=circle,draw=black] (F) at (4,0) {F} ;

    \path [<-](A) edge node[right] {} (B);
    \path [<-](A) edge node[right] {} (C);
    \path [<-](B) edge node[right] {} (D);
    \path [<-](C) edge node[right] {} (D); 
    \path [<-](D) edge node[right] {} (F); 

In the second case I only found a way to construct trees - that I cannot really space out

level distance=20mm,
sibling distance=10mm,
every node/.style={draw=black,border=1mm,circle,inner sep=5pt}
\draw [help lines, dashed,ystep=5, xstep=2] (0,0) grid(15, 5);
\node {0}
child { node {1} }
    child { node {2}
      child { node {3}
        child { node {4} }
        child { node {5} }
        child { node {6} } }
      child { node {7} } };

Is there a way to achieve what I want to do?

  • Please always post compilable code rather than fragments. – cfr Apr 19 '17 at 1:13

Relevant code elements:

  • The library positioning allows you to position nodes relative to other nodes using above, below etc. The distance is controlled with the option node distance. To place the centers of the nodes at a fixed distance, you need the option on grid (otherwise the distance is measured between the borders of the nodes).

  • To make the nodes cover the grid in the background, use fill=white with the nodes.

enter image description here

   node distance=2cm,
   on grid,
  \draw[gray!50] (-1,-2) grid (5,2);
  \node[vertex] (A) {A};
  \node[vertex,above right=1cm and 1cm of A] (B) {B};
  \node[vertex,below=of B] (C) {C};
  \node[vertex,right=of A] (D) {D};
  \node[vertex,right=of D] (F) {F};
    (C) edge (A)
    (B) edge (A)
    (D) edge (C)
    (D) edge (B)
    (F) edge (D);
| improve this answer | |
  • what if on the second layer (where B and C are), I want to add a third node? it doesnt really autoposition – graphtheory92 Apr 18 '17 at 18:40
  • @graphtheory92 None of the nodes is autopositioned. right=of A has a strict meaning, namely 2cm to the right, as specified by node distance and on grid. Where do you want to have the third node? Tikz supports also automatic arrangement of nodes (using the abilities of luatex), see chapter 26 of the Tikz manual. For drawing trees use one of the tree packages. For large arbitrary graphs, e.g. generated by a program, use automatic placement. For small to medium sized non-trees relative positioning by hand is superior, imho. – gernot Apr 18 '17 at 18:54
  • @graphtheory92 TikZ's graph-drawing facilities sound like the most obvious solution, given your desiderata. But it does depend a bit on how much it matters that things end up where precisely you would position them, since an algorithm is unlikely to always agree with your choices (unless you write it and probably not even then). – cfr Apr 19 '17 at 1:12

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.