TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

and an example with placing the value in a node:


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

share|improve this answer
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.


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.