绘制 FSM 自动机时,tikz 似乎吃了箭头

绘制 FSM 自动机时,tikz 似乎吃了箭头

我有以下自动机,并注意到 Tikz 出于某种原因吃掉了一些箭头。我在预期看到箭头的位置(大致)画出了红色。

在此处输入图片描述

代码如下:

\begin{tikzpicture}
\node[state, initial] (s1) {$s_1$};
\node[state, right=2cm of s1] (s2) {$s_2$};
\node[state, accepting, right=2cm of s2] (s3) {$s_3$};

\draw   (s1) edge[loop above] node{0} (s1)
        (s1) edge[above] node{1} (s2)
        (s2) edge[loop above] node{1} (s2)
        (s2) edge[above] node{0} (s3);
\end{tikzpicture}

我还导入了以下库:

\usetikzlibrary{arrows,automata,positioning}

我的代码基于第 4 页的示例: https://www3.nd.edu/~kogge/courses/cse30151-fa17/Public/other/tikz_tutorial.pdf

而且他们的输出好像也没有吃过箭。

答案1

本教程确实\tikzset{->} 全球。这就是它在教程中起作用的原因。在我看来,这是一种有点值得怀疑的方法。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}
\begin{document}
This is your code fragment:
\begin{center}
\begin{tikzpicture}
\node[state, initial] (s1) {$s_1$};
\node[state, right=2cm of s1] (s2) {$s_2$};
\node[state, accepting, right=2cm of s2] (s3) {$s_3$};

\draw   (s1) edge[loop above] node{0} (s1)
        (s1) edge[above] node{1} (s2)
        (s2) edge[loop above] node{1} (s2)
        (s2) edge[above] node{0} (s3);
\end{tikzpicture}
\end{center}

An appropriate way to get the arrow is to add \texttt{->} in a \emph{local
group}. (It gets added to the \verb|\draw| command.)
\begin{center}
\begin{tikzpicture}
\node[state, initial] (s1) {$s_1$};
\node[state, right=2cm of s1] (s2) {$s_2$};
\node[state, accepting, right=2cm of s2] (s3) {$s_3$};

\draw[->]   (s1) edge[loop above] node{0} (s1)
        (s1) edge[above] node{1} (s2)
        (s2) edge[loop above] node{1} (s2)
        (s2) edge[above] node{0} (s3);
\end{tikzpicture}
\end{center}

The tutorial you quote adds \verb|\tikzset{->}| \emph{globally}.
\tikzset{->}%
As you saw, this can lead to a lot of confusion, but it also adds the arrows.
\begin{center}
\begin{tikzpicture}
\node[state, initial] (s1) {$s_1$};
\node[state, right=2cm of s1] (s2) {$s_2$};
\node[state, accepting, right=2cm of s2] (s3) {$s_3$};

\draw   (s1) edge[loop above] node{0} (s1)
        (s1) edge[above] node{1} (s2)
        (s2) edge[loop above] node{1} (s2)
        (s2) edge[above] node{0} (s3);
\end{tikzpicture}
\end{center}

If I was you I would avoid adding this globally (and perhaps select another tutorial).

\end{document}

在此处输入图片描述

相关内容