让自动机图变得更好

让自动机图变得更好

我使用 tikz 库绘制了一个自动机自动机。我需要帮助完善图表。我无法:

  1. 将边标签(kr、ur 等)与中心对齐,即正好位于两个节点之间
  2. 使边缘标签和边缘之间的间距相等
  3. 使图表看起来更漂亮:);)

LaTeX代码如下:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{automata}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid]
\node[state,initial] (q_0) {$q_0$};
\node[state] (q_1) [above right=of q_0] {$q_1$};
\node[state] (q_2) [below right=of q_0] {$q_2$};
\node[state,accepting] (q_3) [right=of q_1] {$q_3$};
\node[state] (q_4) [right=of q_2] {$q_4$};
\node[state] (q_5) [right=of q_4] {$q_5$};
\node[state,accepting] (q_6) [right=of q_5] {$q_6$};
\path[->] (q_0) edge node[above,align=center]{$kr$} (q_1) edge node[below,align=center]{$ur$} (q_2);
\path[->] (q_1) edge node[above] {$f$} (q_3);
\path[->] (q_2) edge node[above] {$\varepsilon$} (q_4);
\path[->] (q_4) edge node[above] {$\varepsilon$} (q_5);
\path[->] (q_5) edge node[above] {$f$} (q_6);
\path[->] (q_1) edge [loop above] node[above] {$kr$} ();
\path[->] (q_5) edge [bend left] node[below] {$ur$} (q_2);
\path[->] (q_5) edge node[above] {$kr$} (q_1);
\end{tikzpicture}
\end{document} 

图表截图:

在此处输入图片描述

答案1

您的align=center代码是多余的,请使用above leftetc 而不是简单的abovepositioning加载库后,您可以自行调整空格(如果需要)并使用above left = 2mm and 2mmabove = 2mmetc 以使它们统一。但我不建议这样做。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{automata}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid]
\node[state,initial] (q_0) {$q_0$};
\node[state] (q_1) [above right=of q_0] {$q_1$};
\node[state] (q_2) [below right=of q_0] {$q_2$};
\node[state,accepting] (q_3) [right=of q_1] {$q_3$};
\node[state] (q_4) [right=of q_2] {$q_4$};
\node[state] (q_5) [right=of q_4] {$q_5$};
\node[state,accepting] (q_6) [right=of q_5] {$q_6$};
\path[->] (q_0) edge node[above left]{$kr$} (q_1) edge node[below left]{$ur$} (q_2);
\path[->] (q_1) edge node[above] {$f$} (q_3);
\path[->] (q_2) edge node[above] {$\varepsilon$} (q_4);
\path[->] (q_4) edge node[above] {$\varepsilon$} (q_5);
\path[->] (q_5) edge node[above] {$f$} (q_6);
\path[->] (q_1) edge [loop above] node[above] {$kr$} ();
\path[->] (q_5) edge [bend left] node[below] {$ur$} (q_2);
\path[->] (q_5) edge node[above right] {$kr$} (q_1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

另一个选择是使用sloped

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{automata}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid]
\node[state,initial] (q_0) {$q_0$};
\node[state] (q_1) [above right=of q_0] {$q_1$};
\node[state] (q_2) [below right=of q_0] {$q_2$};
\node[state,accepting] (q_3) [right=of q_1] {$q_3$};
\node[state] (q_4) [right=of q_2] {$q_4$};
\node[state] (q_5) [right=of q_4] {$q_5$};
\node[state,accepting] (q_6) [right=of q_5] {$q_6$};
\path[->] (q_0) edge node[above,sloped]{$kr$} (q_1) edge node[below,sloped]{$ur$} (q_2);
\path[->] (q_1) edge node[above] {$f$} (q_3);
\path[->] (q_2) edge node[above] {$\varepsilon$} (q_4);
\path[->] (q_4) edge node[above] {$\varepsilon$} (q_5);
\path[->] (q_5) edge node[above] {$f$} (q_6);
\path[->] (q_1) edge [loop above] node[above] {$kr$} ();
\path[->] (q_5) edge [bend left] node[below] {$ur$} (q_2);
\path[->] (q_5) edge node[above,sloped] {$kr$} (q_1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

prettier正如您所知,“最后”是一个相对术语。

相关内容