# Adding tikzpicture layers that can be viewed sequentially in the output (Motzkin graphs)

In a Beamer presentation it's possible to add elements to a tikzfigure on a page sequentially so that the viewer doesn't have to take in all of the complexity at once. You cannot use the Beamer commands in a book documentclass however. Is it possible to add layers to a diagram using tikz, say for an interactive figure in an e-book? The viewer should be able to advance through the sequence of frames with a mouse click.

-
When you say "display a grid and then a path between nodes of the grid, and then a second path", do you want to create three pages in your document, with slightly different versions of the image in the same position on each page? Or do you want to create some kind of animation or movie that appears on only one page? Or do you want to create multiple slightly different versions of the same picture on the same page as a vertical sequence? For the last one, take a look at Highlighting specific parts of a TikZ drawing –  Jake Jun 14 '12 at 5:46
You can generate an animated .gif and include that in your document, as I did for to produce the animation as shown in Sieve of Eratosthenes in tikz. –  Peter Grill Jun 14 '12 at 6:36
If you don't mind using AdobeReader for display, try the animate package. You can create a sequence of (TikZ/PSTricks/...) images with single graphical elements and combine them using a timeline. –  AlexG Jun 14 '12 at 8:10
With package ocg you can use pdf layers, but not all pdf reader can understand them (Adobe and PDF-XChange viewer does it). A nice article about them is Creating PDF layers using ocg.sty –  Ignasi Jun 14 '12 at 9:04
@JasonWhyte: Yes, I think editing your question to include an example would be a good idea. –  Jake Jun 15 '12 at 5:02

Here's a clunky example of what I could achieve with the animate package and tikz -- @AlexG gave a good tip! I compiled the file with pdflatex and could view the layers on the diagram with Adobe Acrobat Pro 9.5.1 on a Mac running OS 10.5.8. This is adequate for what I want for a simple diagram, but if I can remove some of the repetition I will make a revision.

\documentclass[a4paper]{article}
\usepackage{xcolor,tikz,animate,fancyvrb}
\begin{document}
\usetikzlibrary{arrows,matrix,positioning,fit,calc}

% A clunky attempt to make the process of layering tikz pictures
% without having to reproduce previous layers through using a timeline file in animate

\centering
% Sample_timeline is a textfile that adds the
% frames defined below one at a time.
\begin{VerbatimOut}{Sample_timeline}
::0x0
::1x0
::2x0
::3x0
\end{VerbatimOut}

\begin{animateinline}[
step,controls,timeline=Sample_timeline,
begin={%
\begin{tikzpicture}%
\useasboundingbox (-0.5,-0.5) rectangle (10,9.5);%
},
end={\end{tikzpicture}}
]{1} %although not relavant (option `step') fps is required argument
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (0,0);
\coordinate (XAxisMax) at (10,0);
\coordinate (YAxisMin) at (0,0);
\coordinate (YAxisMax) at (0,9);
\draw [thin, gray,-latex] (XAxisMin) -- (XAxisMax) node [right] {$n$}; % Draw x axis
\draw [thin, gray,-latex] (YAxisMin) -- (YAxisMax) node [left] {$j$};% Draw y axis

\pgftransformcm{1}{0}{0}{1}{\pgfpoint{0cm}{0cm}}
\coordinate (Btwo) at (8,8);

\coordinate (Arrowvertex) at (2,6);
\coordinate (Arrowa) at (3,7);
\coordinate (Arrowb) at (3,6);
\coordinate (Arrowc) at (3,5);

\draw[style=help lines,dashed] (0,0) grid[step=1cm] (10,9);
\foreach \x in {0,...,8}{% Two indices running over each
\foreach \y in {0,...,\x}{% node on the grid we have drawn
\node[draw,circle,inner sep=2pt,fill] at (1*\x,1*\y) {};
% Places a dot at those points
}
}

\coordinate (Testlabel) at (8,6);

\foreach \x in {0,...,8}{
\foreach \y in {0,...,7}{
\draw [gray] node [below] at (1*\x,0) {\x};
\draw [gray] node [left] at (0,1*\y) {\y};
}
}

\draw (Testlabel) node [above right] {$n \ge j$};

\draw [thick,-latex,black] (Arrowvertex) -- (Arrowa) node [pos=0.7,left] {$a_{j+1}$} ;
\draw [thick,-latex] (Arrowvertex) -- (Arrowb) node [pos=1.1, above left] {$b_{j}$};
\draw [thick,-latex] (Arrowvertex) -- (Arrowc) node [pos=0.5,below] {$a_{j}$};

\foreach \x in {3}{
\foreach \y in {5,6,7}{
\node[draw,circle,inner sep=2pt,fill] at (1*\x,1*\y) {};
}
}

\foreach \x in {2}{
\foreach \y in {6}{
\node[draw,circle,inner sep=2pt,fill] at (1*\x,1*\y) {};
}
}
\newframe
% these commands allow me to superimpose the paths on the coordinates
% defined by the grid in the previous frame (frame 0) without having
% to draw it again =  more efficient diagram making.
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (0,0);
\coordinate (XAxisMax) at (10,0);
\coordinate (YAxisMin) at (0,0);
\coordinate (YAxisMax) at (0,9);

% draw a path from (0,0) to (5,3)
\path[red,thick,->] (Origin) edge (1,1) {};
\path[red,thick,->] (1,1) edge (2,2) {};
\path[red,thick,->] (2,2) edge (3,3) {};
\path[red,thick,->] (3,3) edge (4,4) {};
\path[red,thick,->] (4,4) edge (5,3) {};
\newframe
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (0,0);
\coordinate (XAxisMax) at (10,0);
\coordinate (YAxisMin) at (0,0);
\coordinate (YAxisMax) at (0,9);

% draw a second path from (0,0) to (5,3)
\path[purple,thick,->] (Origin) edge (1,0) {};
\path[purple,thick,->] (1,0) edge (2,0) {};
\path[purple,thick,->] (2,0) edge (3,1) {};
\path[purple,thick,->] (3,1) edge (4,2) {};
\path[purple,thick,->] (4,2) edge (5,3) {};
\newframe
\coordinate (Origin)   at (0,0);
\coordinate (XAxisMin) at (0,0);
\coordinate (XAxisMax) at (10,0);
\coordinate (YAxisMin) at (0,0);
\coordinate (YAxisMax) at (0,9);

% draw a third path from (0,0) to (5,3)
\path[green,thick,->] (Origin) edge (1,1) {};
\path[green,thick,->] (1,1) edge (2,2) {};
\path[green,thick,->] (2,2) edge (3,3) {};
\path[green,thick,->] (3,3) edge (4,3) {};
\path[green,thick,->] (4,3) edge (5,3) {};
\end{animateinline}
\end{document}
-
I edited your code example somewhat (I hope you don't mind): The contents of the timeline file can be defined within the source using fancyvrb package. The timeline itself could be simplified. And I moved the tikzpicture environment into the animateinline options. –  AlexG Jun 15 '12 at 8:26