我正在尝试学习 TikZ。
我写了以下小文档
\documentclass[tikz,border=12pt]{standalone}
\usepackage{ifthen}
\usepackage{pgf, tikz}
\usetikzlibrary{graphs,calc}
\makeatletter
\newcommand*\circled[1]{\smash{\tikz[baseline=(char.base)]{
\node[anchor=text, shape=circle,fill,draw,inner sep=0pt,text=white,minimum size=1.4em] (char) {#1\strut};}}}
\makeatother
\begin{document}
\begin{tikzpicture}
% \draw(4,-1) rectangle(8,4);
\foreach \y in {1, ..., 4}
\foreach \x in {1, ..., 8} {
\ifthenelse{\equal{\x}{5} \AND \equal{\y}{3}}{\draw(\x,\y)rectangle(0.4cm,0.4cm)}{
\ifthenelse{\equal{\x}{4} \AND \equal{\y}{2}}{
\draw[thick] (\x,\y) rectangle (0.4cm,0.4cm);
\draw(\x,\y)rectangle(0.4cm,0.4cm)}{\draw(\x,\y)circle(0.4cm)}};
}
\end{tikzpicture}
\end{document}
它们的输出应该是一组 4x8 的圆圈,但第二行的第 4 个圆圈和第三行的第 5 个圆圈应该被矩形代替。
我不明白为什么它们会像图片中那样。
最后,如何定义一个命令,根据另一个文档传递的参数在圆圈中写入数字。我教导我可以使用新命令 \circled,但我不明白如何使用。
请帮我!
答案1
我建议使用pic
s 来实现这一点,而不是使用ifthenelse
(来实现这一点)。
\documentclass[tikz,border=12pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[pics/.cd,
circ/.style={code={\draw (0,0) circle[radius=#1];}},
circ/.default=4mm,
rect/.style={code={\draw (-#1,-#1) rectangle (#1,#1);}},
rect/.default=4mm]
% \draw(4,-1) rectangle(8,4);
\path foreach \Y in {1,...,4}
{foreach [evaluate=\X as \Z using {int(10*\Y+\X)}] \X in {1,...,8} {
(\X,\Y)
\ifnum\Z=35
pic{rect}
\else
\ifnum\Z=24
pic{rect}
\else
pic{circ}
\fi
\fi }};
\end{tikzpicture}
\end{document}
\initbao
如果您想将列表中的数字添加到方案中,可以这样做。
\documentclass[tikz,border=12pt]{standalone}
\begin{document}
\begin{tikzpicture}[circ/.style={circle,draw,minimum size=8mm},
rect/.style={rectangle,draw,minimum size=8mm}]
\def\myarray{{22,0,0,0,0,0,0,0,0, 0,2,2,6,0,0,0,0,
0,0,0,0,6,2,2,0, 0,0,0,0,0,0,0,0,22}}
\path foreach \Y in {1,...,4}
{foreach [evaluate=\X as \Z using {int(10*\Y+\X)}] \X in {1,...,8} {
(\X,\Y)
\ifnum\Z=35
node[rect] {\pgfmathparse{\myarray[(\Y-1)*8+\X-1]}\pgfmathresult}
\else
\ifnum\Z=24
node[rect] {\pgfmathparse{\myarray[(\Y-1)*8+\X-1]}\pgfmathresult}
\else
node[circ] {\pgfmathparse{\myarray[(\Y-1)*8+\X-1]}\pgfmathresult}
\fi
\fi
}};
\end{tikzpicture}
\end{document}
或者其命令版本(顺序已改变)。
\documentclass[tikz,border=12pt]{standalone}
\newcommand{\initbao}[1]{\begin{tikzpicture}[circ/.style={circle,draw,minimum size=8mm},
rect/.style={rectangle,draw,minimum size=8mm}]
\path foreach \Y in {1,...,4}
{foreach [evaluate=\X as \Z using {int(10*\Y+\X)}] \X in {1,...,8} {
(\X,\Y)
\ifnum\Z=35
node[rect] {\pgfmathparse{{#1}[(4-\Y)*8+\X-1]}\pgfmathresult}
\else
\ifnum\Z=24
node[rect] {\pgfmathparse{{#1}[(4-\Y)*8+\X-1]}\pgfmathresult}
\else
node[circ] {\pgfmathparse{{#1}[(4-\Y)*8+\X-1]}\pgfmathresult}
\fi
\fi
}};
\end{tikzpicture}}
\begin{document}
\initbao{22,0,0,0,0,0,0,0,0, 0,2,2,6,0,0,0,0,
0,0,0,0,6,2,2,0, 0,0,0,0,0,0,0,0,22}
\end{document}
答案2
在使用循环和条件的地方,您可以将matrix of nodes
所有节点都设为圆形,并将其中一些节点的样式设为矩形。
\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\tikzstyle{r}=[rectangle] % <-- because I like tikzstyle ;)
\matrix[matrix of nodes, nodes in empty cells,
column sep={1cm,between origins},row sep={1cm,between origins},
nodes={circle,anchor=center,draw,minimum size=8mm}](M){
& & & & & & & \\
& & & & |[r]| & & & \\
& & & |[r,red]| ? & & & & \\
& & & & & & & ! \\
};
\end{tikzpicture}
\end{document}