# Is there any way to draw the same shapes at different positions efficiently, for example \foreach?

I know there is a similar question, but I do not think it gave the best solution.

Here is my code:

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\clip(-0.5,-1) rectangle (5.5,3.5);
% defining coordinates
\coordinate (1)   at (0,0);
\coordinate (2)   at (0,3);
\coordinate (3)   at (3,3);
\coordinate (4)   at (3,0);
\coordinate (1_2) at ($(1)!0.5!(2)$);
\coordinate (2_3) at ($(2)!0.5!(3)$);
\coordinate (3_4) at ($(3)!0.5!(4)$);
\coordinate (4_1) at ($(4)!0.5!(1)$);
\coordinate (1_3) at ($(1)!0.5!(3)$);
%draw \cry{4} at different positions
\draw[color=black] (1) node {\large \cry{4}};
\draw[color=black] (2) node {\large \cry{4}};
\draw[color=black] (3) node {\large \cry{4}};
\draw[color=black] (4) node {\large \cry{4}};
\draw[color=black] (1_3) node {\large \cry{4}};
\draw[color=black] (1_2) node {\large \cry{4}};
\draw[color=black] (2_3) node {\large \cry{4}};
\draw[color=black] (3_4) node {\large \cry{4}};
\draw[color=black] (4_1) node {\large \cry{4}};
\end{tikzpicture}


{\large \cry{4}} can be replaced by anything as you like. My question is how can I use more concise code to replace the second part of my code. As every time I draw the same thing,10 of them may be fine, but what if there are 100 of them, so I supposed there should be a more convenient way. I looked the manual, I saw \foreach, but it seems that I can only loop through single coordinates like \x and \y. Anyone have any ideas about this? Thanks.

As you have named the coordinates, just loop over their names:

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,calc}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\clip(-0.5,-1) rectangle (5.5,3.5);
% defining coordinates
\coordinate (1)   at (0,0);
\coordinate (2)   at (0,3);
\coordinate (3)   at (3,3);
\coordinate (4)   at (3,0);
\coordinate (1_2) at ($(1)!0.5!(2)$);
\coordinate (2_3) at ($(2)!0.5!(3)$);
\coordinate (3_4) at ($(3)!0.5!(4)$);
\coordinate (4_1) at ($(4)!0.5!(1)$);
\coordinate (1_3) at ($(1)!0.5!(3)$);
%draw \cry{4} at different positions
\foreach \coordname in {1,2,3,4,1_2,2_3,3_4,4_1,1_3}
\node [draw,font=\large] at (\coordname) {A};
\end{tikzpicture}
\end{document}


I have no idea where \cry comes from, so I just used an A instead.

You can also drop defining the coordinates, and use two nested \foreach loops to create the nodes. In the code below I also give them unique names, if you should need to draw e.g. arrows between some of them.

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach [count=\i] \x in {0,1.5,3}{
\foreach [count=\j] \y in {0,1.5,3} {
\node [draw,font=\large] (n-\i-\j) at (\x,\y) {A};
}
}

% example of drawing an arrow from one node to another
\draw [red,-latex] (n-1-1) to[out=20,in=200] (n-2-3);
\end{tikzpicture}
\end{document}


• Thank you so much, \cry is a crystallographic font used in the materials science! – ted930511 Feb 9 '16 at 18:07
• Hi, Tobjorn, by the way, comparing your code, I would like to ask:is it a better way to construct node like your code? my code is based other people's code.@Torbjørn T. – ted930511 Feb 9 '16 at 18:30
• @DanJiadong I edited my answer, was it something like that you had in mind? – Torbjørn T. Feb 10 '16 at 8:04