![向 tikz 自动机中的节点添加额外文本](https://linux22.com/image/320580/%E5%90%91%20tikz%20%E8%87%AA%E5%8A%A8%E6%9C%BA%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9%E6%B7%BB%E5%8A%A0%E9%A2%9D%E5%A4%96%E6%96%87%E6%9C%AC.png)
我想要绘制如下所示的图表。
然而,我能想到的最接近的代码是下面的代码。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{\jobname TD.tex}
\begin{tikzpicture}[->,>=stealth', shorten >=1pt, node distance=20.0mm, auto, on grid, thick]
\node[state,initial] (0) {0};
\node[state] (1) [right=of 0] {1};
\node[state,accepting] (2) [right=of 1] {2};
\node[state,accepting] (3) [below=of 2] {3};
\node[state,accepting] (4) [below=of 3] {4};
\path[->]
(0) edge node {$<$} (1)
(1) edge node {$=$} (2)
(1) edge [bend right] node {$>$} (3)
(1) edge [bend right] node {other} (4);
\end{tikzpicture}
\end{filecontents}
\input {\jobname TD.tex}
\end{document}
正如你所见,我不知道如何添加这些返回节点 2、3、4 中的文本或节点 4 中的 *。
答案1
另一种选择是使用label
已经存在的节点的键;您可以根据需要多次使用它来在不同位置放置多个标签:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{\jobname TD.tex}
\begin{tikzpicture}[->,>=stealth', shorten >=1pt, node distance=20.0mm, auto, on grid, thick]
\node[state,initial] (0) {0};
\node[state] (1) [right=of 0] {1};
\node[state,accepting,label={right:\textbf{rerun ( relop, \texttt{LE})}}] (2) [right=of 1] {2};
\node[state,accepting,label={right:\textbf{rerun ( relop, \texttt{NE})}}] (3) [below=of 2] {3};
\node[state,accepting,label={right:\textbf{rerun ( relop, \texttt{RE})}},label={60:$\ast$}] (4) [below=of 3] {4};
\path[->] (0) edge node {$<$} (1)
(1) edge node {$=$} (2)
(1) edge [bend right] node {$>$} (3)
(1) edge [bend right] node[swap] {other} (4);
\end{tikzpicture}
\end{filecontents}
\input {\jobname TD.tex}
\end{document}
答案2
可能的解决方案
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{automata}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{\jobname TD.tex}
\begin{tikzpicture}[->,>=stealth', shorten >=1pt, node distance=20.0mm, auto, on grid, thick]
\node[state,initial] (0) {0};
\node[state] (1) [right=of 0] {1};
\node[state,accepting] (2) [right=of 1] {2};
\node (return1) [right=of 2,xshift=3mm] {\textbf{return ( relop, LE)}};
\node[state,accepting] (3) [below=of 2] {3};
\node (return2) [right=of 3,xshift=3mm] {\textbf{return ( relop, NE)}};
\node[state,accepting] (4) [below=of 3] {4};
\node (return3) [right=of 4,xshift=3mm] {\textbf{return ( relop, LT)}};
\node (inv) [below=of 3,xshift=3mm,yshift=7mm] {\textbf{*}};
\path[->]
(0) edge node {$<$} (1)
(1) edge node {$=$} (2)
(1) edge [bend right] node {$>$} (3)
(1) edge [bend right] node {other} (4);
\end{tikzpicture}
\end{filecontents}
\input {\jobname TD.tex}
\end{document}