# Is it possible to implement a “label-goto”-like workflow in composing overlay?

I don't want to hardly code the value 6 in the following code as:

• I have to manually count the number of steps.
• The mechanism breaks when a block of code (\Atom in this example) is repeated in the same frame.
\documentclass{beamer}
\usepackage{pstricks}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt

\def\Atom{%
\bgroup
\only<6->{\psset{fillstyle=solid,fillcolor=cyan}}
\pscustom[linejoin=0]
{
\only<+->{\moveto(0,0)}
\only<+->{\lineto(2,0)}
\only<+->{\lineto(2,2)}
\only<+->{\lineto(0,2)}
\only<+->{\closepath}
}
\egroup
}

\begin{document}
\begin{frame}
\begin{pspicture}[showgrid](-3,-3)(3,3)
\Atom
\psscalebox{-1 -1}{\Atom}
\end{pspicture}
\end{frame}
\end{document}


Is it possible to implement a "label-goto"-like workflow in composing overlay? Something similar to

label xxx:

overlay 1
overlay 2
overlay 3

goto xxx

-
Actually I can make \Atom accepts an argument, e.g., \Atom{6} and \Atom{12} but it looks cumbersome! –  cyanide-based food Mar 29 '13 at 4:34

You can use named overlays as introduced in reference overlay numbers with names: The general idea is to have a command \savepause{name}, which saves the current value of the counter beamerpauses. You can then reference this value by using \usepause{name} in an overlay specification: \only<\usepause{name}->{...}.

In your case, you want the same operation \Atom processed multiple times with different settings. The necessary overlay specifications can be determined by creating named overlays square0, square1, ..., which point to the "end" of the corresponding square drawn by \Atom:

\documentclass{beamer}
\usepackage{pstricks}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt

\def\Atom{%
\bgroup
\only<\currentsquareend->{\psset{fillstyle=solid,fillcolor=cyan}}
\pscustom[linejoin=0]
{
\only<+->{\moveto(0,0)}
\only<+->{\lineto(2,0)}
\only<+->{\lineto(2,2)}
\only<+->{\lineto(0,2)}
\only<+->{\closepath}
}
\egroup
\squarecompleted
}

% Reference overlay numbers with names (http://tex.stackexchange.com/a/50493)
\makeatletter
\newcommand*{\pauseentry}[3]{\global\@namedef{pauses@#1@#2}{#3}}
\makeatother

\newcounter{squares}
\newcommand*{\squarecompleted}{\savepause{square\thesquares}\stepcounter{squares}}
\newcommand*{\currentsquareend}{\usepause{square\thesquares}}

\begin{document}
\begin{frame}
\setcounter{squares}{0}
\begin{pspicture}[showgrid](-3,-3)(3,3)
\Atom
\psscalebox{-1 -1}{\Atom}
\end{pspicture}
\end{frame}
\end{document}


The document has to be compiled twice in order to produce correct results.

### How it works

A counter squares is used to automate the creation of the necessary named overlays. This is done by introducing two new macros: \squarecompleted is invoked at the end of \Atom, it creates a new named overlay square\thesquares (i.e. square0, square1, ...) and steps the counter squares. This named overlay can be retrieved with \currentsquareend, which returns the end frame for the current square and is used in

\only<\currentsquareend->{\psset{fillstyle=solid,fillcolor=cyan}}


Note also the \setcounter{squares}{0} at the beginning of the frame: It is crucial to have it exactly there, and not only in the preamble for example, as each frame is processed multiple times in order to create the animation.

-