我正在使用beamer
一个随机颜色的网格(三种颜色,4 x 19 网格) 在幻灯片上。由于我使用了该\pause
命令,每次单击下一个点时,它都会生成一个新网格。我如何强制它记住它生成的第一个网格?我知道它应该是在第一个生成的网格之后重新定义颜色的东西(也许\edef
?)。
这是我的网格代码:
\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}
答案1
一种简单的方法是将网格保存到一个盒子中:
\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}
您也可以将盒子放置在节点内以进行放置控制。
答案2
\pgfmathsetseed{1}
在 tikzpicture 中指定一个种子应该可以解决问题。
\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}
答案3
或者,除了将网格保存在盒子中之外,您还可以只计算一次随机颜色顺序,并在创建网格时使用它:
\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}
s\@gobble
去掉每个循环添加的第一个逗号。