我在 Tikz 中有以下自动机:
是否有可能实现与此类似的转变?
这是我的代码:
\begin{tikzpicture}[>=stealth,node distance=2.7cm,auto]
\node[state,initial,accepting] (I) {$I$};
\node[state] (p) [above right of = I] {$p$};
\node[state] (q) [right of = p] {$q$};
\node[state] (r) [below right of = I] {$r$};
\node[state,accepting] (F) [below right of = q] {$F$};
\path[->]
(I) edge node {a} (p)
edge node {b} (r)
(p) edge node {b} (q)
edge [swap, bend right] node {abc} (F)
(q) edge [bend left] node {aabc} (F)
edge [swap, bend right] node {c} (F)
(r) edge node {aabc} (F)
edge [swap, bend right] node {c} (F);
\end{tikzpicture}
答案1
像这样:
这很简单。自动机中的节点与 tikzpictures 中的其他节点一样,因此您可以根据需要在它们之间画线。在本例中借助 帮助|-
,yshift
以及-|
节点 I、r 和 F 之间:
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{automata}
\begin{document}
\begin{tikzpicture}[>=stealth,node distance=2.7cm,auto]
\node[state,initial,accepting] (I) {$I$};
\node[state] (p) [above right of = I] {$p$};
\node[state] (q) [right of = p] {$q$};
\node[state] (r) [below right of = I] {$r$};
\node[state,accepting] (F) [below right of = q] {$F$};
\path[->]
(I) edge node {a} (p)
edge node {b} (r)
(p) edge node {b} (q)
edge [swap, bend right] node {abc} (F)
(q) edge [bend left] node {aabc} (F)
edge [swap, bend right] node {c} (F)
(r) edge node {aabc} (F)
edge [swap, bend right] node {c} (F);
\draw[red,thick,->] (I) |- ([yshift=-5mm] r.south) -| (F);% <-- added
\end{tikzpicture}
\end{document}
答案2
Zarko 的解决方案很好,但我预先设计了一个可以自动计算正确位置的解决方案,这个解决方案使用 fit 库
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{automata}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[>=stealth,node distance=2.7cm,auto]
\node[state,initial,accepting] (I) {$I$};
\node[state] (p) [above right of = I] {$p$};
\node[state] (q) [right of = p] {$q$};
\node[state] (r) [below right of = I] {$r$};
\node[state,accepting] (F) [below right of = q] {$F$};
\path[->]
(I) edge node {a} (p)
edge node {b} (r)
(p) edge node {b} (q)
edge [swap, bend right] node {abc} (F)
(q) edge [bend left] node {aabc} (F)
edge [swap, bend right] node {c} (F)
(r) edge node {aabc} (F)
edge [swap, bend right] node {c} (F);
\node[fit=(p) (r) (q) (F) (I)](all){};
\draw[red] (F) |-(all.south) -| (I);
\end{tikzpicture}
\end{document}