# Increment counter in TikZ

How to increment counter in TikZ using code below:

\newcounter{ga} %counter
\setcounter{ga}{1}
\begin{tikzpicture}
\foreach \t in {1,...,10}{
\pgfmathparse{Mod(\t,2) == 1 ? 1 : 0}
\ifnum\pgfmathresult > 0
\ga = \ga + 1
\fi
\end{tikzpicture}


In line 4 I need something similar to \ga = \ga + 1.

-
What is \ga? A TeX counter? A macro expanding to a number? – Caramdir Jun 24 '12 at 22:35
Then maybe you could post a complete MWE. – Caramdir Jun 24 '12 at 22:38
\stepcounter{ga}? – Caramdir Jun 24 '12 at 22:50
\stepcounter{ga} should do the trick. – Werner Jun 24 '12 at 22:51
You don't need that. As commented above, \begin{tikzpicture}\foreach \t in {1,...,10}{\pgfmathparse{Mod(\t,2) ==1?1:0}\ifnum\pgfmathresult>0\stepcounter{ga}\else\fi\node at (\t,0){\number\value{ga}};}\end{tikzpicture} is sufficient. – percusse Jun 25 '12 at 1:22

\documentclass{minimal}
\usepackage{tikz}
\newcounter{ga}\setcounter{ga}{1}

\begin{document}

\foreach \t in {1,...,10}{\ifodd\t \stepcounter{ga}\fi}
\thega
\end{document}


and an example with placing the value in a node:

\documentclass{minimal}
\usepackage{tikz}
\newcounter{ga}\setcounter{ga}{0}
\begin{document}

\begin{tikzpicture}
\foreach \t in {1,...,10}{\ifodd\t \stepcounter{ga}\fi}
\node [circle,draw] (a) at (0:1) {\thega};
\end{tikzpicture}

\end{document}

-
This doesn't work in a tikzpicture environment. – Monkey Supersonic Sep 23 '15 at 10:25
see edited answer – Herbert Sep 23 '15 at 11:30

It's not really a question about TikZ but about the package pgffor and pgfmath. With Tikz I prefer to use the TeX's macros here \newcount, it's more natural if you use \ifdim.

update

I forgot that I'm the author of Integer arithmetics functions in pgfmath, It's possible to use isodd. But \ifodd from Herbert's answer is the correct way with \newcount.

It's perhaps important to develop the difference between the answers. I feel reticent about mixing TeX's macros and LaTeX's macros.

1) It's important to notice that LaTeX’s counters are set globally.

2) The need to use the lower-level TeX system is not obvious but the spirit of TikZ and here for the macro \foreach, it's to work if it's possible locally. The difference is that TeX sets count registers locally. So to do a global assignment you have to do it deliberately like with \global\advance\ga by1 in my code.

A good document (tex-counts-and-latex-counters) is here from Joseph Wright

\documentclass[11pt]{article}
\usepackage{tikz}    % or \usepackage{pgffor,pgfmath}  pgf 2.1cvs

\begin{document}
\newcount\ga
\ga=1 %

\foreach \t in {1,...,10}{%
%\pgfmathparse{Mod(\t,2)==1?1:0}
\pgfmathparse{isodd(\t)}