1

I am using tikz to create a sort of Ceasar cipher animation with beamer.

I can get what I want but, as I am not very skilled in tikz, the result is somewhat weird because it is very slow to compile: this is quite a drawback because inside a big document with many of them it slows down awfully.

My code is as follows (well, a small working example)

\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}

The question is: is there a smarter way to obtain the same result as above which will compile faster?

brad
  • 517

1 Answers1

2

I don't see any major issues that could significantly slow down your code. Since recompiling a large number of complex TikZ pictures can be very bothering, there is a library external that forces TikZ to save and, if possible, reuse the pictures. This is done as follows.

\usetikzlibrary{external}
\tikzexternalize

Edit: As @brad suggests in the comments, it can be advantageous to use \tikzexternalize[prefix=tikz/], to set a folder for the temporary TikZ pictures.

Since you are using the beamer \only construct, this does not work as expected, only the first frame is rendered. There is, however, a "hack":

Wrap the whole tikzpicture environment by \only<m-M> where m is the minimal and M is the maximal frame number that is referenced to by \only commands within the tikzpicture.

Since the [visible on=<m-M>] option is "equivalent" to the \only command, you can use it like this.

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

I have tried this for your code and the second (and later) compile times are much lower.

See Use tikz external feature with beamer \only for more info.

The full code:

\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}
Ondrian
  • 1,536
  • 1
    It works! I didn't know the external library. With a little search, I also realized that it can be better to use \tikzexternalize[prefix=tikz/] instead of \tikzexternalize, otherwise the directory gets quickly overcrowded... – brad Feb 24 '16 at 18:31