使用 beamer 进行复杂的 tikz 动画

使用 beamer 进行复杂的 tikz 动画

我正在使用 tikz 和 beamer 创建一种凯撒密码动画。

我可以得到我想要的,但是由于我对 tikz 不是很熟练,结果有点奇怪,因为它的编译速度非常慢:这是一个很大的缺点,因为在一个包含许多 tikz 的大文档中,它的速度会变得非常慢。

我的代码如下(嗯,一个小的工作示例)

\documentclass{beamer}
\usetheme{Berkeley}
\usepackage{graphicx}
\usepackage{ifthen}
\usepackage{tikz}
\tikzset{
  invisible/.style={opacity=0},
  visible on/.style={alt={#1{}{invisible}}},
  alt/.code args={<#1>#2#3}{\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} 
  },
}
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}[scale=0.3, every node/.style={scale=0.3}]
 \pgfmathsetmacro{\alphsize}{26}
 \pgfmathsetmacro{\ang}{360/\alphsize}
 \pgfmathsetmacro{\d}{3}
 \pgfmathsetmacro{\op}{98 + \ang/2 - 1.2}
 \pgfmathsetmacro{\e}{\ang + \ang*\d}
 \pgfmathsetmacro{\ep}{\op + \ang*\d}
 \foreach \x in {0,\ang,...,360} {\draw[gray] (\x:8em) -- (\x:12em);}
 \foreach \x [count=\xi] in {A,...,Z} {
  \node[rotate=\ang - \ang*\xi,visible on=<1>]  at (\op - \ang*\xi:11em)     {{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<2>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{T}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<3>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{E}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<4>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{L}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<5>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{E}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<6>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{P}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<7>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{H}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<8>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{O}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<9>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{N}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<10>] at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{E}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\e - \ang*\xi,visible on=<1>]  at (\ep - \ang*\xi:9em) {{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<2>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{W}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<3>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{H}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<4>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{O}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<5>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{H}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<6>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{S}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<7>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{K}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<8>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{R}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<9>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{Q}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<10>] at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{H}}{\color{red}\large \x}{\large \x}};     
}
 \draw[thick] (0cm,0cm) circle(12em);
 \draw[gray] (0cm,0cm) circle(10em);
 \draw[thick] (0cm,0cm) circle(8em);
\end{tikzpicture} 

\bigskip

\centering
\begin{tikzpicture}[every node/.style={scale=0.6}]
  \draw[step=1em,gray,thin] (0em,0em) grid (9em,2em);
  \node at (0.5em,1.5em) {\textcolor<2> {green}T};
  \node at (1.5em,1.5em) {\textcolor<3> {green}E};
  \node at (2.5em,1.5em) {\textcolor<4> {green}L};
  \node at (3.5em,1.5em) {\textcolor<5> {green}E};
  \node at (4.5em,1.5em) {\textcolor<6> {green}P};
  \node at (5.5em,1.5em) {\textcolor<7> {green}H};
  \node at (6.5em,1.5em) {\textcolor<8> {green}O};
  \node at (7.5em,1.5em) {\textcolor<9> {green}N};
  \node at (8.5em,1.5em) {\textcolor<10>{green}E};
  \node[visible on=<2->]  at (0.5em,0.5em) {\textcolor<2> {red}W};
  \node[visible on=<3->]  at (1.5em,0.5em) {\textcolor<3> {red}H};
  \node[visible on=<4->]  at (2.5em,0.5em) {\textcolor<4> {red}O};
  \node[visible on=<5->]  at (3.5em,0.5em) {\textcolor<5> {red}H};
  \node[visible on=<6->]  at (4.5em,0.5em) {\textcolor<6> {red}S};
  \node[visible on=<7->]  at (5.5em,0.5em) {\textcolor<7> {red}K};
  \node[visible on=<8->]  at (6.5em,0.5em) {\textcolor<8> {red}R};
  \node[visible on=<9->]  at (7.5em,0.5em) {\textcolor<9> {red}Q};
  \node[visible on=<10->] at (8.5em,0.5em) {\textcolor<10>{red}H};
\end{tikzpicture}

\end{frame}
\end{document}

问题是:有没有更聪明的方法可以获得与上述相同的结果并且编译速度更快?

答案1

我没有发现任何可能严重减慢代码速度的重大问题。由于重新编译大量复杂的 TikZ 图片可能非常麻烦,因此有一个库external可以强制 TikZ 保存图片,并在可能的情况下重复使用图片。具体操作如下。

\usetikzlibrary{external}
\tikzexternalize

编辑:正如@brad 在评论中所建议的那样,使用\tikzexternalize[prefix=tikz/]来为临时 TikZ 图片设置一个文件夹会很有利。

由于您使用的是 beamer\only构造,因此这不会按预期工作,只会渲染第一帧。但是,有一个“黑客”:

用 where包裹整个tikzpicture环境\only<m-M>m最小并且M最大\only命令引用的帧号之内tikz图片。

由于该[visible on=<m-M>]选项与命令“等同” \only,因此您可以这样使用它。

\only<1-10>{%
\begin{tikzpicture}
    \node[visible on=<1>] {};
    % ...
    \node[visible on=<10>] {};
\end{tikzpicture}
}

我已经尝试过这个代码,第二次(及以后)的编译时间是很多降低。

使用 tikz 外部功能与 beamer \only了解更多信息。

完整代码:

\documentclass{beamer}
\usetheme{Berkeley}
\usepackage{graphicx}
\usepackage{ifthen}
\usepackage{tikz}
\tikzset{
  invisible/.style={opacity=0},
  visible on/.style={alt={#1{}{invisible}}},
  alt/.code args={<#1>#2#3}{\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} 
  },
}

\usetikzlibrary{external}
\tikzexternalize

\begin{document}
\begin{frame}
\centering
\only<1-10>{%
\begin{tikzpicture}[scale=0.3, every node/.style={scale=0.3}]
 \pgfmathsetmacro{\alphsize}{26}
 \pgfmathsetmacro{\ang}{360/\alphsize}
 \pgfmathsetmacro{\d}{3}
 \pgfmathsetmacro{\op}{98 + \ang/2 - 1.2}
 \pgfmathsetmacro{\e}{\ang + \ang*\d}
 \pgfmathsetmacro{\ep}{\op + \ang*\d}
 \foreach \x in {0,\ang,...,360} {\draw[gray] (\x:8em) -- (\x:12em);}
 \foreach \x [count=\xi] in {A,...,Z} {
  \node[rotate=\ang - \ang*\xi,visible on=<1>]  at (\op - \ang*\xi:11em)     {{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<2>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{T}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<3>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{E}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<4>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{L}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<5>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{E}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<6>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{P}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<7>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{H}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<8>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{O}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<9>]  at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{N}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\ang - \ang*\xi,visible on=<10>] at (\op - \ang*\xi:11em) {\ifthenelse{\equal{\x}{E}}{\color{green}\Large \x}{\Large \x}};   
  \node[rotate=\e - \ang*\xi,visible on=<1>]  at (\ep - \ang*\xi:9em) {{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<2>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{W}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<3>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{H}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<4>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{O}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<5>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{H}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<6>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{S}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<7>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{K}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<8>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{R}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<9>]  at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{Q}}{\color{red}\large \x}{\large \x}};     
  \node[rotate=\e - \ang*\xi,visible on=<10>] at (\ep - \ang*\xi:9em) {\ifthenelse{\equal{\x}{H}}{\color{red}\large \x}{\large \x}};     
}
 \draw[thick] (0cm,0cm) circle(12em);
 \draw[gray] (0cm,0cm) circle(10em);
 \draw[thick] (0cm,0cm) circle(8em);
\end{tikzpicture} 
}
\bigskip

\centering
\only<1-10>{%
\begin{tikzpicture}[every node/.style={scale=0.6}]
  \draw[step=1em,gray,thin] (0em,0em) grid (9em,2em);
  \node at (0.5em,1.5em) {\textcolor<2> {green}T};
  \node at (1.5em,1.5em) {\textcolor<3> {green}E};
  \node at (2.5em,1.5em) {\textcolor<4> {green}L};
  \node at (3.5em,1.5em) {\textcolor<5> {green}E};
  \node at (4.5em,1.5em) {\textcolor<6> {green}P};
  \node at (5.5em,1.5em) {\textcolor<7> {green}H};
  \node at (6.5em,1.5em) {\textcolor<8> {green}O};
  \node at (7.5em,1.5em) {\textcolor<9> {green}N};
  \node at (8.5em,1.5em) {\textcolor<10>{green}E};
  \node[visible on=<2->]  at (0.5em,0.5em) {\textcolor<2> {red}W};
  \node[visible on=<3->]  at (1.5em,0.5em) {\textcolor<3> {red}H};
  \node[visible on=<4->]  at (2.5em,0.5em) {\textcolor<4> {red}O};
  \node[visible on=<5->]  at (3.5em,0.5em) {\textcolor<5> {red}H};
  \node[visible on=<6->]  at (4.5em,0.5em) {\textcolor<6> {red}S};
  \node[visible on=<7->]  at (5.5em,0.5em) {\textcolor<7> {red}K};
  \node[visible on=<8->]  at (6.5em,0.5em) {\textcolor<8> {red}R};
  \node[visible on=<9->]  at (7.5em,0.5em) {\textcolor<9> {red}Q};
  \node[visible on=<10->] at (8.5em,0.5em) {\textcolor<10>{red}H};
\end{tikzpicture}
}
\end{frame}
\end{document}

相关内容