2

I used the following code (which did not work)

\begin{frame}[t]
\frametitle{not working}
\begin{center}
\vskip -.8cm
\begin{tikzpicture}
\draw[line width=.04cm,looseness=1]
  \foreach \i/\clr in {3/orange,1/orange,2/brown,4/red,0/purple} {
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2,rotate=\i*11.25]{spiro};
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2,rotate=-\i*11.25]{spiro};
}
\end{tikzpicture}
\end{center}
\end{frame}

trying to combine the Spirograph code from the answer to this question and \foreach loop from the answer to this question.

What is wrong in my trial code?

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\begin{document}
\tikzset{pics/spiro/.style={code={
\tikzset{spiro/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}} 
\draw[trig format=rad,pic actions]
 plot[variable=\t,domain=0:2*pi*\pv{nRotations}, samples=90*\pv{nRotations}+1, smooth cycle] 
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spiro/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1,nRotations/.initial=1}
\begin{frame}[t]
\frametitle{Spiropraph mine2}
\begin{center}
\vskip -.8cm
\begin{tikzpicture}[]
\draw%the angle of rotation is caclulated as 360/4(number of spikes in the drawing)/8(number of rotations wanted)*(order of rotation) 360/4/8=11.25
(0,0) pic[scale=0.75, orange, line width=0.6mm, rotate=360/4/8*3, fill=orange!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, orange, line width=0.6mm, rotate=-360/4/8*3, fill=orange!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, orange, line width=0.6mm, rotate=360/4/8*1, fill=orange!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, orange, line width=0.6mm, rotate=-360/4/8*1, fill=orange!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, brown, line width=0.6mm, rotate=360/4/8*2, fill=brown!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, brown, line width=0.6mm, rotate=-360/4/8*2, fill=brown!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, red, line width=0.6mm, rotate=360/4/8*4, fill=red!40]{spiro={R=6,r=-1.5,p=1.52}}
(0,0) pic[scale=0.75, purple, line width=0.6mm, rotate=360/4/8*0, fill=purple!40]{spiro={R=6,r=-1.5,p=1.52}}
;
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}[t]
\frametitle{Spirifankerln using scale Fractal solution simple}
\begin{tikzpicture}[pics/spiro/.style={code={
 \draw[line width=.04cm,looseness=1,pic actions]
 (0,-2) arc (180:90:2) arc (270:180:2) arc (360:270:2) arc (90:0:2);
}}]
  \foreach \i/\clr in {3/orange,1/orange,2/brown,4/red,0/purple} {
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2,rotate=\i*11.25]{spiro};
    \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2,rotate=-\i*11.25]{spiro};
  }
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

Hany
  • 4,709
  • 1
    Tikz commands must end with ;. You have \draw[line width=.04cm,looseness=1]. Instead, you should have \draw[line width=.04cm,looseness=1];. The error message in the log provided a helpful hint to me: ! Package tikz Error: Giving up on this path. Did you forget a semicolon?. – Cicada Jan 03 '20 at 12:51
  • 1
    Do you want something like \begin{frame}[t] \frametitle{now working} \begin{center} \vskip -.8cm \begin{tikzpicture}[line width=.04cm,looseness=1] \foreach \i/\clr in {3/orange,1/orange,2/brown,4/red,0/purple} { \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=1/2,rotate=\i*11.25]{spiro}; \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=1/2,rotate=-\i*11.25]{spiro}; } \end{tikzpicture} \end{center} \end{frame}? –  Jan 03 '20 at 14:56
  • @Schrödinger's cat Yes, thank you. – Hany Jan 04 '20 at 04:35

1 Answers1

0

As pointed out by @Cicada,

\draw[line width=.04cm,looseness=1]

is not a complete path. Yet, rather than making it an empty path, I thick you want to apply these options to the pics. There are (at least) two possibilities:

  1. Add the options to the options of the tikzpicture.
  2. Drop the backslashes in front of foreach and the pics, and remove the ; and add one at the end of the combined path.

Explicit code:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\tikzset{pics/spiro/.style={code={
\tikzset{spiro/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/spiro/##1}} 
\draw[trig format=rad,pic actions]
 plot[variable=\t,domain=0:2*pi*\pv{nRotations}, samples=90*\pv{nRotations}+1, smooth cycle] 
(
{(\pv{R}+\pv{r})*cos(\t)+\pv{p}*cos((\pv{R}+\pv{r})*\t/\pv{r})},
{(\pv{R}+\pv{r})*sin(\t)+\pv{p}*sin((\pv{R}+\pv{r})*\t/\pv{r})}
);
}},
spiro/.cd,R/.initial=6,r/.initial=-1.5,p/.initial=1,nRotations/.initial=1}
\begin{document}
\begin{frame}[t]
\frametitle{now working}
\begin{center}
\begin{tikzpicture}
\draw[line width=.04cm,looseness=1]
  foreach \i/\clr in {3/orange,1/orange,2/brown,4/red,0/purple} {
    pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2/3,rotate=\i*11.25]{spiro}
    pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2/3,rotate=-\i*11.25]{spiro}
};
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}[t] 
\frametitle{now working} 
\begin{center}
\begin{tikzpicture}[line width=.04cm,looseness=1]
 \foreach \i/\clr in {3/orange,1/orange,2/brown,4/red,0/purple} 
 { \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2/3,rotate=\i*11.25]{spiro}; 
 \pic[draw/.expanded=\clr!100,fill/.expanded=\clr!40,scale=2/3,rotate=-\i*11.25]{spiro}; } 
\end{tikzpicture} 
\end{center} 
\end{frame}
\end{document}

enter image description here

  • Thank you very much for your clarifications. I just have 2 questions, if I may bother you:

    what do 90* and +1 in: samples=90*\pv{nRotations}+1 mean

    and in your answer to this question https://tex.stackexchange.com/questions/522190/modifying-spirograph-code-to-get-more-control?noredirect=1&lq=1

    (in spiro2 code) what do the following parts mean

    \pgfmathparse{(int(1/\pv{dx}+1)}

    samples/.initial=21

    – Hany Jan 04 '20 at 05:50
  • 1
    @Hany samples is the number of points that TIkZ computes to draw the plot, so samples=90*\pv{nRotations}+1 just means it uses 91 samples fo \pv{nRotations}=1, 181 of \pv{nRotations}=2 and so on. Similarly, \pgfmathparse{(int(1/\pv{dx}+1)} \tikzset{spiro2/samples=\pgfmathresult} means it uses 21 plot points for \pv{dx}=1/20=0.05 and so on. –  Jan 04 '20 at 07:05
  • Thank you for your explanation. – Hany Jan 04 '20 at 09:17