[Tex/LaTex] How to position the labels of the path in automata

automatatikz-pgf

This is my tikz code :

\documentclass[a4paper,10pt]{article}
\usepackage{verbatim}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsmath}

\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=7 cm,
                semithick, scale = 0.6, transform shape]


\node[initial,state] (A)                                    {$s_0$};
\node[state]         (B) [above right of=A]                 {$s_1$};
\node[state]         (C) [below right of=A]                 {$s_2$};
\node[state]         (D) [below right of=B]                         {$s_3$};
\node[state]         (E) [above right of=D]                 {$s_4$};
\node[state]         (F) [below right of=D]                 {$s_5$};


  \path (A) edge [left]             node {$0$ $ \rightarrow $ [$x$ = $x.0.0$] } (B)
        edge [left]             node {$1$ $ \rightarrow $ [$x$ = $x.0.1$] } (C)
(B) edge [loop above]       node {$0$ $ \rightarrow\\ $  [$x$ = $x.0$] }   (B)
        edge [bend right,left]  node {$1$ $ \rightarrow $ [$x$ = $x.1$] }   (C)
    edge []                 node {$\$$ $ \rightarrow $ [$x$ = $x.0.\$$] } (D);   

\end{tikzpicture}
\end{document}

I am getting this :

enter image description here

My doubts are :

  1. Right now all labels are exactly at center of any arrow. How to change its position?
  2. I gave \\ within the label description. Still it didn't show in new line. For example,
    (B) edge [loop above] node {$0$ $ \rightarrow\\$ [$x$ = $x.0$] } (B). I have given \\ after \rightarrow (ie $ \rightarrow\\$). Still everything coming in the same line. How to give a line break there?

Thanks

Best Answer

  1. To change the position of the labels use pos=0.25to specify the position along the path.

  2. To use a \\ , you need to specify the alignment of the text, so something like align=center (or you can specify the text width).

You should also reconsider how you are using $ to enter and exit math mode. So something like $\$$ $ \rightarrow $ [$x$ = $x.0.\$$] should really be specified as:

$\$  \rightarrow  [x = x.0.\$]$

and if you want to use a \\ , then it should be:

$\$  \rightarrow$ \\  $[x = x.0.\$]$

You can also use sloped to get the text to go along the line. The colors below have been used to more easily see how the code below relates to the image:

enter image description here

Code:

\documentclass[a4paper,10pt]{article}
\usepackage{verbatim}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsmath}

\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=7 cm,
                semithick, scale = 0.4, transform shape]


\node[initial,state] (A)                    {$s_0$};
\node[state]         (B) [above right of=A] {$s_1$};
\node[state]         (C) [below right of=A] {$s_2$};
\node[state]         (D) [below right of=B] {$s_3$};
\node[state]         (E) [above right of=D] {$s_4$};
\node[state]         (F) [below right of=D] {$s_5$};


  \path (A) 
    edge [left] node [blue, pos=0.5, sloped, above] {$0 \rightarrow [x = x.0.0]$} (B)
    edge [left] node [cyan, pos=0.8]{$1 \rightarrow [x = x.0.1]$} (C)(B) 
    edge [loop above] node [align=center] {$0 \rightarrow$ \\ $[x = x.0]$ }   (B)
    edge [bend right,left] node  {$1 \rightarrow [x = x.1]$ }   (C)
    edge [] node [red, pos=0.2] {$\$  \rightarrow  [x = x.0.\$]$ } (D);   

\end{tikzpicture}
\end{document}