Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

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}
share|improve this answer
    
Ok, thanks for the quick help, that does the trick for me. But just out of academic curiousity: Why can't I access the saved coordinates? –  x2b Jan 17 '12 at 23:11
    
@x2b: Sorry, I was working on a longer answer and pressed Save accidentally. –  Jake Jan 17 '12 at 23:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.