[Tex/LaTex] Clever beamer animation? Writing a script that changes a tikz picture in beamer

animationsbeameroverlaystikz-pgf

Here is a start of an answer to this "challenge":

\documentstyle[beamer]
\usepackage{tikz}

\begin{document}
\begin{frame}
\tikzset{click/.style={draw}}

\begin{tikzpicture}[start chain=going right,
    node distance=4mm,
every node/.style={on chain,draw}]

\node(A)  {Alpha};
\node(B)  {Bravo};
\node(C)  {Charlie};
\node(D) {Delta};
\node(E)  {Echo};
\node(F) {Foxtrot};
\end{tikzpicture}


\play{F,E,E,D}
\play{D,E,A,F}
\play{B,E,D}

\end{frame}
\end{document}

The question is how to define ''click'' so that in each overlay, the highlighted node, would change.

In pseudo code, "click" should be defined to:

If currentStep = myNodeName then
   add "ultra thick", or "fill=green", or whatever
   to the current node.
fi

Ideally, the [click] macro should apply to any path, not just to nodes.

At the same time, "\play" should translate to

Foreach currentStep in argumentList do
  \only<+> {
      invoke the above tikzpicture while setting currentStep
   }
od

*MORE INFORMATION *

Here is something that almost works

tikzset{
  Z/.code={
    \ifthenelse{\equal{\noexpand\pc}{#1}}{%
  \tikzset{rectangle}%
    }{%
  \tikzset{circle}%
    }
  }
}
\begin{frame}[fragile,label=current]{my frame}
  \foreach \pc in {A,B,A,D,C,A} { 
  \only<+>{
    \begin{tikzpicture}[start chain=going right,node distance=1cm]
      \node[on chain][draw,Z=A] {A\pc};
      \node[on chain][draw,Z=B] {B\pc};
      \node[on chain][draw,Z=C] {C\pc};
      \node[on chain][draw,Z=D] {D\pc};
    \end{tikzpicture}
  }
}
\end{frame}

But, there must be something I miss in the expansion process. The logic seems right, the document compiles nicely, but the true branch of the conditional is never executed. Any ideas?

Best Answer

2nd attempt

(Warning: TikZ 3.0 is required!)

Additional filled requirements (see 1st attempt below for all other filled requirements):

  • Allow for "decay" via special prev styles. Each named keyword (such as kw) must provide three styles: kw, special kw and special prev kw.

  • The names of "program counter" (\pc) and its previous value (\pcprev) can be chosen via the Z choose macros key.

enter image description here

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{chains,shadows}
\tikzset{
  Z apply special/.style={special #1},
  Z apply special prev/.style={special prev #1},
  Z choose macros/.style 2 args={
    Z/.style={normal,
      Z apply special prev/.list/.expand once=#2,%
      Z apply special/.list/.expand once=#1,%
      ##1},
  },
}
\begin{document}
\begin{frame}
  \begin{tikzpicture}
    \tikzset{
      % choose macros name
      Z choose macros={\pc}{\pcprev},
      % global style for all nodes
      my node/.style={
        align=center,font=\footnotesize,anchor=center,line width=1pt,
        inner sep=1em,circle,line width=2pt,
      },
      my double/.style={double=white,double distance=1mm,draw=black},
      % normal styles for each keyword
      normal/.style={
        A1/.style={my node,black,fill=white,draw},
        B2/.style={my node,black,fill=white,draw},
        key word/.style={my node,black,fill=white,draw},
        a path/.style={draw=black,line width=1mm},
      },
      % styles for previous states for each keyword
      special prev A1/.style={A1/.append style={fill=red!50!white}},
      special prev B2/.style={B2/.append style={fill=lime!50!white}},
      special prev key word/.style={key word/.append style={fill=cyan!50!white}},
      special prev a path/.style={a path/.append style={draw=orange!50!white}},
      % styles for special state for each keyword
      special A1/.style={A1/.append style={my double,fill=red,drop shadow}},
      special B2/.style={B2/.append style={my double,fill=lime,drop shadow}},
      special key word/.style={key word/.append style={my double,fill=cyan}},
      special a path/.style={a path/.append style={my double,draw=orange}},
    }
    \foreach \pc [remember=\pc as \pcprev (initially \pc)] in {
      A1, B2, A1, a path, key word,
      {A1,B2}, {key word, a path}
    } {
      \only<+>{
        \node[anchor=base west,align=left] at (0,2) {current: \pc\\
          (prev: \pcprev)};
        \node[Z=A1] (A1) at (0,0) {A1};
        \node[Z=B2] (B2) at (3,0) {B2};
        \node[Z=key word] (key word) at (6,0) {key word};
        \draw[Z=a path]
        (A1) -- (B2)
        (B2) -- (key word)
        (key word) to[out=-145,in=-35] (A1);
      }
    }
    \path (-2,-2) (5,3); % fixed bounding box
  \end{tikzpicture}
\end{frame}
\end{document}

1st attempt

Here is an attempt without any test!

Filled requirements:

  • Support not only nodes, but also any path element.
  • Allow for longer symbolic names.
  • Allow for \pc that would contain multiple nodes [...]

Filled unformulated requirements:

  • Support different normal and special styles for each keywords.

enter image description here

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{frame}
  \begin{tikzpicture}[start chain=going right,on grid,node distance=2cm]
    \tikzset{
      every node/.style={align=center,font=\footnotesize,anchor=base},
      % normal styles for each keywords
      normal/.style={
        A1/.style={rectangle,black,fill=gray},
        B2/.style={rectangle,black,fill=red!20},
        key word/.style={rectangle,black},
        4th item/.style={black},
      },
      % special styles for each keywords
      special A1/.style={A1/.style={circle,red}},
      special B2/.style={B2/.style={circle,olive}},
      special key word/.style={key word/.style={circle,blue}},
      special 4th item/.style={4th item/.style={double=white,draw=orange,line width=1pt}},
      % select the special styles to apply
      apply special/.style={special ####1},
      Z/.style={normal,apply special/.list/.expand once=\pc,####1},
    }
    \foreach \pc in {A1, B2, A1, 4th item, key word, {A1,B2}, {key word, 4th item}} { 
      \only<+>{
        \node[overlay] at (4,1) {\pc};
        \node[on chain,draw,Z=A1] {A1};
        \node[on chain,draw,Z=B2] {B2};
        \node[on chain,draw,Z=key word] {key word};
        \draw[Z=4th item,rounded corners] (5,-1) rectangle (7,1);
      }
    }
    \path (-1,-2) (8,2); % fixed bounding box
  \end{tikzpicture}
\end{frame}
\end{document}