TEX: tikz package and drawing graphs (labels, colors, bends etc.)

Resource: http://tex.stackexchange.com/questions/29712/moving-a-label-along-the-path

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata,positioning}

\begin{document}

\pagestyle{empty}

\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto,/tikz/initial text=] 
   \node[state,initial] (q_0) {$q_0$};
   \node[state] (q_2) [above of=q_0] {$q_2$};
   \node[state] (q_1) [left of=q_2] {$q_1$};
   \node[state] (q_3) [right of=q_2] {$q_3$};
   \node[state] (q_5) [right of=q_0] {$q_5$};
   \node[state] (q_4) [left of=q_0] {$q_4$};   
   \node[state] (q_7) [below of=q_0] {$q_7$};
   \node[state] (q_6) [left of=q_7] {$q_6$};
   \node[state] (q_8) [right of=q_7] {$q_8$};

   \path[->,every node/.style={font=\scriptsize}]
    (q_0) edge [bend left=15] node {$a$} (q_2)
    (q_2) edge [bend left=15] node {$b$} (q_0)
    (q_0) edge [bend left=15] node {$c$} (q_5)
    (q_5) edge [bend left=15] node {$d$} (q_0)
    (q_0) edge [bend left=15] node {$e$} (q_7)
    (q_7) edge [bend left=15] node {$f$} (q_0)
    (q_0) edge [bend left=15] node {$g$} (q_4)
    (q_4) edge [bend left=15] node {$h$} (q_0)
    (q_0) edge [bend left=15] node {$i$} (q_1)
    (q_1) edge [bend left=15] node {$j$} (q_0)
    (q_0) edge [bend left=15] node {$k$} (q_3)
    (q_3) edge [bend left=15] node {$l$} (q_0)
    (q_0) edge [bend left=15] node {$m$} (q_8)
    (q_8) edge [bend left=15] node {$n$} (q_0)
    (q_0) edge [bend left=15] node {$o$} (q_6)
    (q_6) edge [bend left=15] node {$p$} (q_0);  
\end{tikzpicture}

\end{document}

Using the options such as [midway], [near start], [near end], [very near end] enable us to specify the location of the label.

(q_0) edge [bend left=15] node [red, near end] {$a$} (q_2)
(q_2) edge [bend left=15] node [blue, near start]{$b$} (q_0)
(q_0) edge [bend left=15] node [green, near end] {$c$} (q_5)
(q_0) edge [bend left=15] node [red, very near end] {$k$} (q_3)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s