Tikz 中的叠加层给出错误

Tikz 中的叠加层给出错误

我的 tikz 图形在 beamer 中运行良好。但我需要在第二张幻灯片上画一个箭头。每当我插入\only<2>\onslide<2>在此 tikz 图片中插入时,都会出现错误:

\begin{frame}[t]{Example 3.2}
\Large $L = \lbrace w\#w | w \in \lbrace 0,1 \rbrace^* \rbrace$ \normalsize
\vspace{2cm}
\begin{center}
\begin{tikzpicture}[->, >=stealth', shorten >= 1pt, node distance = 0.6cm, semithick]
\begin{scope}
\newcounter{prevI}\setcounter{prevI}{0}
\node(n0) at (0,0){};
\foreach \x [count=\i] in {0,1,1,0,0,0,\#,0,1,1,0,0,0,$\sqcup$}{
    \node[right of = n\theprevI,minimum size=0.6cm] (n\i) {\x};
    \stepcounter{prevI}
}
\node[right of= n\theprevI]{$\cdots$};
\end{scope}

%\only<2>{ 
    \draw [->] (0,1) -| (n1); 
%}
\end{tikzpicture}
\end{center}
\end{frame}

这编译得很好,甚至正确地在当前幻灯片上绘制了箭头。\only<2>当我取消底部的注释时,Latex 编译器给出错误:

line 145: Command \c@prevI already defined. \end{frame}
line 95: You need to compile with XeLaTeX or LuaLaTeX to use the Fira fonts
line 14: Snakes have been superseded by decorations. Please use the decoration libraries instead of the snakes library
line 66: Overfull \vbox (50.88618pt too high) detected
line 66: Overfull \vbox (15.63992pt too high) detected
: Label(s) may have changed. Rerun to get cross-references right.

答案1

欢迎!问题是,如果您添加第二个覆盖,计数器将再次创建,因此会出现错误。因此,为了解决这个问题,您需要\newcounter{prevI}在框架之前移动。事实证明,同样的效果可能会导致计数器产生奇怪的效果,这就是为什么您可能想要添加\resetcounteronoverlays{prevI},即使这里还不需要它。此外,\only我建议使用visible on=from而不是overlay-beamer-styles。对于更复杂的幻灯片,这有助于避免跳转。

\documentclass[fleqn]{beamer}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,overlay-beamer-styles}
\newcounter{prevI}%<-moved
\resetcounteronoverlays{prevI}%<-added, not necessary at this point
\begin{document}
\begin{frame}[t]
\frametitle{Example 3.2}
\Large $L = \lbrace w\#w | w \in \lbrace 0,1 \rbrace^* \rbrace$ \normalsize
\vspace{2cm}
\begin{center}
\begin{tikzpicture}[->, >=stealth', shorten >= 1pt, node distance = 0.6cm, semithick]
\begin{scope}
\setcounter{prevI}{0}
\node(n0) at (0,0){};
\foreach \x [count=\i] in {0,1,1,0,0,0,\#,0,1,1,0,0,0,$\sqcup$}{
    \node[right of = n\theprevI,minimum size=0.6cm] (n\i) {\x};
    \stepcounter{prevI}
}
\node[right of= n\theprevI]{$\cdots$};
\end{scope}

\draw [->,visible on=<2>] (0,1) -| (n1); 
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

在此处输入图片描述

PS 另外,\vspace您可以只增加的边界框tikzpicture,然后用计数器换取remember钥匙。

\documentclass[fleqn]{beamer}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,overlay-beamer-styles}
\begin{document}
\begin{frame}[t]
\frametitle{Example 3.2}
\Large $L = \lbrace w\#w | w \in \lbrace 0,1 \rbrace^* \rbrace$ \normalsize
\begin{center}
\begin{tikzpicture}[->, >=stealth', shorten >= 1pt, node distance = 0.6cm, semithick]
\begin{scope}
\node(n0) at (0,0){};
\foreach \x [count=\i,remember=\i as \j (initially 0)] in {0,1,1,0,0,0,\#,0,1,1,0,0,0,$\sqcup$}{
    \node[right of = n\j,minimum size=0.6cm,alias=lastn] (n\i) {\x};
}
\node[right of= lastn]{$\cdots$};
\end{scope}
\draw [->,visible on=<2>] (0,1) -| (n1); 
\path (0,2.5); %instead of \vspace
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

这是一个更加精致的动画。

\documentclass[fleqn]{beamer}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,overlay-beamer-styles}
\makeatletter
\newcommand*{\currentoverlaynumber}{\number\beamer@slideinframe}
\makeatother
\newcounter{prevI}%<-moved
\resetcounteronoverlays{prevI}%<-added, not necessary at this point
\begin{document}
\begin{frame}[t]
\frametitle{Example 3.2}
\Large $L = \lbrace w\#w | w \in \lbrace 0,1 \rbrace^* \rbrace$ \normalsize
\vspace{2cm}
\begin{center}
\begin{tikzpicture}[->, >=stealth', shorten >= 1pt, node distance = 0.6cm, semithick]
\begin{scope}
\setcounter{prevI}{0}
\node(n0) at (0,0){};
\foreach \x [count=\i] in {0,1,1,0,0,0,\#,0,1,1,0,0,0,$\sqcup$}{
    \node[right of = n\number\value{prevI},minimum size=0.6cm] (n\i) {
    \ifnum\i<\currentoverlaynumber%
    X
    \else
    \x
    \fi};
    \stepcounter{prevI}
}
\node[right of= n\number\value{prevI}]{$\cdots$};
\end{scope}
\foreach \X in {1,...,\value{prevI}}
{\draw [->,visible on=<\the\numexpr\X+1>] (0,1) -| (n\X); }
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

在此处输入图片描述

答案2

我会使用不同的方法,不用计数器prevI。通过使用 TikZ 库并在循环中 chains插入空节点,代码变得更简单并按预期工作:n0

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows,
                chains}

\begin{document}
\begin{frame}[t]{Example 3.2}
\Large $L = \lbrace w\#w | w \in \lbrace 0,1 \rbrace^* \rbrace$ \normalsize
\vspace{2cm}
\begin{center}
    \begin{tikzpicture}[>=stealth', shorten >= 1pt, semithick, 
node distance = 6mm and 0mm,
  start chain = going right,
                        ]
\foreach \x [count=\i] in {,0,1,1,0,0,0,\#,0,1,1,0,0,0,$\sqcup$, \dots}%
{
    \node[minimum size=6mm, on chain] (n\i) {\x};
}

\only<2>{
    \draw [->] (0,1) -| (n2);
}
    \end{tikzpicture}
\end{center}
\end{frame}
\end{document}

在此处输入图片描述

相关内容