4

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
1
  • Actually I can make \Atom accepts an argument, e.g., \Atom{6} and \Atom{12} but it looks cumbersome! Mar 29, 2013 at 4:34

1 Answer 1

7

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 (https://tex.stackexchange.com/a/50493)
\makeatletter
\DeclareRobustCommand*{\savepause}[1]{\only<1>{\immediate\write\@auxout{\string\pauseentry{\the\c@framenumber}{#1}{\the\c@beamerpauses}}}}
\newcommand*{\pauseentry}[3]{\global\@namedef{pauses@#1@#2}{#3}}
\newcommand*{\usepause}[1]{\@ifundefined{pauses@\the\c@framenumber @#1}{1}{\@nameuse{pauses@\the\c@framenumber @#1}}}
\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}

animated output result, showing 11 frames

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.