35

Friends, consider the following code:

\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}

which gives us the following output:

Automaton 1

So far, so good (believe me, there are worse drawings).

I was wondering if it's possible to move the labels to an arbitrary position along the path, that is:

Path

The workaround I found was to draw a path without a label and then \draw the texts in the proper absolute positions. I was trying to achieve something like this:

Automaton 2

I'm a TikZ newbie, so bear with me. I'm sure there are better approaches than the one I used. Any ideas? :-)

  • 3
    You can use near end and similars (page 193 of the manual), for example: (q_0) edge [bend left=15,near end] node {$c$} (q_5) – Gonzalo Medina Sep 27 '11 at 18:58
43

You can use options such as [midway], [near start] and [near end], [very near end] 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)

enter image description here

As Gonzalo pointed out, these are documented on Page 193 of the manual and correspond as follows:

at start pos=0

very near start pos=0.125

near start pos=0.25

very near end pos=0.875

near end pos=0.75

at end pos=1

|improve this answer|||||
14

It is much simpler than you might think. For example replace your first edge with this:

 (q_0) edge [bend left=15] node[pos=0.8] {$a$} (q_2)

And play around with 0.8 number between zero and one.

|improve this answer|||||
4

In your example, alternately use near end and near start:

(q_0) edge [bend left=15] node [near end] {$a$} (q_2)
(q_2) edge [bend left=15] node [near start] {$b$} (q_0)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.