# Is it possible to use a string instead of a slide number when invoking overlay commands such as \only?

Up to now I always use slide numbers when invoking overlay commands such as \only<...>. But for me this is a bit cumbersome because I must count the slide position in my head. If I can use a string (like \label and \ref mechanism) instead of a slide number, it becomes more elegant, doesn't it?

Is it possible to use a string instead of a slide number when invoking overlay commands such as \only?

Minimal Working Example:

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

\begin{document}

\begin{frame}
\begin{pspicture}(6,6)
\pnode(0,0){A}
\pnode(6,0){B}
\pnode(6,6){C}
\pnode(0,6){D}
\pnode(1,1){P}
\pnode(5,1){Q}
\pnode(3,5){R}
\only<5>{\psclip{\pscircle[linestyle=none,dimen=middle](3,3){3}}}
\only<1->{\pspolygon*[linecolor=red](A)(B)(Q)(P)}
\only<2->{\pspolygon*[linecolor=green](B)(C)(R)(Q)}
\only<3->{\pspolygon*[linecolor=blue](C)(D)(R)}
\only<4->{\pspolygon*[linecolor=yellow](D)(A)(P)(R)}
\only<5>{\endpsclip}
\end{pspicture}
\end{frame}

\end{document}


Edit: Oh no. I just knew that the slide numbers in one frame can be reused for other frames. I thought when writing this question that the slide numbers continue from the first slide to the last slide in the beamer document. My question does not make sense now!

My imagination when writing this question is illustrated in the following minimal (NOT) working example:

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

\begin{document}

\begin{frame}
\begin{pspicture}(6,6)
\pnode(0,0){A}
\pnode(6,0){B}
\pnode(6,6){C}
\pnode(0,6){D}
\pnode(1,1){P}
\pnode(5,1){Q}
\pnode(3,5){R}
\only<5>{\psclip{\pscircle[linestyle=none,dimen=middle](3,3){3}}}
\only<1->{\pspolygon*[linecolor=red](A)(B)(Q)(P)}
\only<2->{\pspolygon*[linecolor=green](B)(C)(R)(Q)}
\only<3->{\pspolygon*[linecolor=blue](C)(D)(R)}
\only<4->{\pspolygon*[linecolor=yellow](D)(A)(P)(R)}
\only<5>{\endpsclip}
\end{pspicture}
\end{frame}

\begin{frame}
\begin{pspicture}(6,6)
\pnode(0,0){A}
\pnode(6,0){B}
\pnode(6,6){C}
\pnode(0,6){D}
\pnode(1,1){P}
\pnode(5,1){Q}
\pnode(3,5){R}
\only<10>{\psclip{\pscircle[linestyle=none,dimen=middle](3,3){3}}}
\only<6->{\pspolygon*[linecolor=red](A)(B)(Q)(P)}
\only<7->{\pspolygon*[linecolor=green](B)(C)(R)(Q)}
\only<8->{\pspolygon*[linecolor=blue](C)(D)(R)}
\only<9->{\pspolygon*[linecolor=yellow](D)(A)(P)(R)}
\only<10>{\endpsclip}
\end{pspicture}
\end{frame}
\end{document}


I thought it will become cumbersome if the frame order change as I need to modify the slide numbers for each frame.

-
This is what the relative slide syntax is for: is there a reason for not using it? – Joseph Wright Jul 28 '12 at 16:11
Ah, your edit somewhat alters the situation: it would be very hard if every overlay had to be tracked separately! – Joseph Wright Jul 28 '12 at 16:24
Somewhat related: Reference overlay numbers with names – diabonas Jul 29 '12 at 10:16
@diabonas: good +1 for your answer and comment each. (this comment will be deleted shortly) – kiss my armpit Jul 29 '12 at 10:18

Yes, you can as long as you use anything that is a number; in the case of \label, \ref you can't directly use \ref because the string associated is not a number; you can use the refcount package to convert the string to a number first. A simple example:

\documentclass{beamer}

\newcounter{refcount}

\begin{document}

\begin{frame}
\begin{enumerate}[<+->]
\item\label{a} Item on first slide.
\item\label{b} Item on second slide.
\item\label{c} Item on third slide.
\end{enumerate}
\only<\getrefnumber{b}>{Text on second slide}
\end{frame}

\end{document}


In your concrete example, you can do something like this:

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

\newcounter{tmp}

\begin{document}

\begin{frame}
\begin{pspicture}(6,6)
\pnode(0,0){A}
\pnode(6,0){B}
\pnode(6,6){C}
\pnode(0,6){D}
\pnode(1,1){P}
\pnode(5,1){Q}
\pnode(3,5){R}
\only<\thetmp>{\psclip{\pscircle[linestyle=none,dimen=middle](3,3){3}}}
\only<+->{\pspolygon*[linecolor=red](A)(B)(Q)(P)}
\only<+->{\pspolygon*[linecolor=green](B)(C)(R)(Q)}
\only<+->{\pspolygon*[linecolor=blue](C)(D)(R)}
\only<+->{\pspolygon*[linecolor=yellow](D)(A)(P)(R)}
\setcounter{tmp}{\value{beamerpauses}}
\only<\thetmp>{\endpsclip}
\end{pspicture}
\end{frame}

\end{document}


Section 9.6.4 Incremental Specifications of the beamer manual contains some other useful details.

-
@HiggsBoson I updated my answer; it is something like that what you want? – Gonzalo Medina Jul 28 '12 at 16:23