Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to increment counter in TikZ using code below:

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

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

share|improve this question
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

2 Answers 2



\foreach \t in {1,...,10}{\ifodd\t \stepcounter{ga}\fi}
share|improve this answer

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.


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

\usepackage{tikz}    % or \usepackage{pgffor,pgfmath}  pgf 2.1cvs

\ga=1 %

\foreach \t in {1,...,10}{%
\ifnum\pgfmathresult>0 \global\advance\ga by1 \fi


Result : 6

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.