5

I'm using beamer and I have a randomly colored grid (three colors, 4 x 19 grid) on the slides. Since I use the \pause command, it generates a new grid every time I click to the next point. How do I force it to remember the first grid it generated? I know it should be something with redefining the color after the first generated grid (maybe \edef?).

So this is my code for the grid:

\documentclass[a4paper,12pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}

\pgfmathdeclarerandomlist{MyRandomColors}{%
{red}%
{blue}%
{green}%
}

\begin{document}
\begin{tikzpicture}[x=0.6cm,y=0.8cm]
\foreach \x in {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
\foreach \y in {0,1,2,3}
{
    \pgfmathrandomitem{\RandomColor}{MyRandomColors} 
    \draw[\RandomColor, fill=\RandomColor] (\x,\y) circle (0.2cm);
}
\end{tikzpicture}
\end{document}

enter image description here

8

A simple way is to save the grid to a box:

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\newbox{\mygrid}
\savebox{\mygrid}{%
\begin{tikzpicture}[x=0.6cm,y=0.8cm]
\foreach \x in {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
\foreach \y in {0,1,2,3}
{
    \pgfmathrandomitem{\RandomColor}{MyRandomColors} 
    \draw[\RandomColor, fill=\RandomColor] (\x,\y) circle (0.2cm);
}
\end{tikzpicture}
 }

%when you want to use the grid, you just call \usebox

\usebox{\mygrid}

\end{document}

You may also place the box within a node for placement control.

9

Specifying a seed \pgfmathsetseed{1} within your tikzpicture should fix the problem.

\documentclass[10pt,a4paper]{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}

\pgfmathdeclarerandomlist{MyRandomColors}{%
    {red}%
    {blue}%
    {green}%
}

\begin{document}
    \begin{frame}
        \begin{tikzpicture}[x=0.6cm,y=0.8cm]
        \pgfmathsetseed{1}%
        \foreach \x in {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
        \foreach \y in {0,1,2,3}
        {
            \pause
            \pgfmathrandomitem{\RandomColor}{MyRandomColors} 
            \draw[\RandomColor, fill=\RandomColor] (\x,\y) circle (0.2cm);
        }
        \end{tikzpicture}
    \end{frame}

\end{document}
  • 1
    Note that this leads to all random numbers being the same on every slide, which may not be desired. – schtandard May 1 at 12:52
3

Alternatively to saving the grid in a box, you could compute the random color order only once and use it when creating the grid:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{etoolbox}

\pgfmathdeclarerandomlist{MyRandomColors}{%
    {red}%
    {blue}%
    {green}%
}

\makeatletter
    \def\colorrows{\@gobble}
    \foreach \row in {0,...,3} {
        \def\colors{\@gobble}
        \foreach \col in {0,...,18} {
            \pgfmathrandomitem{\randomcolor}{MyRandomColors}
            \xappto\colors{,\randomcolor}
        }
        \xappto\colorrows{,{\colors}}
    }
    \edef\colorrows{\colorrows}
\makeatother

\begin{document}

\begin{tikzpicture} [x=0.6cm, y=0.8cm]
    \foreach \row [count=\y] in \colorrows {
        \foreach \col [count=\x] in \row {
            \draw [\col, fill=\col] (\x,\y) circle (0.2cm);
        }
    }
\end{tikzpicture}

\end{document}

The \@gobbles get rid of the first comma added by each loop.

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.