2

I am trying to add an edge between two nodes but getting unexpected outcome.

I would like to add a path between nodes g, h, and i in a similar way that has been added between a, b, and c. However, when I try to do this I get an unexpected (to me) outcome -- the path goes backwards and not towards node h: the resultant lines that are produced are shown in red. It seems I have misunderstood the syntax; how can I correct it please?

enter image description here

code:

\documentclass[tikz]{standalone}    
\usetikzlibrary{calc}    
\begin{document}    
\begin{tikzpicture}[node distance = 3cm, auto]    
     \node (a) {a};
     \node [above right of=a, xshift=2cm] (b) {b};
     \node [below right of=a, xshift=2cm] (c) {c};
     \node [right of=b] (d) {d};
     \node [above right of=d, yshift=-1cm] (e) {e};
     \node [below right of=d, yshift=1cm] (f) {f};
     \node [right of=e] (g) {g};
     \node [above right of=g, xshift=2cm] (h) {h};
     \node [below right of=g, xshift=2cm] (i) {i};  
     \draw (a.east) -| (45:20mm) |- (b.west);
     \draw (a.east) -| (-45:20mm) |- (c.west);
     % offending line
     \draw[red] (g.east) -| (45:20mm) |- (h.west);;
\end{tikzpicture}
\end{document}
0
3

The issue is that (45:2mm) is an absolute coordinate, which happens to be left of g. In the case of a, b and c you get the desired result because a happens to sit at (0,0), the default position that gets used if you do not specify any, so (45:2mm) is right of a "by accident". The arguably cleaner way is to add + or ++ (in this case this does not make a difference since this is the second coordinate of the path) in front of all of the (45:2mm) coordinates.

\documentclass[tikz]{standalone}    
\usetikzlibrary{calc}    
\begin{document}    
\begin{tikzpicture}[node distance = 3cm, auto]    
     \node (a) {a};
     \node [above right of=a, xshift=2cm] (b) {b};
     \node [below right of=a, xshift=2cm] (c) {c};
     \node [right of=b] (d) {d};
     \node [above right of=d, yshift=-1cm] (e) {e};
     \node [below right of=d, yshift=1cm] (f) {f};
     \node [right of=e] (g) {g};
     \node [above right of=g, xshift=2cm] (h) {h};
     \node [below right of=g, xshift=2cm] (i) {i};  
     \draw (a.east) -| +(45:20mm) |- (b.west);
     \draw (a.east) -| +(-45:20mm) |- (c.west);
     % no longer offending line
     \draw[red] (g.east) -| +(45:20mm) |- (h.west);;
\end{tikzpicture}
\end{document}

enter image description here

That way you will not encounter bad surprises if you ever decide to move a around.

Finally let me mention that such diagrams can conveniently be produced with forest, which is based on tikz.

\documentclass[tikz,border=10pt]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={forked edges,text height=0.75em,text depth=0.25ex,grow'=0}
  [a
   [b
    [d
     [e
      [g
       [h]
       [i]
      ]
      ]
     [f]
    ]
   ]
   [c]
  ]
\end{forest}
\end{document}

enter image description here

1
  • thank you for the explanation.
    – user20650
    Aug 22 '19 at 17:50

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.