讓自動機圖變得更好

讓自動機圖變得更好

我使用 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 而不是簡單地使用above。加載庫後positioning,您可以自行調整空間(如果您願意)above left = 2mm and 2mmabove = 2mm,以使它們統一。但我不建議這樣做。

\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是一個相對術語。

相關內容