# Draw a checker pattern with a black X in the center

I am trying to recreate the following image in TikZ

Using some old code I was able to produce the following result

While I was able to produce the correct result, I feel that my solution was a bit strange as it required two passes. Any suggestions for alternative approaches, or improvements to the code are more than welcome.

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}[x=1cm]
\edef\size{4}
\foreach \x in {0,...,\size} \foreach \y in {0,...,\size}
{
\pgfmathparse{mod(\x+\y,\size) ? "none" : "black"}
\edef\colour{\pgfmathresult}
\path[draw=black, fill=\colour] (\x,\y) rectangle ++ (1,1);

\pgfmathparse{\x==\y ? "black" : "none"}
\edef\colour{\pgfmathresult}
\path[fill=\colour] (\x,\y) rectangle ++ (1,1);
}
\draw (0,0)--(0,\size+1)--(\size+1,\size+1)--(\size+1,0)--cycle;
\end{tikzpicture}

\end{document}


With tikz:

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
node distance = 0mm,
box/.style = {draw, minimum size=10mm, fill=black,
outer sep=0pt},
]
\edef\size{4}
\foreach \y in {0,...,\size}
\foreach \x in {0,...,\size}
{\ifnum\x=\y
\node[box]   at (\x,\size-\y) {};
\node[box]   at (\x,\y) {};
\else
\node[box,fill=none] at (\x,\y) {};
\fi
}
\end{tikzpicture}
\end{document}


Note: Value of \size had to be zero or any even natural number (0, 2, 4, ...)

• default baseline of above image is as (current bounding box.south)˙ For series of those images for different value ofsize is:
\documentclass{article}
\usepackage{tikz}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
\begin{figure}
\begin{tabularx}{\linewidth}{>{\hsize=0.5\hsize}C C >{\hsize=1.5\hsize}C}
\begin{tikzpicture}[baseline=(current bounding box.south),
node distance = 0mm,
box/.style = {draw, minimum size=10mm, fill=black,
outer sep=0pt},
]
\edef\size{0} % in this MWE the meaning of \size is changed
\foreach \y in {0,...,\size}
\foreach \x in {0,...,\size}
{\ifnum\x=\y
\node[box]   at (\x,\size-\y) {};
\node[box]   at (\x,\y) {};
\else
\node[box,fill=none] at (\x,\y) {};
\fi
}
\end{tikzpicture}
\caption{}
&
\begin{tikzpicture}[%baseline=(current bounding box.south),
node distance = 0mm,
box/.style = {draw, minimum size=10mm, fill=black,
outer sep=0pt},
]
\edef\size{1}
\foreach \y in {0,...,2*\size} % changed, now number of boxes is odd
\foreach \x in {0,...,2*\size} % changed,
{\ifnum\x=\y
\node[box]   at (\x,\size-\y) {};
\node[box]   at (\x,\y) {};
\else
\node[box,fill=none] at (\x,\y) {};
\fi
}
\end{tikzpicture}
\caption{}
&
\begin{tikzpicture}[baseline=(current bounding box.south),
node distance = 0mm,
box/.style = {draw, minimum size=10mm, fill=black,
outer sep=0pt},
]
\edef\size{2}
\foreach \y in {0,...,\size}
\foreach \x in {0,...,\size}
{\ifnum\x=\y
\node[box]   at (\x,\size-\y) {};
\node[box]   at (\x,\y) {};
\else
\node[box,fill=none] at (\x,\y) {};
\fi
}
\end{tikzpicture}
\caption{}
\end{tabularx}
\end{figure}
\end{document}


• How can this produce the correct case with only one black box? – N3buchadnezzar May 27 '19 at 15:33
• With \edef\size{1}? I'm not sure if I understood your comment correctly. – Zarko May 27 '19 at 15:36
• \size{1} produces 4 black squares not 1. Here is how it looks for n=1 and n=2, not the same baseheight either. i.imgur.com/0OamEbM.png – N3buchadnezzar May 27 '19 at 15:37
• Indeed. It should be \size{0}. – Zarko May 27 '19 at 15:41
• @N3buchadnezzar, images are aligned to their bottom side. also see "Note" in edited answer. – Zarko May 27 '19 at 16:06

A PSTricks solution only for fun purposes!

\documentclass[border=1pt]{standalone}
\usepackage{pstricks}
\def\obj#1{%
\pspicture[dimen=m](#1,#1)
\multips(0,0)(0,1){#1}{\multips(0,0)(1,0){#1}{\psframe(1,1)}}
\multips(0,0)(1,1){#1}{\psframe*(1,1)}
\multips(0,#1)(1,-1){#1}{\psframe*(1,-1)}
\endpspicture}

\begin{document}
\end{document}


# Edit

I invented the algorithm (that has not been patented yet) as follows. No nested loop is needed.

\documentclass[border=12pt]{standalone}
\usepackage[nomessages]{fp}
\usepackage{xintexpr}
\usepackage{pstricks}
\psset{unit=5mm}
\def\obj#1{%
\pspicture[dimen=m](#1,#1)
\FPeval\N{#1*#1-1}
\foreach \j in {0,...,\N}
{
\FPeval\y{trunc(\j/#1:0)}
\FPeval\x{\j-#1*y}
\xintifboolexpr{\x=\y||(\x+\y)=(#1-1)}
{\psframe[fillstyle=solid,fillcolor=black](\x,\y)(+\x+1,\y+1)}
{\psframe(\x,\y)(+\x+1,\y+1)}
}
\endpspicture}
\begin{document}
\end{document}


Edit: The following works for all values of size

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}
\edef\size{4}
\foreach \x in {0,...,\size} \foreach \y in {0,...,\size} {
\pgfmathsetmacro{\colour}{(\x==\y || \x+\y==\size) ? "black" : "none"}
\draw[fill=\colour] (\x,\y) rectangle ++ (1,1);
}
\end{tikzpicture}

\end{document}


• How can this produce the correct case with only one black box? – N3buchadnezzar May 27 '19 at 15:33
• The mod function was not a good choice, \pgfmathparse{\x+\y==\size ? "black" : "\colour"} is better. – AboAmmar May 27 '19 at 17:00

I do not see the reason for a double loop, nor complicated conditions.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[xboard/.style={insert path={
(0,0) grid (#1,#1)
foreach \X in {1,...,#1}
{(\X-0.5,\X-0.5) pic{bx} (\X-0.5,#1-\X+0.5) pic{bx}}}},
pics/bx/.style={code={\fill (-0.5,-0.5) rectangle (0.5,0.5);}}]
\draw[xboard=1] [xshift=2cm,xboard=3]
[xshift=4cm,xboard=5] [xshift=6cm,xboard=7];
\end{tikzpicture}
\end{document}
`