我对“倾斜”选项有疑问(见下面的示例)。从s0
到s3
和s3
到s0
,标签太靠近边缘。该pos
选项允许用户沿路径移动标签,但我想要的是将其移离边缘,同时仍保持标签根据边缘倾斜。可以做到吗?
\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}
答案1
通过使用,near end
您非常接近所需的解决方案...pos= ..
您可以确定路径上节点的相对位置。请参阅下面的 MWE。
\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}
我擅自修改了您的 MWE,使其更加简洁。在此,我纠正了所有奇怪的节点选项用法。为此,我定义了新样式并重新定义了默认标签样式。我还利用positioning
库来定位自动机状态。
附录:
另一种方法是使用 TikZ 库quotes
并根据边的属性对其进行分组:
\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}
结果与上面的 MWE 相同。