I have a problem with the 'sloped' option (see my example below). From s0 to s3 and s3 to s0, the labels are too close to the edge. The pos option allows the user to move the label along the path, but what I want is to move it away from the edge while still keeping the label sloped according to the edge. Is it possible to do?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=2cm]

  \node[state,accepting][label=above:{\tiny{label:10}}] (q0)      {$s_0$};
  \node[state](q1) at (4,0)  {$s_1$};
  \node[state][label=right:\rotatebox{-90}{\tiny{label:20}}](q2) at (4,-3) {$s_2$};

  \node[state][label={[xshift=-0.9cm, yshift=-1cm]\tiny{label:5}}](q3) at (0,-3) {$s_3$};

  \path[->] (q0)  edge [in=150, out=120, loop, above, align=left] node {$a=0$ \\ $x:=x+1$} (q0);
  \path[->] (q0)  edge [bend right=10, align=left, below] node  {$a=1$} (q1);
  \path[->] (q1)  edge [bend right=10, align=left, above, inner sep=1pt] node  {$a=0$} (q0);
  \path[->] (q1)  edge [bend right=10, align=left, sloped] node  {$a=2$} (q2);
  \path[->] (q2)  edge [bend left=-10, sloped, align=left] node  {$a=1$} (q1);
    \path[->] (q0)  edge [bend right=10, sloped, align=left, inner sep=1pt] node  {$a=3$} (q3);
  \path[->] (q3)  edge [bend left=-10, align=left, sloped, inner sep=1pt] node  {$a=0$} (q0);    
  \path[->] (q0)  edge [bend left=10, align=left, sloped, inner sep=1pt]      node  {$a=2$} (q2);

  \path[->] (q2)  edge [bend left=10, align=left, sloped, near end]      node  {$a=0$} (q0);

 \path[->] (q1)  edge [bend left=10, align=left, sloped, near end]      node  {$a=3$} (q3);
 \path[->] (q2)  edge [bend left=10, align=left, below, inner sep=4pt]      node  {$a=3$} (q3);
  \path[->] (q1)  edge [in=30, out=60, loop, above, align=left]  node {$x=1$ \\ $x:=x+1$} (q1);

  \path[->] (q3)  edge  [bend left=10, align=left, sloped, inner sep=1pt] node  {$a=1$} (q1);
   \path[->] (q3)  edge  [bend right=-10, align=left, above, inner sep=1pt] node  {$a=2$} (q2);     

  \path[->] (q2)  edge  [in=-30,out=-60, loop, below, align=left, inner sep=1pt] node  {$a=2$ \\ $x:=x+1$} (q2);

  \path[->] (q3)  edge  [in=-150,out=-120, loop, below, align=left, inner sep=3pt] node  {$a=3$ \\ $x:=x+1$} (q3);

\end{tikzpicture}
\end{document}
up vote 7 down vote accepted

By use near end you were very close to desired solution ... With pos= .. you can determine relative position of node on path. See MWE below.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}
\usepackage{amsmath,amssymb,amsfonts}

\begin{document}
\begin{tikzpicture}[>=stealth',
    shorten > = 1pt,
node distance = 3cm and 4cm,
    el/.style = {inner sep=2pt, align=left, sloped},
every label/.append style = {font=\tiny}
                    ]
\node (q0) [state,accepting,label=left:{label:10}]  {$s_0$};
\node (q1) [state,right=of q0]                      {$s_1$};
\node (q2) [state,below=of q1,
            label=right:{label:20}]                 {$s_2$};
\node (q3) [state,below=of q0,
            label=left:{label:5}]                   {$s_3$};
\path[->] 
    (q0)  edge [in=150,out=120,loop] 
                node[el,above,rotate=-45] {$a=0$ \\ $x:=x+1$}   (q0)
    (q0)  edge [bend right=10]  node[el,below]  {$a=1$}         (q1)
    (q1)  edge [bend right=10]  node[el,above]  {$a=0$}         (q0)
    (q1)  edge [bend right=10]  node[el,below]  {$a=2$}         (q2)
    (q2)  edge [bend left=-10]  node[el,below]  {$a=1$}         (q1)
    (q0)  edge [bend right=10]  node[el,below]  {$a=3$}         (q3) 
    (q3)  edge [bend left=-10]  node[el,below]  {$a=0$}         (q0)
    (q0)  edge [bend left= 10]  node[el,above,pos=0.8] {$a=2$}  (q2)
    (q2)  edge [bend left= 10]  node[el,below,pos=0.8] {$a=0$}  (q0)
    (q1)  edge [bend left= 10]  node[el,above,pos=0.75] {$a=3$} (q3)
    (q2)  edge [bend left= 10]  node[el,below]  {$a=3$}         (q3)
    (q1)  edge [in=30, out=60,loop]  
                node[el,above,rotate=45] {$x=1$\\ $x:=x+1$}     (q1)
    (q3)  edge [bend left=10]   node[el,below,pos=0.75] {$a=1$} (q1)
    (q3)  edge [bend right=-10] node[el,above]  {$a=2$}         (q2)
    (q2)  edge [in=-30,out=-60, loop] 
                node[el,below,rotate=-45] {$a=2$ \\ $x:=x+1$}   (q2)
    (q3)  edge [in=-150,out=-120, loop] 
                node[el,below,rotate=45] {$a=3$ \\ $x:=x+1$}    (q3);
\end{tikzpicture}
\end{document}

enter image description here

I took liberty and change your MWE that it to be more concise. In this I correct all strange use of nodes options. For it I define new style as well redefine default label style. Also I exploit positioning library for positioning of automaton states.

Addendum: An alternative, with use of TikZ library quotes and grouping edges according to their properties:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows, automata, positioning, quotes}
\usepackage{amsmath,amssymb,amsfonts}

\begin{document}
\begin{tikzpicture}[
node distance = 3cm and 4cm,
    el/.style = {inner sep=2pt, align=left},
every label/.append style = {font=\tiny},
every  edge/.append style = {draw, -stealth', shorten > = 1pt,
                             font=\footnotesize, inner sep=2pt, auto,
                             align=left, sloped},                       
                    ]
\node (q0) [state,accepting,label=left:{label:10}]  {$s_0$};
\node (q1) [state,right=of q0]                      {$s_1$};
\node (q2) [state,below=of q1,
            label=right:{label:20}]                 {$s_2$};
\node (q3) [state,below=of q0,
            label=left:{label:5}]                   {$s_3$};
    \begin{scope}[bend left=10,sloped]
\path[->]
    (q0)  edge ["$a=1$"] (q1)
    (q1)  edge ["$a=0$"] (q0)
    (q2)  edge ["$a=3$"] (q3)
    (q3)  edge ["$a=2$"] (q2);
    \end{scope}
    \begin{scope}[bend left=10,pos=0.64,sloped]
\path   (q1)  edge ["$a=2$"] (q2)
        (q2)  edge ["$a=1$"] (q1)
        (q0)  edge ["$a=3$"] (q3)
        (q3)  edge ["$a=0$"] (q0);
    \end{scope}
    \begin{scope}[bend left=10,pos=0.75,sloped]
\path   (q0)  edge ["$a=2$"] (q2)
        (q2)  edge ["$a=0$"] (q0)
        (q1)  edge ["$a=3$"] (q3)
        (q3)  edge ["$a=1$"] (q1);
    \end{scope}
\path   (q0)  edge [in=150,out=120,loop]
                    node[el,above,rotate=-45] {$a=0$ \\ $x:=x+1$}   (q0)
        (q1)  edge [in=30, out=60,loop]
                    node[el,above,rotate=45]  {$x=1$\\ $x:=x+1$}   (q1)
        (q2)  edge [in=-30,out=-60, loop]
                    node[el,below,rotate=-45] {$a=2$ \\ $x:=x+1$}   (q2)
        (q3)  edge [in=-150,out=-120, loop]
                    node[el,below,rotate=45] {$a=3$ \\ $x:=x+1$}    (q3);
\end{tikzpicture}
\end{document}

The result is the same as with above MWE.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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