3

How can I place the arrowhead in the center of an edge?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\usepackage{tkz-graph}
\usetikzlibrary{decorations.markings}

\tikzset{->-/.style={decoration={
  markings,
  mark=at position #1 with {\arrow{>}}},postaction={decorate}}}

\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=4cm,on grid,auto] 
   \node[state] (s_0)   {$s_0$}; 
   \node[state] (s_1) [right=of s_0]  {$s_1$}; 

    \path[->]
    (s_0) edge  node {0} (s_1)
    (s_1) edge [bend right=45] node [above] {1} (s_0)
    ;
\end{tikzpicture}
\end{document} 

There are several excellent answers given for \draw, such as Tikz: Arrowheads in the center but applying the same mechanism to \path gives an error: "I cannot decorate an empty path'

1 Answers1

4

Just add the option [->-=.5] to your edges like this:

\documentclass{article}    
\usepackage{tikz}
\usetikzlibrary{automata,positioning}
\usetikzlibrary{decorations.markings}

\tikzset{->-/.style={decoration={
  markings,
  mark=at position #1 with {\arrow{>}}},postaction={decorate}}}

\begin{document}

\begin{tikzpicture}[shorten >=1pt,shorten <=1pt,node distance=4cm,on grid,auto] 
  \node[state] (s_0)   {$s_0$}; 
  \node[state] (s_1) [right=of s_0]  {$s_1$};     
  \path
    (s_0) edge[->-=.5]  node {0} (s_1)
    (s_1) edge [bend right=45,->-=.5] node [above] {1} (s_0);
\end{tikzpicture}

\end{document} 

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
  • Is is possible to do it for loops? 'edge [loop right,->-=.5] node {0} ()' gives two arrows. – Athena Widget Mar 14 '17 at 22:59
  • 1
    @AthenaWidget: Yes, it is possible. Add to the tikzpicture options the following option: every loop/.style={}, this way, the loops will have no end arrows but only those ->- you have defined in the middle. – AboAmmar Mar 14 '17 at 23:29