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}
You can try this, using the calc
library (See Section 13.5 Coordinate Calculations of the pgf manual):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (2,2);
\coordinate (b) at (0,-2);
\node[draw=red] at (a) {a};
\node[draw=red] at (b) {b};
\draw[help lines] (-1,-3) grid (3,3);
\draw ($ (a) + (0,1) $) -- ($ (b) + (0,1) $);
\end{tikzpicture}
\end{document}
To answer the follow-up, you can use partway modifiers (Section 13.5.3 of the manual). For example, the meaning of
(1,2)!.75!(3,4)
is "the coordinate that is three quarters on the way from (1,2) to (3,4)." A little complete example:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (2,2);
\coordinate (b) at (0,-2);
\node[draw=red] at (a) {a};
\node[draw=red] at (b) {b};
\draw[help lines] (-1,-3) grid (3,3);
\draw ($ (a) + (0,1) $) -- ($ (b) + (0,1) $);
\draw[blue,thick] (-1,2) -- ($ (a)!0.5!(b) $) -- (3,2);
\draw[magenta,thick] (2,-2) -- ($ (a)!0.75!(b) $) -- ($ (a)!0.25!(b) $) -- (3,2);
\end{tikzpicture}
\end{document}
Best Answer
The problem is that, inside your
\bacterium
macro you use a\tikz{}
command to draw the bacterium. This command creates its own picture, so that each bacterium has its own coordinate system. In this coordinate system the bacteriumm is placed randomly, but then that picture is cropped to the size of the bacterium, so there is no difference if the bacterium is located at (30,10) or at (0,0), becacuse after the cropping it will look the same. In addition, you wrap that picture inside a\scalebox
, which makes the whole picture to be treated as a single character.The main for loop invokes
\bacterium
to create that set of pictures, and places them one after the other, as when composing a word from several characters.As it was said other times at other places, it is not a good idea to nest tikz pictures.
What you need is that all the bacteria share the same coordinate system, i.e. they are all in the same tikz picture. The following code achieves this goal, by replacing your
\tikz
command by a tikzscope
which allows the specification of a scale factor and a rotation factor.