3

I'm using the presentation mode in beamer. There's a frame about random stars distributed in a rectangle. I made a pause effect on that slide. However, the next frame would give me a different distribution of random stars. How can I fix the positions of that random stars and also keep the pause effect??

enter image description hereenter image description here

\documentclass{beamer}

\mode<presentation> {
\usetheme{boxes}
\usecolortheme{rose}}

\usepackage{graphicx} % Allows including images
\usepackage{booktabs} % Allows the use of \toprule, \midrule and \bottomrule in tables
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}

\begin{frame}
\begin{figure}
\centering
\begin{tikzpicture}[scale=0.7]
\filldraw[fill= blue!40, very thick,opacity=0.8] (0,0) rectangle (6,6);\pause
\draw plot [blue!80, only marks, mark=star, mark size=3, domain=0:6, samples=300] (\x,{0.5*6+0.5*rand*6});\pause
\draw[color=red!80,very thick] (2,1) circle (0.5);
\end{tikzpicture}
\end{figure}

\end{frame}
\end{document} 
6
  • 9
    I think you want to call \pgfmathsetseed{n} to explicity set the initial seed for the pseudo-random number generator - then it will be the same each time it's drawn. n should be an integer.
    – Thruston
    Commented Mar 27, 2015 at 17:31
  • 3
    The macro mentioned by @Thruston should be invoked inside of the tikzpicture. Commented Mar 27, 2015 at 17:41
  • 1
    Quick and dirty solution? Write your TikZ picture to a file with the standalone package or the exteranalize TikZ library, then bring it in via \includegraphics. Commented Mar 27, 2015 at 18:50
  • Thanks for the help! It works very well! Now the positions are fixed!@Thruston.
    – Windy
    Commented Mar 27, 2015 at 21:33
  • Yes, this is another way to make it happen. Thank you so much, too. @MikeRenfro
    – Windy
    Commented Mar 27, 2015 at 21:34

1 Answer 1

3

Some explanation could be found here. I will do it again because your example is more interesting.

\documentclass{beamer}
\usepackage{pgfplots}

\begin{document}
\makeatletter
\def\pgfmathsaveseed{\xdef\pgfmath@savedseed{\pgfmath@rnd@z}}
\def\pgfmathloadseed{\xdef\pgfmath@rnd@z{\pgfmath@savedseed}}
\def\beamerframeseed{\only<1>{\pgfmathsaveseed}\only<2->{\pgfmathloadseed}}
\begin{frame}
    \beamerframeseed
    \centering
    \begin{tikzpicture}[scale=1.5]
        \filldraw[fill= blue!40, very thick,opacity=0.8] (0,0) rectangle (6,6);\pause
        \draw plot[blue!80,only marks,mark=star,mark size=3,domain=0:6,samples=60](\x,{0.5*6+0.5*rand*6});\pause
        \draw plot[blue!80,only marks,mark=star,mark size=3,domain=0:6,samples=60](\x,{0.5*6+0.5*rand*6});\pause
        \draw plot[blue!80,only marks,mark=star,mark size=3,domain=0:6,samples=60](\x,{0.5*6+0.5*rand*6});\pause
        \draw plot[blue!80,only marks,mark=star,mark size=3,domain=0:6,samples=60](\x,{0.5*6+0.5*rand*6});\pause
        \draw plot[blue!80,only marks,mark=star,mark size=3,domain=0:6,samples=60](\x,{0.5*6+0.5*rand*6});\pause
        \draw[color=red!80,very thick] (2,1) circle (0.5);
    \end{tikzpicture}
\end{frame}
\end{document}
3
  • why don't you simply fix the seed?
    – percusse
    Commented Mar 28, 2015 at 10:23
  • @percusse Image that you are working in handout mode (\pause's inactivated), where a beamer document is nothing but an article with specific paper size. Caring about seeds, in this case, is unnecessary and ... umm ... unromantic? So I would like to keep that romance in every mode. One may also write \only<1,3>{\pgfmathsaveseed}\only<2-4>{\pgfmathloadseed} to achieve something fantastic.
    – Symbol 1
    Commented Mar 28, 2015 at 11:27
  • That explains perfectly my mere existence on this site too :) I meant as a mechanism you can set and unset the seed and it will result in the same mechanism. Didn't mean as a criticism :)
    – percusse
    Commented Mar 28, 2015 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .