tikz draw 出错了

tikz draw 出错了

我正在尝试绘制一个状态图,尽管许多类似的状态图都可以正常工作,但这个特定的状态图似乎无缘无故地出现了这个错误。

我编译时得到的错误pdflatex

! Package tikz Error: (, +, coordinate, pic, or node expected.

See the tikz package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.21    \draw[input] (start) to \node
                               [above,sloped] {a} (a);

这是我的代码

\documentclass[fleqn]{article}

\usepackage{tikz}   % for diagrams
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[
    state/.style={circle, draw=blue!60, fill=blue!5, very thick, node distance=1.5cm},
    start/.style={name=start},
    input/.style={->, thick, shorten >= 1mm, shorten <= 1mm},
]
    \node[state, start] {\(q_0\)};
    \node[state, above right=of start] (a) {\(q_1\)};
    \node[state, below right=of start] (b) {\(q_2\)};

    \draw[input] (start) to \node[above,sloped] {a} (a);
    \draw[input] (start) to \node[below,sloped] {b} (b);
    \draw[input, loop above] (start) to \node[above] {c} (start);
    \draw[input] (start.west)+(-1cm,0) to (start.west);
\end{tikzpicture}
\end{document}

一个类似的、可以工作的图表的例子是

\documentclass[fleqn]{article}

\usepackage{tikz}   % for diagrams
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[
    state/.style={circle, draw=blue!60, fill=blue!5, very thick, node distance=1.5cm},
    start/.style={name=start},
    input/.style={->, thick, shorten >= 1mm, shorten <= 1mm},
]
    \node[state, start] {\(q_0\)};
    \node[state, right=of start] (odd) {\(q_1\)};

    \draw[input, bend left=40] (start) to node[below] {a,b,c} (odd);
    \draw[input, bend left=40] (odd) to node[above] {a,b,c} (start);
\end{tikzpicture}
\end{document}

答案1

你不应该在路径内使用\nodebut (另请参阅nodeZarko 的评论,我刚才看到了。)

\documentclass[fleqn]{article}

\usepackage{tikz}   % for diagrams
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}[
    state/.style={circle, draw=blue!60, fill=blue!5, very thick, node distance=1.5cm},
    start/.style={name=start},
    input/.style={->, thick, shorten >= 1mm, shorten <= 1mm},
]
    \node[state, start] {\(q_0\)};
    \node[state, above right=of start] (a) {\(q_1\)};
    \node[state, below right=of start] (b) {\(q_2\)};

    \draw[input] (start) to node[above,sloped] {a} (a);
    \draw[input] (start) to node[below,sloped] {b} (b);
    \draw[input, loop above] (start) to node[above,midway] {c}  (start);
    \draw[input] (start.west)+(-1cm,0) to (start.west);
\end{tikzpicture}
\end{document}

在此处输入图片描述

quotes如果你不喜欢这个语法,你可以在这里使用node。(但是如果你添加了 babel 包,请记得也添加\usetikzlibrary{babel}。)

\documentclass[fleqn]{article}

\usepackage{tikz}   % for diagrams
\usetikzlibrary{positioning}
\usetikzlibrary{quotes}

\begin{document}
\begin{tikzpicture}[
    state/.style={circle, draw=blue!60, fill=blue!5, very thick, node distance=1.5cm},
    start/.style={name=start},
    input/.style={->, thick, shorten >= 1mm, shorten <= 1mm},
]
    \node[state, start] {\(q_0\)};
    \node[state, above right=of start] (a) {\(q_1\)};
    \node[state, below right=of start] (b) {\(q_2\)};

    \draw[input] (start) to["a" sloped] (a);
    \draw[input] (start) to["b" sloped] (b);
    \draw[input, loop above] (start) to["c"]  (start);
    \draw[input] (start.west)+(-1cm,0) to (start.west);
\end{tikzpicture}
\end{document}

答案2

扩展我的评论,作为对@Schrödinger 的猫答案的补充,基于tikzautomata

\documentclass[fleqn]{article}

\usepackage{tikz}   % for diagrams
\usetikzlibrary{automata, 
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[auto,
           node distance = 11mm and 11mm,
            state/.style = {circle, draw=blue!60, very thick, fill=blue!5, 
                            minimum size=2em, inner sep=0pt},
               shorten > = 1mm, shorten < = 1mm, 
                           thick,
            initial text = , initial distance = 9mm,
every edge quotes/.style = {sloped}
]
\node (start) [state, initial] {\(q_0\)};
\node (a)     [state, above right=of start] {\(q_1\)};
\node (b)     [state, below right=of start] {\(q_2\)};
%
\path[->]   (start) edge["$a$"]   (a) 
            (start) edge["$b$"]   (b)
            (start) edge[loop above,looseness=10,"$c$"] (start);% default looseness=8
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容