TikZ PGF – How to Draw an Arrow in the Middle of a Line

arrowsdecorationstikz-pgf

Like this:

.------<------.
|             |
v             ^
|             |
'------>------'

I am currently using

\begin{scope}[very thick,->]
  \draw (-4,1)--(-4,0)--(0.1,0);
  \draw (0,0)--(4,0)--(4,1.1);
  \draw (4,1)--(4,2)--(-0.1,2);
  \draw (0,2)--(-4,2)--(-4,0.9);
\end{scope}

but this is rather inelegant. I prefer something more like

\begin{scope}[very thick,middle decoration=>] 
             %           ^^^^^^^^^^^^^^^^^ a hypothetical option
  \draw (-4,0)--(4,0);
  \draw (4,0)--(4,2);
  \draw (4,2)--(-4,2);
  \draw (-4,2)--(-4,0);
\end{scope}

Best Answer

The decorations library can be used to all kinds of stuff like this. Unfortunately it is slightly verbose.

\usetikzlibrary{decorations.markings}

\begin{scope}[very thick,decoration={
    markings,
    mark=at position 0.5 with {\arrow{>}}}
    ] 
    \draw[postaction={decorate}] (-4,0)--(4,0);
    \draw[postaction={decorate}] (4,0)--(4,2);
    \draw[postaction={decorate}] (4,2)--(-4,2);
    \draw[postaction={decorate}] (-4,2)--(-4,0);
\end{scope}