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\
beforecoordinate
).Also, in the first snippet, you're using
\draw
without any drawing commands, instead you're following withnode
. 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
, sincenode
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:Then you can draw circles at the same positions later on using
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):
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.