12

I am trying to produce a 6-by-6 checkboard of small circles, alternating colour between red and black.

To do this I have attempted to use \pgfmathparse and \ifnum in order to decide if to colour a circle red or back:

\documentclass[11pt]{article}
\pagestyle{empty}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,5} \foreach \y in {0,...,5}
{
  \pgfmathparse{mod(\x+\y,2)}
  \ifnum\pgfmathresult=0{\path[fill=black] (\x*0.5cm,\y*0.5cm) circle (0.1cm);}
  \else{\path[fill=red] (\x*0.5cm,\y*0.5cm) circle (0.1cm);}\fi
}
\end{tikzpicture}
\end{document}

Although the code errors when I attempted to run it through pdflatex with ! Missing = inserted for \ifnum. and ! Missing number, treated as zero. it does produce the expected result.

What precisely am I doing wrong with regards to the calculation and how should one go about producing such a picture?

3 Answers3

13

Since you are already using the pgfmath library, you may as well use the conditional syntax from that:

\documentclass[11pt]{article}
\pagestyle{empty}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,5} \foreach \y in {0,...,5}
{
  \pgfmathparse{mod(\x+\y,2) ? "black" : "red"}
  \edef\colour{\pgfmathresult}
  \path[fill=\colour] (\x*0.5cm,\y*0.5cm) circle (0.1cm);
}
\end{tikzpicture}
\end{document}

Produces:

alt text

with no errors. (I'm using TikZ/PGF 2.10; I'm not sure when this conditional syntax was implemented.)

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
12

you just need to add int before the modulo operator (otherwise it returns a float that \ifnumdoesn't like), thus:

\pgfmathparse{int(mod(\x+\y,2))}

and all is good :)

Sylvain
  • 731
2

\ifnum compares two integers so you must ensure that the numbers are really integers. So a working code, combining the previous answers could be:

\documentclass[11pt]{article}
\pagestyle{empty}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,5} 
  \foreach \y in {0,...,5}
  {
    \pgfmathparse{int(mod(\x+\y,2))}
    \let\r\pgfmathresult
    \ifnum\r=0
      \path[fill=red] (\x*0.5cm,\y*0.5cm) circle (0.1cm);
    \else
      \path[fill=black] (\x*0.5cm,\y*0.5cm) circle (0.1cm);
    \fi
  }
\end{tikzpicture}
\end{document}

Observe that instead of using \pgfmathresult directly, its value it is dumped in the \r macro.

rgallego
  • 2,112
  • Is there an advantage to \let\foo\pgfmathresult so far as good practise goes? – Freddie Witherden Dec 07 '10 at 21:33
  • 1
    @Freddie: I've noticed that \pgfmathsetmacro{\r}{int(...)} doesn't return an integer (you get the .0 on the end), which you need the \let\r\pgfmathresult for. Also, you never know when a TikZ command will overwrite \pgfmathresult, so saving it is a good idea if you need to refer to it later. However, in a case like this, I don't think there's any need for the \let\r\pgfmathresult. It doesn't hurt, though. – Antal Spector-Zabusky Dec 08 '10 at 02:53
  • @Freddie, @Antal: in the code that I posted, the \edef is necessary. If you put color=\pgfmathresult then TeX complains (try it). So Antal is correct (on both counts). – Andrew Stacey Dec 08 '10 at 10:44
  • having to do this is sickeningly disappointing. :-( – Jason S Mar 03 '16 at 22:29