Draw a checker pattern with a black X in the center
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, ...)
addendum:
- default baseline of above image is as
(current bounding box.south)˙ For series of those images for different value of
size` 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}
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}
\foreach \i in {3,5,7}{\obj{\i}\quad}
\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}
\foreach \i in {1,3,5,7,9}{\obj{\i}\quad}
\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}