[Tex/LaTex] Naming coordinates in a foreach loop

beamerrandom numberstikz-pgf

I have the following problem: I am using beamer together with tikz, and I want to use some random points in a drawing which is shown over a couple of slides.
If I use the normal random method, the points are regenerated after each slides, having different coordinates which I dont want:

\foreach \x in {1/6,3/6,5/6}
  \foreach \y in {1/6,3/6,5/6}
    \draw[fill,scale=1] ($ (\x,\ y) + 1/10 * (rand, rand) $)
      \coordinate ( \x\y ) node[circle, fill, inner sep=1pt] {};

Obviously I need to somehow save the coordinates for later use.
I thought that I could to this using for loops declaring coordinates:

\foreach \x in {1/6,3/6,5/6}
  \foreach \y in {1/6,3/6,5/6}
    \coordinate( \x\y ) at ($ (\x,\ y) + 1/10 * (rand, rand) $);

This seems to work, but I can't access the coordinates later:

\foreach \x in {1/6,3/6,5/6}
  \foreach \y in {1/6,3/6,5/6}
    \draw[fill,scale=1] ( \x\y )
      \coordinate ( \x\y ) node[circle, fill, inner sep=1pt] {};

This piece of code gives me the error: "No shape named 1/61/6."

I don't know if I am going about this in the right way, I would really appreciate any help.

Best Answer

There are some errors/quirks in your code: In your first example, you're using the incorrect syntax \draw ... \coordinate. You should use \draw ... coordinate (note the missing \ before coordinate).

Also, in the first snippet, you're using \draw without any drawing commands, instead you're following with node. This is technically okay, but you should really either use \path ... node, or just a \draw ... circle or \fill ... circle command. I would suggest you use \fill ... circle, since node is a bit to powerful if you just need small circles.

Also, there's a spurious space in \ y. There also shouldn't be spaces in between the factor and the coordinate in ($ (\x,\y) + 1/10 * (rand, rand) $). In general, you have to be careful with spaces: When you name coordinates, the spaces matter: \coordinate ( A ) is not the same as \coordinate (A). Correcting all this, your first snippet, which draws the circles for the first time and saves their coordinates, could look like this:

\foreach \x in {1/6,3/6,5/6}
  \foreach \y in {1/6,3/6,5/6}
    \fill ($ (\x,\y) + 1/10*(rand, rand) $) circle [radius=1pt] coordinate   (\x\y);

Then you can draw circles at the same positions later on using

\foreach \x in {1/6,3/6,5/6}
  \foreach \y in {1/6,3/6,5/6}
    \fill  (\x\y) circle [radius=1pt];

Here's a complete example (it's generally a good idea to post questions using complete examples like this, since it makes finding errors much easier):

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{frame}{One}
\begin{tikzpicture}
\foreach \x in {1/6,3/6,5/6}
  \foreach \y in {1/6,3/6,5/6}
    \fill ($ (\x,\y) + 1/10*(rand, rand) $) circle [radius=1pt] coordinate   (\x\y);
\end{tikzpicture}
\end{frame}

\begin{frame}{Two}
\begin{tikzpicture}
\foreach \x in {1/6,3/6,5/6}
  \foreach \y in {1/6,3/6,5/6}
    \fill  (\x\y) circle [radius=1pt];
\end{tikzpicture}
\end{frame}

\end{document}

Alternatively, instead of saving the coordinates, you could set the seed for the random number generator by setting \pgfmathsetseed{<integer>}. That would have the added benefit that you can try out different values for the seed and find the one that gives the nicest / "most random" pattern, and is reproducible.

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}



\begin{frame}{One}
\begin{tikzpicture}
\pgfmathsetseed{1}
\foreach \x in {1/6,3/6,5/6}
  \foreach \y in {1/6,3/6,5/6}
    \fill ($ (\x,\y) + 1/10*(rand, rand) $) circle [radius=1pt] ;
\end{tikzpicture}
\end{frame}

\begin{frame}{Two}
\begin{tikzpicture}
\pgfmathsetseed{1}
\foreach \x in {1/6,3/6,5/6}
  \foreach \y in {1/6,3/6,5/6}
    \fill ($ (\x,\y) + 1/10*(rand, rand) $) circle [radius=1pt] ;
\end{tikzpicture}
\end{frame}

\end{document}
Related Question