# TikZ counter and logical comparison

Does TikZ have counter that is similar to for loop in c language and logical comparison? e.g.,

\foreach \x in {0mm,11.200000mm,...,33.600000mm}
{
counter++; <== is this possible in tikz
if(counter == 0) <== is this possible in tikz
output something
}

-

You have a new possibility perhaps interesting for you with pgf 2.1 (or with the last version of pgffor), you can use a new option count. I create two counters \xi and \yi, automatically for each loop you get \xi=\xi+1 etc.

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[x=0.75cm,y=0.75cm]
\foreach \x [count=\xi] in {0,...,6}
{\foreach \y [count=\yi] in {0,...,6}
{\ifnum\xi=\yi
\node [draw, minimum size=0.666cm] at (\xi,-\yi) {$\infty$};
\else
\node [draw, minimum size=0.666cm] at (\xi,-\yi) {$\x\y$};
\fi }}
\end{tikzpicture}
\end{document}

-
this sounds an interesting thing. what is the initiative value for the counter, e.g. \xi? is it from 0? – warem Mar 2 '11 at 2:11
@warem: the pgfmanual gives /pgf/foreach/count=⟨macro⟩from⟨value⟩ so you can fix the initial value that you want! – Alain Matthes Mar 2 '11 at 6:25
got it. thank you. – warem Mar 2 '11 at 6:35

You can use the normal LaTeX counters for that. You can define your own one using \newcounter{name}, set it using \setcounter{name}{value} or add to it using \addtocounter{name}{value}.

You can test counter values using \ifnum 0=\value{name} <true> \else <false> \fi or with the help of the ifthen package.

Note that the content for \foreach is executed inside a group and assignments which should survive to the next iteration or after the end of the loop must be global. Luckily the above set and add macros are global assignments anyway.

-
@Martin for a counter in Latex, how to do arithmetic, e.g., counter++ in the above example? – warem Mar 1 '11 at 8:56
@warem: like I wrote \addtocounter{name}{1}. There is also the PGFmath version of it which accepts more complicated arithmetic: \pgfmathaddtocounter{name}{expression} – Martin Scharrer Mar 1 '11 at 9:06
@Martin \pgfmathaddtocounter is what i need. thank you. – warem Mar 2 '11 at 2:06
@Martin: how to define following c language: if(x == 1 && y % 4 == 0) pgfmanual is really a big stone. it is not easy to find something. and it takes time to finish reading it. i found in it that logical and is also &&, similar to c language, % uses mod. so i tried this: \ifnum\x=1 && mod(\y,4)=0 but it didn't work. by the way, how can i input enter in comment? the above content has paragraph style. but it came up in one big paragraph in the end. – warem Mar 2 '11 at 5:08
@Martin: i have read some other's example. none used complex logical comparison. at the beginning i hope to draw something in tikz depending on some complex logical comparison. now i give it up. my tex file is generated from my own program. some properties must be extracted from link list in c language. so it makes no sense to take much time in tikz in this part. i can generate the right sentences in my own program and write them into tex source file directly. anyway, thank you. – warem Mar 2 '11 at 8:16