[Tex/LaTex] How to make a tkz graph with edges starting and ending at the same vertex

graphstkz-graph

I am trying to use the tkz-graph package to put together a directed graph. I have never used it before, so I am trying to piece together examples I have found on the internet. Below is the code that I have so far:

\begin{tikzpicture}
\SetUpEdge[lw = 1.5pt, color = blue]
\GraphInit[vstyle=Normal] 
\SetGraphUnit{4}
\tikzset{VertexStyle/.append  style={fill}}
\Vertex{S}
\EA(S){I} \EA(I){R}
\tikzset{EdgeStyle/.style={->}}
\Edge[label=$\beta$](I)(R)
\Edge[label=$\alpha$](S)(I)
\end{tikzpicture}

This gives me a graph with three vertices, and two edges. I want to also add in a loop, starting and ending at the same vertex, for each vertex. I tried including the following three lines:

\Edge[label=$1-\alpha$](S)(S)
\Edge[label=$1-\beta$](I)(I)
\Edge[label=$1$](R)(R)

But this didn't work. The graph just came out looking messy. I also tried to fiddling with the angle of the edges, by doing the following:

\tikzset{EdgeStyle/.style={->}, in=45, out=135}
\Edge[label=$1-\alpha$](S)(S)
\Edge[label=$1-\beta$](I)(I)
\Edge[label=$1$](R)(R)

But it still looked messy. I just want those edges to look like loops, with labels on them, as shown in the code. Any help would be appreciated.

Best Answer

There is a \Loop command for this, see the example on page 25 of the manual.

The syntax is

\Loop[<options>](<vertex>)

without any spaces before/after brackets/parens.

In the options one can specify

  • dir - the possible values are WE, EA, NO, SO, NOWE, NOEA, SOWE, SOEA for west, east, north, south, north west etc., e.g. dir=NO.
  • dist - defines the size of the loop. Specify a length, e.g. dist=2cm.
  • label - the label of the vertex, e.g. label={$a$}
  • style - the style of the loop, allowing you to customize thickness, color etc., e.g. style={green,->,thick}
  • labelstyle - the style of the label, allowing you to customize positioning, colour etc., e.g. labelstyle={above} to place the label above the loop.

Example

result of code below

\documentclass[convert]{standalone}
\usepackage{tkz-graph}
\begin{document}
\begin{tikzpicture}
\SetUpEdge[lw = 1.5pt, color = blue]
\GraphInit[vstyle=Normal] 
\SetGraphUnit{4}
\tikzset{VertexStyle/.append  style={fill}}
\Vertex{S}
\EA(S){I} \EA(I){R}
\tikzset{EdgeStyle/.style={->}}
\Edge[label=$\beta$](I)(R)
\Edge[label=$\alpha$](S)(I)
\Loop[dir=NO,label={$1-\beta$},labelstyle={above}](I)
\Loop[dir=SO,dist=1cm](R)
\Loop[dir=WE,dist=2cm,style={blue,very thick},label={$1-\alpha$},labelstyle={fill=white}](S)
\end{tikzpicture}
\end{document}
Related Question