在以下工作示例中,您可以看到一个由 4 个正方形物体组成的正方形。我想知道如何将复合正方形定义为新命令,以便在循环中的下一次迭代中使用它?
\documentclass{article}
\usepackage{tikz}
\newcommand*{\wuerfel}[3][]{%
\begin{scope}[rotate=#1,shift={(#2,#3)}]
\draw[fill=red!20, very thin] (0,0) rectangle (.5,.5);%
\draw [step=.5, very thin] (0,0) grid (1,1);%
\end{scope}
}%
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,0}{
%\let\oldwuerfel\wuerfel
\wuerfel[0]{\i}{\i};
\wuerfel[90]{\i+1}{\i-1};
\wuerfel[0]{\i+1}{\i+1};
\wuerfel[-90]{\i-1}{\i+1};
%\renewcommand*{\wuerfel}[1][]{%
% \oldwuerfel[0]{#1}{#1};
% \oldwuerfel[90]{#1+1}{#1-1};
% \oldwuerfel[0]{#1+1}{#1+1};
% \oldwuerfel[-90]{#1-1}{#1+1};
% }%
%\wuerfel[\i]{}{};
}%
\end{tikzpicture}
\end{document}
答案1
这里建议使用盒子来保存物体。
\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\newcommand\Wuerfellist{}
\newsavebox{\Wuerfel}
\sbox{\Wuerfel}{%
\tikz{
\useasboundingbox(0,0)rectangle(1,1);
\fill[blue!20] (0,0) rectangle (.5,.5);
\draw [step=.5, very thin] (0,0) grid (1,1);
}}
\newcommand*\mehrereWuerfel[1]{{%
\def\Wuerfellist{}%
\foreach[count=\j from 0] \i in {1,...,#1}{\listxadd{\Wuerfellist}{\j}}%
\renewcommand*\do[1]{%
\sbox{\Wuerfel}{%
\tikz[nodes={inner sep=0pt,outer sep=0pt,anchor=south west}]{
\node{\usebox\Wuerfel};
\begin{scope}[shift={({2^##1},{2^##1})}]
\node[rotate=90]{\usebox\Wuerfel};
\node{\usebox\Wuerfel};
\node[rotate=-90]{\usebox\Wuerfel};
\end{scope}
}}}%
\dolistloop{\Wuerfellist}%
\usebox\Wuerfel%
}}
\begin{document}
\usebox\Wuerfel \hfill \mehrereWuerfel{1} \hfill \mehrereWuerfel{2}
\bigskip
\mehrereWuerfel{1}\hfill\mehrereWuerfel{3}
\bigskip
\scalebox{.25}{\mehrereWuerfel{6}}
\end{document}