我想在 TikZ 中绘制一个自动机,我有以下代码段:
\usepackage{tikz}
\usetikzlibrary{automata,arrows}
\begin{document}
\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=5 cm, scale = 1, transform shape]
\node[initial,state] (B) {$B$};
\node[state] (AB) [below of=B] {$A,B$};
\node[state] (ABD) [right of=AB] {$A,B,D$};
\node[state,accepting] (BC) [right of=B] {$B,C$};
\node[state,accepting] (ABCD) [right of=BC] {$A,B,C,D$};
\path[->] (B) edge [above] node [align=center] {$a$} (BC)
(B) edge [left] node [align=center] {$b$} (AB)
(AB) edge [left] node [align=center] {$a$} (BC)
(AB) edge [above] node [align=center] {$b$} (ABD)
(BC) edge [loop above] node [align=center] {$a$} (BC)
(BC) edge [above] node [align=center] {$b$} (ABCD)
(ABD) edge [right] node [align=center] {$a$} (BC)
(ABD) edge [loop below] node [align=center] {$b$} (ABD)
(ABCD) edge [below] node [align=center] {$a$} (BC)
(ABCD) edge [loop above] node [align=center] {$b$} (ABCD);
\end{tikzpicture}
\end{document}
现在,我对这个输出有两个问题,首先,在初始状态下它有箭头,另外还写了start
,我该如何删除该start
文本?其次,可以看出我有平行边,例如:
(BC) edge [above] node [align=center] {$b$} (ABCD)
(ABCD) edge [below] node [align=center] {$a$} (BC)
但是当它们画出来时看起来像一条粗线,我怎样才能在它们之间留出一些空间以便清楚地看到平行连接?
答案1
要删除起始节点的默认文本,start
请添加选项initial text=
。您可以稍微弯曲边缘,使它们显示为单独的线条;例如,添加bend left=10
。
您不必添加align=center
,因为这是默认设置。您也不必指定在边的哪一侧放置标签。如果您不喜欢默认设置,只需将 -option 添加swap
到节点,例如(B) edge node[swap] {$a$} (BC)
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{automata,arrows}
\begin{document}
\begin{tikzpicture}%
[>=stealth',shorten >=1pt,auto,node distance=5 cm,
scale = 1, transform shape]
\node[initial,state,initial text=] (B) {$B$};
\node[state] (AB) [below of=B] {$A,B$};
\node[state] (ABD) [right of=AB]{$A,B,D$};
\node[state,accepting] (BC) [right of=B] {$B,C$};
\node[state,accepting] (ABCD) [right of=BC]{$A,B,C,D$};
\path[->]
(B) edge node {$a$} (BC)
(B) edge node {$b$} (AB)
(AB) edge node {$a$} (BC)
(AB) edge node {$b$} (ABD)
(BC) edge [loop above] node {$a$} (BC)
(BC) edge [bend left=10] node {$b$} (ABCD)
(ABD) edge node {$a$} (BC)
(ABD) edge [loop below] node {$b$} (ABD)
(ABCD) edge [bend left=10] node {$a$} (BC)
(ABCD) edge [loop above] node {$b$} (ABCD)
;
\end{tikzpicture}
\end{document}