我已经编写(复制并粘贴)了下面的代码。
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows}
\begin{document}
\begin{tikzpicture}[]
\node[state, initial, accepting] (1) {$1$};
\node[state, below left of=1] (2) {$2$};
\node[state, right of=2] (3) {$3$};
\draw (1) edge[above] node{$b$} (2)
(1) edge[below, bend right, left=0.3] node{$\epsilon$} (3)
(2) edge[loop left] node{$a$} (2)
(2) edge[below] node{$a, b$} (3)
(3) edge[above, bend right, right=0.3] node{$a$} (1);
\end{tikzpicture}
\end{document}
我得到了下面的图像。
但我希望代码能够生成下面的图像。
引用的代码和图片来自这里。
到底是怎么回事?
答案1
您应该使用包中定义的节点放置语法positioning
或省略此包并增加节点距离例如 33 毫米。
在前一种情况下,用于标记边的 MWE 使用quotes
库是:
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 15mm and 15 mm,
every edge/.append style = {draw, -{Straight Barb[scale=0.8]}},
every edge quotes/.style = {auto=right, font=\small, inner sep=2pt}
]
% nodes
\node[state, initial, accepting] (1) {$1$};
\node[state, below left= of 1] (2) {$2$};
\node[state, right= of 2] (3) {$3$};
% arrows
\path (1) edge["$b$"] (2)
edge[bend right, "$\epsilon$"] (3)
(2) edge[loop left,"$a$" '] ()
edge["{$a, b$}"] (3)
(3) edge[bend right, "$a$"] (1);
\end{tikzpicture}
\end{document}