# How to use mod operation in latex with tikz

I need something similar to

\ifnum \j mod 2 = 0


rest of a number, but do not know how to do this

in line 22.

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{0pt}%

\foreach \k in {0,...,9}{
\draw[dashed] (\k,0) -- ++(0,10);
\draw[dashed] (0,\k) -- ++(10,0);
}
}

\begin{document}
\foreach \j in {1,...,9}{
\foreach \i in {1,...,9}{
\begin{tikzpicture}[y=-1cm]
\clip[fill=white] (1,1) rectangle (10,10);
%animation
\ifnum \j < 5
\fill[blue] (\i,\j) rectangle ++(1,1);
\else
\fill[blue] (\j,\i) rectangle ++(1,1);
\fi
\end{tikzpicture}
}
}
\end{document}

-
There is the primitive conditional \ifodd – egreg Jun 22 '12 at 12:08
\ifnum \j mod 3 = 0 or other number? how to calculate? – Regis da Silva Jun 22 '12 at 12:18

As egreg mentions there are already primitives available. Also the CVS version of TikZ/PGF 2.1 will bring iseven,isodd,isprime functions. However you can also use a slightly redundant ifthenelse function as follows:

\documentclass{article}
\usepackage{tikz}

\foreach \k in {0,...,9}{
\draw[dashed] (\k,0) -- ++(0,10) (0,\k) -- ++(10,0);
}
}
\begin{document}

\foreach \j in {1,...,9}{
\foreach \i in {1,...,9}{
\pgfmathparse{Mod(\j,2)==0?1:0}
\ifnum\pgfmathresult>0
\fill[blue] (\i,\j) rectangle ++(1,1);
\else
\fill[blue] (\j,\i) rectangle ++(1,1);
\fi
}
}
\end{tikzpicture}
\end{document}


I hope I've understood the goal correctly.

-
Thanks egreg and percusse. – Regis da Silva Jun 22 '12 at 12:25