# define different positions in foreach

in the following code

\begin{tikzpicture}
\foreach \pos in {0,...,5} {
\pic at (1.85*\pos,0) {cross};
\node[formula] at ($(1.85*\pos,0)+(0.4,0.4)$) {Na};
}
\end{tikzpicture}


can different positions be assigned for example for position 1 1.85, for position 2 2.85, for position 3 1.85

• Can you provide the full code? I get an error about the \pic command. Also, do you want to alternate between positions 1.85 and 2.85? – Alenanno Dec 14 '15 at 8:22
• You can replace {0,...,5} by {1.85, 2.85, 1.85} and replace 1.85*\pos by \pos. – Kpym Dec 14 '15 at 12:04

## 1 Answer

You mean to shift the node position for 1cm? If this is case, than try:

1.

\begin{tikzpicture}
\foreach \pos in {0,...,5} {
\pic at (1.85*\pos,0) {cross};
\node[formula, xshift=1cm] at ($(1.85*\pos,0)+(0.4,0.4)$) {Na};
}
\end{tikzpicture}


2.

\begin{tikzpicture}
\foreach \pos in {0,...,5} {
\pic at (1.85*\pos,0) {cross};
\node[formula] at ($({1+1.85*\pos},0)+(0.4,0.4)$) {Na};
}
\end{tikzpicture}


Addendum:

3.

\begin{tikzpicture}
\foreach \pos in {0,...,5} {
\pic at (1.85*\pos,0) {cross};
\node[formula] at ($(1.85*\pos,0)+(1.4,0.4)$) {Na};
}
\end{tikzpicture}


4.

\begin{tikzpicture}
\foreach \pos in {0,...,5} {
\pic at (1.85*\pos,0) {cross};
\node[formula] at ({1.4+1.85*\pos},0.4) {Na};
}
\end{tikzpicture}


5.

\begin{tikzpicture}
\foreach \pos in {0,...,5} {
\pic (cross\pos) at (1.85*\pos,0) {cross};
\node[formula,above right=1cm and 0.4cm of cross\pos] {Na};
}
\end{tikzpicture}

1. etc
• Thank you for your very helpful answers, but your last example is not working. \begin{tikzpicture} \foreach \pos in {0,...,5} { \pic (cross\i) at (1.85*\pos,0) {cross}; \node[formula,above right=1cm and 0.4cm of cross\i] {Na}; } \end{tikzpicture} – Hany Feb 15 '16 at 6:01
• Short inspection of example show, that in it was inconsistent use of variable \pos (in two places was by mistake replaced with not defined \i). It is corrected now. – Zarko Feb 15 '16 at 11:53
• The corrected code gave me an error message Package PGF Math Error: Unknown operator a' or an' (in '1and 0.4 of cross0') – Hany Feb 17 '16 at 6:25
• Do you load the TikZ library positioning? As you not provide complete MWE, I consider that you are interested only on picture code. Please, provide MWE (by editing your question) that I can see, how you test above solutions. All given code snippets I test in my "test-bed" which is rather overwhelming regarding to their necessities. – Zarko Feb 17 '16 at 6:40