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 :
My doubts are :
- Right now all labels are exactly at center of any arrow. How to change its position?
- 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
To change the position of the labels use
pos=0.25
to specify the position along the path.To use a
\\
, you need to specify the alignment of the text, so something likealign=center
(or you can specify thetext 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:and if you want to use a
\\
, then it should be: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:Code: