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 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?

share|improve this question

3 Answers 3

up vote 10 down vote accepted

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 :)

share|improve this answer

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.)

share|improve this answer

\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.

share|improve this answer
    
Is there an advantage to \let\foo\pgfmathresult so far as good practise goes? –  Freddie Witherden Dec 7 '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 S-Z Dec 8 '10 at 2: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). –  Loop Space Dec 8 '10 at 10:44

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.