# How does the counter in TikZ \foreach work?

I want to partition a line segment into 5 segments of equal length and put tick marks at the new partition points. I want the tick marks to appear in black. Next I want to partition each new segment into 5 segments of equal length and want to color only the new tick marks in blue.

To achieve this task, I used TikZ and following is the code.

\documentclass[11pt]{amsart}
\usepackage{pgf,tikz}

\begin{document}
\begin{tikzpicture}
\draw (0,10)-- (15,10);
\foreach \x in {0,...,5}
\draw (3*\x,10)--(3*\x,9.8);
\foreach \x in {1,...,4,6,...,9,11,...,14,16,...,19,21,...,24}
\draw[draw=blue] (3*\x/5,10)--(3*\x/5,9.8);
\end{tikzpicture}

\end{document}


However, the outcome is not the desired outcome.

So I read the manual and it suggests that I should include at least two starting numbers. I edited the code accordingly, and obtained the desired outcome. The new code is given below.

\documentclass[11pt]{amsart}
\usepackage{pgf,tikz}

\begin{document}
\begin{tikzpicture}
\draw (0,10)-- (15,10);
\foreach \x in {0,...,5}
\draw (3*\x,10)--(3*\x,9.8);
\foreach \x in {1,...,4, 6,7,...,9, 11,12,...,14,16,17,...,19, 21,22,...,24}
\draw[draw=blue] (3*\x/5,10)--(3*\x/5,9.8);
\end{tikzpicture}

\end{document}


Here is my puzzle. If you give the starting number and the ending number to a counter that knows the increment is by 1(by default) then you do not have to give two starting numbers.

This seems to be the case, as the following works.

\foreach \x in {0,...,5}
\draw (3*\x,10)--(3*\x,9.8);


But, the counter seems to loose this capability the second time around as I have to give two numbers to remind the counter that the increment is by 1. (This is my hypothesis.)

What is the reason behind this?

-
I am not sure, but it seems that if you have the sequence 4,6,…,9, how do you determine that the step size is one? From the 4,6 it appears that the step size might be 2. But once you add the second value, 4,6,7,…9 then from 7...9 the step size is determined to be one from the 6,7. –  Peter Grill Sep 30 '11 at 0:13

By trial and error, it seems like whenever TikZ has an ambiguous step increment size, it simply assumes the previous integer current index minus one, as the step size. In other words, when you enter {1,...4,6,...20}, it gives you {1,2,3,4,6,11,16} with step size 6-1=5and similarly if you enter {1,...4,8.7,...25} you get {1,2,3,4,8.7, 16.4, 24.09999} with step size 8.7-1=7.7. You can test it for yourself using

\begin{tikzpicture}
\foreach \x in {1,...,4,8,...,25}
{\node at (\x / 2,0) {$\x$};}
\end{tikzpicture}


It also applies to letters such that

\begin{tikzpicture}
\foreach \x [count = \xi] in {a,...,c,f,...,z}
{\node at (\xi,0) {$\x$};}
\end{tikzpicture}


results with a,b,c,f,k,p,u,z

-
Nice investigative work! And I didn't know that the ... syntax worked with letters. Good to know. –  Loop Space Sep 30 '11 at 7:50

This is not an answer to your question but it's a way of drawing your black and blue ticks with two \foreach help.

\documentclass[11pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw (0,10)-- (15,10);
\foreach \x in {0,...,4}{
\draw (3*\x,10)--++(0,-0.2);
\foreach \j in {1,...,4}
\draw[draw=blue] ({3*(\x+\j/5)},10)--++(0,-0.2);
}
\draw (3*5,10)--++(0,-0.2);
\end{tikzpicture}
\end{document}


-