Graphics, Figures & TablesHow to draw Chess Board?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
xaero7
Posts: 2
Joined: Sat Apr 23, 2016 11:07 am

How to draw Chess Board?

Post by xaero7 »

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview} 
\PreviewEnvironment{tikzpicture}

\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}
	\pagestyle{empty}
	
\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;

\def\flag{0}

\foreach \x in {0, 1, 2, 3} {
	\foreach \y in {0, 1, 2, 3} {
		\ifnum \flag =0  {
			\filldraw[greenfill] (\x, \y) -- (\x +1, \y) -- (\x +1, \y +1) -- (\x, \y +1) -- cycle;
			\def\flag{1};
		}
		\else {
			\filldraw[whitefill] (\x, \y) -- (\x +1, \y) -- (\x +1, \y +1) -- (\x, \y +1) -- cycle;
			\def\flag{0};
		}		
		\fi		
	}
}


\end{tikzpicture}
	
\end{document} 
I'm trying to draw something like Chess Board, but it seems all the squares are green, no one is white!

how to modify?
QQ20160424104557.png
QQ20160424104557.png (598 Bytes) Viewed 5166 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

How to draw Chess Board?

Post by rais »

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
xaero7
Posts: 2
Joined: Sat Apr 23, 2016 11:07 am

Re: How to draw Chess Board?

Post by xaero7 »

Thank you very much! rais
Post Reply