1

I need to iterate over two lists:

\begin{tikzpicture}[x=5mm, y=5mm]
    \foreach \x in {0,2,...,8} {
        \node[] at (\x, 0) {value};
    }
\end{tikzpicture}

The "value" has to be a value from the second list: {2, 3, 5, 7, 11} (yes, these are first five prime numbers).

I know, TeX is not really a programming language, but is there a way to do so?

Nesting foreach doesn't solve the problem:

\foreach \x in {0,2,...,8} {
    \foreach \value in {2,3,5,7,11} {
        \node[] at (\x, 0) {\value};
    }
}

The output looks as expected like this:

output

Solution (thank you Pier Paolo):

\foreach \x/\value in {0/2,2/3,4/5,6/7,8/11} {
    \node[] at (\x, 0) {\value};
}
georgmierau
  • 1,588
  • 2
    If your list is relatively short you could go with: \foreach \x/\value in {0/2,2/3,4/5,6/7,8/11} \node at (\x,0) {\value};. – Pier Paolo May 20 '15 at 10:02
  • Works like a charm! Thank you. Will I get performance problems while compiling with longer lists? – georgmierau May 20 '15 at 10:06
  • 1
    I don't think performance would be an issue, but this way you have to manually input all entries (i.e., all the values that the control variable can assume in the loop) and cannot use the ... facility to automatically compute the step and insert all missing values between first and last. – Pier Paolo May 20 '15 at 10:11
  • You cannot nest foreach for this example to work. The result is correct because for each x value all the \value values are evaluated. Hence @PierPaolo's solution is not an alternative it is the only correct one. – percusse May 20 '15 at 11:47

0 Answers0