3

I can't keep repeating the shape in the corners. I can't issue a command like corner of the corner. I want it to continue in a loop like fractal but I couldn't make the loop. With the command I wrote, the same shape is not drawn again on the small pink corners, can you help me?

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage{caption}
\tikzset{
    buffer/.style={
        draw,
        regular polygon,
        regular polygon sides=4,
        minimum size=20em
    },
    pics/nine/.style 2 args= {code={ 
            \node[buffer, fill=#1] (A) {};
            \foreach \mycorner in {1,2,3,4}
            \node[buffer, fill=#2, scale=.33, transform canvas] at (A.corner \mycorner) {};
    }},
}
\begin{document}
    \begin{figure}
        \centering
        \begin{tikzpicture}
            \node[buffer, fill=pink] (B) {};
            \foreach \mycorner in {1,2,3,4}
            \pic[scale=.33, transform shape] at (B.corner \mycorner) {nine={green}{pink}};
        \end{tikzpicture}
        \caption{Number 4}
    \end{figure}
\end{document}
SmmTex
  • 33
  • 4
  • 1
    Hi, Welcome to TeX-SE! Please consider to post a MWE, so that we can understand what you managed to do : https://tex.meta.stackexchange.com/questions/3343/what-makes-a-good-mwe – LukeTheWolf Nov 20 '21 at 08:34
  • I added my answer, but I would like to tell you can ping me only under an answer or a question of mine or if I commented before your ping. I saw your message only by chance, it didn't reach me. – CarLaTeX Nov 22 '21 at 21:00

2 Answers2

3

Here's a way to do recursive drawing in Metapost that uses the luamplib. You need to compile this with lualatex.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
input colorbrewer-rgb

def do(expr s, level) = fill s withcolor if odd level: Blues else: Reds fi [8][1+level]; draw s; if level > 0: for i=1 upto length s: do(s shifted - center s scaled 7/16 shifted point i of s, level - 1); endfor fi enddef;

beginfig(1); do(unitsquare scaled 200, 5); endfig; \end{mplibcode} \end{document}

You may want to adjust the scaling parameter to get different effects, and you can try it with other paths. But don't try it with more than about 6 or 7 levels, because it will take forever.

Thruston
  • 42,268
  • thank you, this is exactly what I wanted but I am getting an error while running the command. Is it possible for me to continue as I wrote? I hadn't heard of MetaPost, now I'll look into it. Why can't I run the tex file you wrote? Is there a separate environment or version difference for metapost? – SmmTex Nov 21 '21 at 11:13
  • You need to compile it using lualatex to enable the luamplib package. – Thruston Nov 21 '21 at 18:55
  • thank you, ı will try. @CarLaTeX can you please take a look at my question? I'm trying to continue with the answer you wrote. – SmmTex Nov 22 '21 at 06:48
  • I tried to activate it, but I still get too many errors, the figure does not appear, I don't know what to do. I searched a lot, but I did not understand how to use LualaTex. Is there a command sequence you can recommend as I wrote? @Thruston Sorry for asking so many questions, I'm trying to learn :) – SmmTex Nov 22 '21 at 07:22
  • This answer explains about the different TeX "engines" and how to use them. – Thruston Nov 22 '21 at 11:06
  • You say nothing about your own environment, so I do not know exactly how you should run my example. In my environment, I save the code part as smm.tex, then open a command line and run lualatex smm.tex which produces smm.pdf. – Thruston Nov 22 '21 at 11:09
  • I've added my full command and I'm wondering if I can get your shape by adding on top of that. – SmmTex Nov 22 '21 at 13:22
2

I'm sure there are better solutions, but this is very simple, with TikZ:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage{caption}

\tikzset{ buffer/.style={ draw, regular polygon, regular polygon sides=4, minimum size=20em }, }

\begin{document} \begin{figure} \centering \begin{tikzpicture} \node[buffer, fill=pink]{}; \end{tikzpicture} \caption{One square} \end{figure} \begin{figure} \centering \begin{tikzpicture} \node[buffer, fill=pink] (A) {}; \foreach \mycorner in {1,2,3,4} {\node[buffer, fill=green,scale=.33, transform shape] (A\mycorner) at (A.corner \mycorner) {};} \end{tikzpicture}
\caption{One square with squares} \end{figure} \begin{figure} \centering \begin{tikzpicture} \node[buffer, fill=pink] (A) {}; \foreach \mycorner in {1,2,3,4} {\node[buffer, fill=green,scale=.33, transform shape] (A\mycorner) at (A.corner \mycorner) {};} \foreach \mycornerone in {1,2,3,4} {\foreach \mycornertwo in {1,2,3,4} {\node[buffer, fill=pink,scale=.11, transform shape] at (A\mycornerone.corner \mycornertwo) {};}} \end{tikzpicture}
\caption{One square with squares with squares} \end{figure} \begin{figure} \centering \begin{tikzpicture} \node[buffer, fill=pink] (A) {}; \foreach \mycorner in {1,2,3,4} {\node[buffer, fill=green,scale=.33, transform shape] (A\mycorner) at (A.corner \mycorner) {};} \foreach \mycornerone in {1,2,3,4} {\foreach \mycornertwo in {1,2,3,4} {\node[buffer, fill=pink,scale=.11, transform shape] (A\mycornerone\mycornertwo) at (A\mycornerone.corner \mycornertwo) {};}} \foreach \mycornerone in {1,2,3,4} {\foreach \mycornertwo in {1,2,3,4} {\foreach \mycornerthree in {1,2,3,4} {\node[buffer, fill=green,scale=.0367, transform shape] at (A\mycornerone\mycornertwo.corner \mycornerthree) {};}}} \end{tikzpicture}
\caption{One square with squares with squares with squares} \end{figure} \end{document}

enter image description here

CarLaTeX
  • 62,716
  • I can't thank you enough :) I didn't know that the /mycorner command was used like this. Now I tried to continue this way and I succeeded. Thank you very much :) – SmmTex Nov 23 '21 at 08:46
  • can i ask one more thing ? i am continuing this in polygons but is this possible in a foreach loop ? For example, is it possible to define n and then draw steps for the given n values ? – SmmTex Dec 09 '21 at 09:33
  • @SmmTex Please ask a new question with this one linked. I think it's possible but maybe it is beyond my ability. With a new question someone else could answer. – CarLaTeX Dec 09 '21 at 12:27