我想绘制一个 NFA,并使其适合列宽,我将其设为锯齿形。令我沮丧的是,它看起来就像喝了很多杯龙舌兰酒后才离开酒吧。我曾尝试明确指定 q₇ – q₁₁ 的相对位置,但没有成功。这个额外的偏移来自哪里?我现在该如何将其拉直?
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{arrows,automata,positioning}
\usepackage{libertine}
\begin{document}
\begin{tikzpicture}[->,>=stealth',semithick,node distance=2cm,shorten >=5pt]
\node[initial,state] (0) {q₀};
\node[state] (1) [above right of=0] {q₁};
\node[state] (2) [below of=1] {q₂};
\node[state] (3) [above right of=2] {q₃};
\node[state] (4) [below of=3] {q₄};
\node[state] (5) [above right of=4] {q₅};
\node[state] (6) [below of=5] {q₆};
\node[state] (7) [above right=1cm and 1cm of 6] {q₇};
\node[state] (8) [below of=7] {q₈};
\node[state] (9) [above right=1cm and 1cm of 8] {q₉};
\node[state] (10) [below=1cm of 9] {q₁₀};
\node[state] (11) [above right=1cm and 1cm of 10] {q₁₁};
\node[state,accepting] (12) [below=1cm of 11] {q₁₂};
\path (0) edge [right] node {\textit{F}} (1)
(1) edge [right] node {\textit{o}} (2)
(2) edge [right] node {\textit{o}} (3)
(3) edge [right] node {\textit{:}} (4)
(4) edge [right] node {␣} (5)
(5) edge [right] node {\textit{t}} (6)
(6) edge [right] node {\textit{h}} (7)
(7) edge [right] node {\textit{i}} (8)
(8) edge [right] node {\textit{n}} (9)
(9) edge [right] node {\textit{g}} (10)
(10) edge [right] node {\textit{s}} (11)
(11) edge [right] node {\textit{!}} (12)
(0) edge [loop above, above] node {$Σ$} (0)
;
\end{tikzpicture}
\end{document}
答案1
\foreach
我认为您可以使用(自动化就是爱)和一些节点放置计算来节省大量代码空间。
该字符␣
未在我的示例中显示,但我猜测对于在这种情况下解决方案的工作来说它并不重要。
输出
代码
\documentclass[margin=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage{libertine}
\begin{document}
\begin{tikzpicture}[->,>=stealth',semithick,node distance=2cm,shorten >=5pt]
\foreach \labels [count=\x starting from 0, remember=\x as \lastx (initially 0)] in {%
{$Σ$},
\textit{F},%
\textit{o},%
\textit{o},%
\textit{:},%
\textit{␣},%
\textit{t},%
\textit{h},%
\textit{i},%
\textit{n},%
\textit{g},%
\textit{s},%
\textit{!}%
}{
\pgfmathsetmacro\xpos{int(\x+1-mod(\x+1,2))}
\pgfmathsetmacro\ypos{int(mod(\x,2)*2)}
\ifnum\x=0
\node[initial,state] (\x) at (0,0) {$q_{\x}$};
\path (0) edge [loop above, above] node {\labels} (0);
\else\ifnum\x=12
\node[accepting,state] (\x) at (\xpos,\ypos) {$q_{\x}$};
\else
\node[state] (\x) at (\xpos,\ypos) {$q_{\x}$};
\fi
\path (\lastx) edge[right] node {\labels} (\x);
\fi
}
\end{tikzpicture}
\end{document}