0

In the following working example you can see a square consisting of 4 squared objects. I like to know how to define the compound square as a new command which I can use at next iteration inside the loop?

\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
  • Welcome to TeX.SX! Can you explain more what result you are trying to achieve. Mar 24, 2015 at 15:28

1 Answer 1

1

Here is a suggestion using a box to save the objects.

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

enter image description here enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.