45

Can someone explain what does [count=\i] in a foreach mean?

Antal S-Z's answer to Fitting a list of points with TikZ and its \foreach use this. It is also mentioned in Increment loop variable in inner foreach loop, but I am not able to locate documentation on this in the TikZ-PGF manual (V2.10)

1
  • 1
    The TikZ manual has a nice index, which is quite useful for finding where an option is described.
    – Caramdir
    Jun 25, 2011 at 10:14

1 Answer 1

72

Section Options to customize the foreach-statement. (pages 507-508):

/pgf/foreach/count= <macro>from<value> (no default) This key allows <macro> to hold the position in the list of the current item. The optional from<value> statement allows the counting to begin from <value>.

So, for example, in

\foreach \x [count=\xi] in {a,...,e}

the \x will take the values a,b,c,d,e, while \xi will take the values 1,2,3,4,5, and in

\foreach \x [count=\xi from 3] in {a,...,e}

the \x will still take the values a,b,c,d,e, while \xi will now take the values 3,4,5,6,7.

2
  • Since which version of Tikz is this function available?
    – user8442
    Oct 8, 2011 at 10:01
  • 1
    (for Python people) it resembles Python's builtin "enumerate" function Aug 1, 2022 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .