[Tex/LaTex] TikZ-pgf directed graph: change arrow color and location

arrowstikz-pgftkz-graph

I have something like this:

\SetVertexNormal[Shape     = circle,
                 LineWidth = 2pt]
\SetUpEdge[lw    = 1.5pt,
           color = blue]
...
\begin{tikzpicture}
  \Vertex[x=0 ,y=0]{1}
  \Vertex[x=0 ,y=2]{2}
  \tikzset{EdgeStyle/.append style = {straight}}
  \tikzset{EdgeStyle/.style={->}}
  \Edge(1)(2)
\end{tikzpicture}

This creates a simple directed graph with two nodes (1 and 2) connected by a blue arrow. However, I would like to move the arrow to the center of the line (instead of leaving it at the tip) and would like to change its color to red. Can anyone help me out?

Best Answer

I assume you're using TikZ and tkz-graph

You can use the decoration.markings library to set a marking at the middle position.
To the human eye this does not look like the middle, though, because only the tip of the arrow is halfway between (1) and (2).
But you can use arbitrary TikZ code in the with {<code goes here>} part. I cloned the first solution and changed it so that it does draw a zero length arrow (without a line). To me it looks better than the first solution.

Code

\documentclass{article}
\usepackage{tikz,tkz-graph}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.markings}
\begin{document}
\SetVertexNormal[
        Shape = circle,
    LineWidth = 2pt
]
\SetUpEdge[
       lw = 1.5pt,
    color = blue
]

\begin{tikzpicture}
    \Vertex[x=0 ,y=0]{1}
    \Vertex[x=0 ,y=2]{2}
    \tikzset{EdgeStyle/.append style = {straight}}
    \tikzset{
        EdgeStyle/.style={
            postaction=decorate,
            decoration={
                markings,
                mark=at position .5 with {\arrow{>}}
            }
        }
    }
    \Edge(1)(2)
    \Vertex[x=0 ,y=0]{1}
    \Vertex[x=0 ,y=2]{2}

    \Vertex[x=1 ,y=0]{1}
    \Vertex[x=1 ,y=2]{2}
    \tikzset{
        EdgeStyle/.style={
            postaction=decorate,
            decoration={
                markings,
                mark=at position .5 with {\draw[red,->] (0,0) (0,0);}
            }
        }
    }
    \Edge(1)(2)
    \Vertex[x=1 ,y=0]{1}
    \Vertex[x=1 ,y=2]{2}
\end{tikzpicture}
\end{document}

Output

Compiled example

Closer look

closer look at the arrow


More solutions can be found: TikZ: How to draw an arrow in the middle of the line?