\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,...,8}
\foreach \y in {1,...,8}
if (\pgfmathparse{mod(\x+\y,2)==0}) then
\draw [fill=gray!15] (\x,\y) rectangle (\x+1,\y+1);
\draw [line width=.5mm] (1,1) -- (9,1) -- (9,9) -- (1,9) -- (1,1);
\foreach \x in {1,...,8}
\foreach \y in {1,...,2}
\draw [fill=red!50](\x+0.5,\y+0.5) circle (0.4cm);
\foreach \x in {1,...,8}
\foreach \y in {7,...,8}
\draw [fill=blue!50](\x+0.5,\y+0.5) circle (0.4cm);
\end{tikzpicture}
\end{document}
答案1
我宁愿做以下事情:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,...,8} {
\foreach \y [evaluate=\y as \z using {int(mod(\x+\y,2))}] in {1,...,8} {
\ifnum\z=0\relax
\draw[fill=gray!15] (\x,\y) rectangle (\x+1,\y+1);
\fi
}
}
\draw[line width=.5mm] (1,1) rectangle (9,9);
\foreach \x in {1,...,8}
\foreach \y in {1,2}
\draw[fill=red!50] (\x+0.5,\y+0.5) circle[radius=0.4];
\foreach \x in {1,...,8}
\foreach \y in {7,8}
\draw[fill=blue!50] (\x+0.5,\y+0.5) circle[radius=0.4];
\end{tikzpicture}
\end{document}
但实际上,你并不需要任何 if-else 语句:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,3,...,7}
\foreach \y in {1,...,8}
\draw[fill=gray!15] ({\x+mod(\x+\y,2)},\y) rectangle ++(1,1);
\draw[line width=.5mm] (1,1) rectangle (9,9);
\foreach \x in {1,...,8}
\foreach \y in {1,2}
\draw[fill=red!50] (\x+0.5,\y+0.5) circle[radius=0.4];
\foreach \x in {1,...,8}
\foreach \y in {7,8}
\draw[fill=blue!50] (\x+0.5,\y+0.5) circle[radius=0.4];
\end{tikzpicture}
\end{document}
答案2
如果你想要按照你的模板进行实施,你可以\tikzmath
使用如何在\foreach
TikZ 中跳过某个值?
\documentclass[tikz]{standalone}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}[radius=.4]
\tikzmath{
for \x in {1, ..., 8}{
for \y in {1, ..., 8} {
if mod(\x+\y,2)==0 then {
{ \draw [fill=gray!15] (\x, \y) rectangle +(1, 1); };
};
};
};
}
\draw [line width=.5mm] (1,1) rectangle (9,9);
\foreach \x in {1, ..., 8}
\foreach \y in {1, 2}{
\draw [fill=red!50] (\x+0.5, \y+0.5) circle[];
\draw [fill=blue!50] (\x+0.5, 9-\y+0.5) circle[];
}
\end{tikzpicture}
\end{document}
但是,我们不必经历所有这些,\x
我们可以从一开始就跳过每一秒:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[radius=.4]
\foreach \x in {1, ..., 4}
\foreach \y in {1, ..., 8}
\draw[shift=(right:iseven \y), fill=gray!15] (2*\x-1, \y) rectangle +(1, 1);
\draw [line width=.5mm] (1,1) rectangle (9,9);
\foreach \x in {1, ..., 8}
\foreach \y in {1, 2}{
\draw [fill=red!50] (\x+0.5, \y+0.5) circle[];
\draw [fill=blue!50] (\x+0.5, 9-\y+0.5) circle[];
}
\end{tikzpicture}
\end{document}
我还擅自使用了radius = .4
而不是.4cm
来避免混合坐标和画布坐标系。
答案3
使用({NiceArray}
您nicematrix
需要多次编译)。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\NewDocumentCommand{\Blue}{}
{\tikz [baseline] \draw [fill=blue!50] circle (0.4cm) ; }
\NewDocumentCommand{\Red}{}
{\tikz [baseline] \draw [fill=red!50] circle (0.4cm) ; }
$\begin{NiceArray}{>{\rule[-16pt]{0pt}{32pt}}cccccccc}[hvlines,rounded-corners]
\CodeBefore
\chessboardcolors{gray!10}{}
\Body
\Blue & \Blue & \Blue & \Blue & \Blue & \Blue & \Blue & \Blue \\
\Blue & \Blue & \Blue & \Blue & \Blue & \Blue & \Blue & \Blue \\
\\
\\
\\
\\
\Red & \Red & \Red & \Red & \Red & \Red & \Red & \Red \\
\Red & \Red & \Red & \Red & \Red & \Red & \Red & \Red \\
\end{NiceArray}$
\end{document}