xaero7 wrote:
I'm trying to draw something like Chess Board, but it seems all the squares are green, no one is white!
Even without the additional braces in your
\ifnum
/
\else
statements, your
\def\flag{..}
is seen inside a group (from
\foreach
), thus your
\flag
reverts to 0 (what you set it to outside the group), whenever leaving the group (which
\foreach \y in {...}{...}
does for every value of \y).
You could use
\gdef
instead of
\def
to redefine \flag in a way that it keeps its new value even after leaving the group it was used in, but that would probably give you the desired effect only if you're using an odd number of values for \y---or if you toggle your \flag inside the \x-
\foreach
, as well.
Another idea would be to check both \x and \y values, if they're odd/even, then you don't need your \flag anymore:
Code: Select all
\documentclass{standalone}
\usepackage{tikz}
\definecolor{fillcolor}{rgb}{0.008, 0.753, 0.353} %rgb(2, 192, 90)
\definecolor{drawcolor}{rgb}{0, 0.376, 0.173} %rgb(0, 96, 44)
\begin{document}
\begin{tikzpicture}
\tikzstyle{greenfill} = [fill=fillcolor!20, draw=drawcolor]
\tikzstyle{whitefill} = [fill=white, draw=drawcolor]
\filldraw[greenfill] (0,0) -- (1,0) -- (1,1) -- (0, 1) -- cycle;
\foreach \x in {0, 1, 2, 3} {
\foreach \y in {0, 1, 2, 3} {
\ifodd\x
\ifodd\y % \x odd, \y odd
\filldraw[greenfill] (\x, \y) -- (\x +1, \y) -- (\x +1, \y +1) -- (\x, \y +1) -- cycle;
\else % \x odd, \y even
\filldraw[whitefill] (\x, \y) -- (\x +1, \y) -- (\x +1, \y +1) -- (\x, \y +1) -- cycle;
\fi
\else
\ifodd\y % \x even, \y odd
\filldraw[whitefill] (\x, \y) -- (\x +1, \y) -- (\x +1, \y +1) -- (\x, \y +1) -- cycle;
\else % \x even, \y even
\filldraw[greenfill] (\x, \y) -- (\x +1, \y) -- (\x +1, \y +1) -- (\x, \y +1) -- cycle;
\fi
\fi
}
}
\end{tikzpicture}
\end{document}
KR
Rainer