3

Note: I'm aware of another response here, but its solution was a quick-fix rather than the underlying issue.

Say I want to create the following diagram but using iteration:

\begin{tikzpicture}
\foreach \x/\y in {1/10,2/11,3/12,4/13,5/14,6/15}
 {
    \draw (\x,0) circle (7pt);
    \draw (\x,0) node {$\y$};
 }
\end{tikzpicture}

I could easily exploit the fact that tikz chooses an appropiate center so that

\begin{tikzpicture}
\foreach \x in {10,11,...,15}
 {
    \draw (\x,0) circle (7pt);
    \draw (\x,0) node {$\x$}; 
 }
\end{tikzpicture}

works just as well. But if I don't want to use this exploit, and try

\begin{tikzpicture}
\foreach \x/\y in {1/10,2/11,...,6/15}
 {
    \draw (\x,0) circle (7pt);
    \draw (\x,0) node {$\y$};
 }  
\end{tikzpicture}

I get a bunch of errors, beginning with

Illegal unit of measure (pt inserted). [ }]

Two questions: it is possible to iterate over several variables in a list (let's assume the lists are large enough that it's not feasible to do each of the cases by hand as above), and what's causing the error?

Thanks

8
  • 1
    If one of the variables is sequential and starts at 1 (like the first in your case) you can use the count: \foreach [count=\x] \y in {10,11,...,15}{\draw (\x,0) circle (7pt);\draw (\x,0) node {$\y$};}
    – Guho
    Jan 23, 2016 at 4:47
  • 2
    This question is a duplicate of How to iterate \foreach with two variables and a list of values.
    – Werner
    Jan 23, 2016 at 7:16
  • 2
    @Werner Which might be a duplicate of Possible to combine "..." and "x/y" in TikZ's foreach?. Jan 23, 2016 at 9:29
  • 1
    @CookieMonster The boring answer is probably "because pgf isn't coded to support that syntax". So would you agree that we can close as a duplicate of my question? Jan 23, 2016 at 14:54
  • 1
    @CookieMonster: Not having a solution has no bearing on whether something is a duplicate or not; the system just doesn't allow for closure of a question of another without the target having answers. Additionally, if the question is convoluted but asks the same thing, it's the same thing.
    – Werner
    Jan 23, 2016 at 18:44

0

You must log in to answer this question.

Browse other questions tagged .