[pstricks] psRandom (PSTricks-add command) and Beamer overlays

Alexander Grahn A.Grahn at hzdr.de
Wed Sep 19 14:50:18 CEST 2012

On Wed, Sep 19, 2012 at 01:09:16PM +0200, Christian Häger wrote:
>Hello,
>
>consider the following minimal example, where the \psRandom command is used to draw random points on a slide, together with an overlay.
>
>\documentclass{beamer}
>
>\begin{document}
>\begin{frame}
>	\begin{pspicture}(5,5)
>		\psRandom[randomPoints=100](0,0)(5,5){\pscircle(2.5,2.5){2.5}}
>	\end{pspicture}
>	\uncover<2>{I am an overlay}
>\end{frame}
>\end{document}
>
>The effect is that on the second slide, where the overlay appears, the random points are changing. I assume that this is because beamer automatically copies the \psRandom command on each overlay-slide and therefore draws new random points on every slide. Is there a possibility to have the same random points appear on every slide? I don't know how the \psRandom command works but maybe it uses some input seed for a random number generator which could be frozen for the slide?

You are right, the Postscript code for calculating the random dots is
copied on every slide. The calculation itself is deferred to the ps2pdf
conversion step or to visualisation time and executed (once per page) by
the Postscript interpreter. Therefore you get different sets of random dots.

Fortunately, one can distill the graphics once into a PDF Form-XObject
and replay it deliberately often without re-executing the Postscript
code. This is, e. g., implemented in the media9 LaTeX package. Making use
of one of the macros defined therein, I came up with the following
solution (Replaying the Form XObject is done using the /SP pdfmark):

\documentclass{beamer}
\usepackage{media9}

\ExplSyntaxOn
\let\xform\mix_pdfxform:n
\def\lastxform{\g_mix_pdflastxform_tl}
\ExplSyntaxOff

\newsavebox\mysbox
\sbox{\mysbox}{%
\begin{pspicture}(5,5)
\psRandom[randomPoints=100](0,0)(5,5){\pscircle(2.5,2.5){2.5}}
\end{pspicture}%
}

\begin{document}
\begin{frame}
\begin{pspicture}(5,5)
\only<1>{\xform{\mysbox}}
\special{ps: mark \lastxform /SP pdfmark }
\end{pspicture}
\uncover<2>{I am an overlay}
\end{frame}

\end{document}