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.

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}
share|improve this question
4  
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
add comment

1 Answer

up vote 9 down vote accepted

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.

share|improve this answer
    
Thanks egreg and percusse. –  Regis da Silva Jun 22 '12 at 12:25
add comment

Your Answer

 
discard

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.