为什么这个 tikz 图片无法编译

为什么这个 tikz 图片无法编译

我是 Latex 的新手,尤其是 Latex-Beamer。我尝试编译这个 tikz 图片,但它不起作用。有人能帮我修复它吗?提前谢谢!

梅威瑟:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
    automata,
    positioning,
    quotes}

\begin{document}
    \begin{frame}[t]{}
        \begin{tikzpicture}[scale = 0.4, transform shape][
            - > , >= stealth,
            shorten >=1pt ,
            node distance =2.5 cm ,
            initial text = ,
            semithick ]
            \tikzstyle{accepting}=[green, text = black, accepting by double]
            \tikzstyle{non-acc} = [red, text = black]
            \node[state, initial, accepting] (q) {$ \varepsilon_1 $};
            \node[state, non-acc](q0) [right = 2cm of q]{$ 0_2 $}
            \node[state, draw = none](d)[above =1 cm of q] {};
            \path[->]   
            (q)   edge [above] node {0} (q0)
            (q) edge [right] node {1} (d)
            ;
        \end{tikzpicture}
    \end{frame}
\end{document}

答案1

您有几个语法错误。

  1. 您必须...[scale = 0.4, transform shape][ ...关闭并重新打开一个可选参数,但tikzpicture只接受一个;
  2. 你忘了第二个分号\node
  3. 你写的- >不是->

此外,您正在使用(非常)弃用的样式语法。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
    automata,
    positioning,
    quotes}

\begin{document}
    \begin{frame}[t]{}
        \begin{tikzpicture}[scale = 0.4, transform shape,
            -> , >= stealth,
            shorten >=1pt ,
            node distance =2.5 cm ,
            initial text = ,
            semithick ]
            \tikzset{accepting/.style={green, text = black, accepting by double},
                non-acc/.style={red, text = black}}
            \node[state, initial, accepting] (q) {$ \varepsilon_1 $};
            \node[state, non-acc](q0) [right = 2cm of q]{$ 0_2 $};
            \node[state, draw = none](d)[above =1 cm of q] {};
            \path[->]
            (q)   edge [above] node {0} (q0)
            (q) edge [right] node {1} (d)
            ;
        \end{tikzpicture}
    \end{frame}
\end{document}

在此处输入图片描述

答案2

我将利用所有已加载的库并正确定义图像元素的样式(对于您的错误,请参阅不错的(+1)@Rmano 回答):

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata,
                positioning,
                quotes}

\begin{document}
\begin{frame}[t]
\frametitle{Title of frame}
    \begin{center}
\begin{tikzpicture}[scale=0.5, transform shape,
node distance = 24mm, on grid,
    semithick,
    shorten > = 1pt,
            > = Stealth,
    initial text = {}]
\node[state,initial, double, draw=green]        (q0)    {$\varepsilon_1$};
\node[state, draw=red, right=of q0]  (q1)    {$0_1$};
%
\path[->] (q0) edge ["0"] (q1)
          (q0) edge ["1" '] ++ (0,2);
\end{tikzpicture}
    \end{center}
\end{frame}
\end{document}

在此处输入图片描述

如果您喜欢将图像放在幻灯片的左上角,则添加选项[t]\begin{frame}删除center环境。

** 附录:** 通过定义初始节点样式,图像代码变得更短:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata,
                positioning,
                quotes}

\begin{document}
\begin{frame}
\frametitle{Title of frame}
    \begin{center}
\begin{tikzpicture}[scale=0.8, transform shape,
node distance = 24mm, on grid,
    semithick,
    outer sep = 1.6pt,      % new
            > = Stealth,
initial/.append style = {initial text = {}, % new
                         double, double distance=1.6pt, draw=green}
                    ]
\node[state,initial]        (q0)    {$\varepsilon_1$};
\node[state, draw=red, right=of q0]  (q1)    {$0_1$};
%
\path[->] (q0) edge ["0"] (q1)
          (q0) edge ["1" '] ++ (0,2);
\end{tikzpicture}
    \end{center}
\end{frame}
\end{document}

在此处输入图片描述

相关内容