[Tex/LaTex] Draw a checker pattern with a black X in the center

code-reviewtikz-pgf

I am trying to recreate the following image in TikZ

enter image description here

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

enter image description here

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}

Best Answer

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}

enter image description here

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

addendum:

  • 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}

enter image description here