0

I want to write a single code for many polygons using foreach. How can I plot each one with the same code by using n variables and writing n values? I am repeating a few steps in the same way as below. But I want to write a single code for each polygon, usually foreach loop is used, but I would like to learn different loops. I'm working on one detail, 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} \end{document}

SmmTex
  • 33
  • 4
  • 1
    Why do you want to use different loops? What exactly do you mean? Usually \foreach is able to do way more than the standard user needs, what exactly are you missing from it? – Skillmon Dec 10 '21 at 11:25
  • Sounds like you want something recursive... I don't think it's a good idea, as TeX is not very fast that you can't exceed a few layers anyway. Just copy and paste manually. If you still insist, use LuaTeX and write Lua code that generates TeX code. – user202729 Dec 10 '21 at 11:30
  • I would love to use foreach, but I couldn't write the code I wanted. I wanted to say that I am open to different alternatives. It would be more convenient for me to write it with foreach. @Skillmon – SmmTex Dec 10 '21 at 11:32
  • Then what exactly do you want? You didn't describe it. – user202729 Dec 10 '21 at 11:33
  • yes I want something recursive. actually I'm looing for a common code for each polygon, which will only take a few steps. @user202729 – SmmTex Dec 10 '21 at 11:33
  • Not specific at all. Describe clearly by [edit]ing the question. (anyway, as I said above, that's not really a good idea as code in TeX is hard to read, just write out theed nested foreach loops by hand or use LuaTeX) – user202729 Dec 10 '21 at 11:35
  • 1
    Does these help? https://tex.stackexchange.com/q/597789/1952 or https://tex.stackexchange.com/q/222881/1952 – Ignasi Dec 10 '21 at 12:12
  • Do you mean parametrize this construction with respect the number of sides of the polygon? – vi pa Dec 10 '21 at 12:45
  • yes i want to do that @vi pa – SmmTex Dec 10 '21 at 13:26

1 Answers1

1

Parametrize this construction with respect to the number of side is easy with a \newcommand. This is my attempt.

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

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

\newcommand{\myPoly}[1]{\node[buffer=#1, fill=pink] (A) {}; \foreach \mycorner in {1,2,...,#1} {\node[buffer=#1, fill=green,scale=.33, transform shape] (A\mycorner) at (A.corner \mycorner) {};} }

\begin{document} \begin{figure} \centering \begin{tikzpicture} \node[buffer=4, fill=pink]{}; \end{tikzpicture} \caption{One square} \end{figure} \begin{figure} \centering \begin{tikzpicture} \myPoly{5} \end{tikzpicture}
\caption{One pentagon with pentagons} \end{figure} \end{document}

One pentagon with pentagons

vi pa
  • 3,394