[Tex/LaTex] Draw arrows between nodes with tikz

tikz-pgf

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?

Best Answer

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