13

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}%

\newcommand{\grade}{
    \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);
            \grade
            %animation
            \ifnum \j < 5
                \fill[blue] (\i,\j) rectangle ++(1,1);
            \else
                \fill[blue] (\j,\i) rectangle ++(1,1);
            \fi
        \end{tikzpicture}
    }
}
\end{document}
  • 4
    There is the primitive conditional \ifodd – egreg Jun 22 '12 at 12:08
  • 2
    \ifnum \j mod 3 = 0 or other number? how to calculate? – Regis da Silva Jun 22 '12 at 12:18
13

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}

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

\begin{tikzpicture}[y=-1cm]\grade
\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}

enter image description here

I hope I've understood the goal correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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