60

I have drawn 4 nodes with

\documentclass[tikz,border=5pt]{standalone}

\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[node distance=2cm]

% nodes
\node [draw] (A) {A};
\node [draw, right=of A] (B) {B};
\node [draw, right=of B] (C) {C};
\node [draw, right=of C] (D) {D};

% arrows
\draw [->] (A) -- (B) -- (C) -- (D);

\end{tikzpicture}

\end{document}

I am trying to draw an arrow between each node.

If I use

\draw [->] (A) -- (B) -- (C) -- (D);

I get lines between each node but only an arrow from C to D.

Can I, in one line, draw arrows between all of the nodes?

1 Answer 1

76

edge can be used to put the arrows in one \draw command:

\draw [->] (A) edge (B) (B) edge (C) (C) edge (D);

The usage of -| as path for edge is also possible via option to path:

\documentclass[tikz,border=5pt]{standalone}

\begin{document}

\begin{tikzpicture}[node distance=2cm]

% nodes
\node (A) at (0, 0) {A};
\node (B) at (1, 1) {B};
\node (C) at (2, 1.5) {C};
\node (D) at (3, 0) {D};

% arrows
\draw[->, to path={-| (\tikztotarget)}]
  (A) edge (B) (B) edge (C) (C) edge (D);

\end{tikzpicture}

\end{document}

Result

5
  • 1
    @Raphael Yes, see updated answer. Oct 7, 2015 at 16:11
  • Thank you! (Nota bene: more complicated paths can be given, but then nodes specified like edge[...] node {...} (target) won't appear.)
    – Raphael
    Oct 7, 2015 at 19:10
  • @HeikoOberdiek, how to add names to edge? For example (A) edge node[pos=0.75,right] {a} (B) doesn't show node a.
    – Zarko
    Oct 20, 2015 at 2:05
  • @Zarko The edges are set at a later time, thus node does not see the path of the edge. A workaround is to add the node to a non-drawn path: \path (A) -| node[pos=0.75,right] {a} (B); Oct 20, 2015 at 4:49
  • @HeikoOberdiek, yes, on such a way this works, however, with your solution \draw[->, to path={-| (\tikztotarget)}] .... this is not possible anymore (because edge target is already incorporated into edge, I think so). Meanwhile i figured out an complicated solution ... if you interested, how I do this, see my upgrade of my answer on question tex.stackexchange.com/questions/273928/orthogonal-edges-style. And, thank you very much!
    – Zarko
    Oct 20, 2015 at 14:19

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.