# Tikz plot polar equation with foreach

Im trying to animate a polar equation using beamer, i couldnt find anything related to my problem here or on Google so my last chance is to request some help. I tried to take out the domain command out of the equation so the plot could read the x variable value (that ranges from 0 to 20). But well that dont work at all... after that i tried to make domain=\x but well that didnt worked either. Does anyone have any solution (or even an hint) to my problem?

Ps.: I need to use Tikz package.

\documentclass{beamer}

\usepackage{tikz}

\begin{document}

\foreach \x in {0,1,...,20}{
\begin{frame}
\begin{tikzpicture}
\draw[align=center,color=orange,smooth] plot (canvas polar cs:angle=\x r,radius= {\x});
\end{tikzpicture}
\end{frame}
}
\end{document}

-

You need to place the \foreach loop inside the frame and use overlay specifications for the \draw command (this will produce several slides inside a single frame, which then will give you the animation effect); something along these lines:

\documentclass{beamer}
\usepackage{tikz}

\tikzset{
invisible/.style={opacity=0,text opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}
},
}

\begin{document}

\begin{frame}
\centering
\begin{tikzpicture}[scale=3]
\foreach \x in {0,...,10}
{
\draw[
color=orange,
domain=0:\x,
samples=200,
visible on=<\number\numexpr\x+1\relax->
]
plot (canvas polar cs:angle=\x r,radius= {\x});
}
\end{tikzpicture}
\end{frame}

\end{document}


I used the visible on style (illustrated, for example, in this answer) for the overlay specification to prevent the elements from jumping around.

In the above solution, the whole spiral (the "old part" and the new one) is drawn in each slide, so the compilation time can be very long and the calculations will be increasingly heavy; to prevent this you can use

\documentclass{beamer}
\usepackage{tikz}

\tikzset{
invisible/.style={opacity=0,text opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}
},
}

\begin{document}

\begin{frame}
\centering
\begin{tikzpicture}[scale=3]
\xdef\lastx{0}
\foreach \x in {1,...,20}
{
\draw[
color=orange,
domain=\lastx:\x,
samples=200,
visible on=<\number\numexpr\x+1\relax->
]
plot (canvas polar cs:angle=\x r,radius= {\x});
\xdef\lastx{\x}
}
\end{tikzpicture}
\end{frame}

\end{document}


so in each new slide only the new piece is added. The result:

And here's a version of the animation produced using the animate package; only some pdf viewers (Acrobat Reader, tipically) will support the animation correctly:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{animate}
\usepackage{ifthen}
\hypersetup{pdfpagemode=FullScreen}

\newcounter{tmp}
\stepcounter{tmp}

\begin{document}

\begin{frame}[fragile]{}
\begin{center}
\begin{animateinline}[loop,poster=first,controls]{2}
\whiledo{\thetmp<21}{%
\begin{tikzpicture}
\begin{scope}[scale=2]
\foreach \x in {1,...,\thetmp}
{
\draw[
color=red,
domain=0:\thetmp,
samples=200,
]
plot (canvas polar cs:angle=\x r,radius= {\x});
}
\end{scope}
\path[use as bounding box] (-3,-3) rectangle (3,3);
\end{tikzpicture}%
\stepcounter{tmp}
\ifthenelse{\thetmp<21}
{\newframe}
{\end{animateinline}\relax}
}
\end{center}
\end{frame}

\end{document}

-
Thanks, Gonzalo! That worked like a charm. :) For what i read on the topic you linked that tikzset does that: "that avoids jumping effects is that makes the elements of the TikZ picture (nodes, paths or whatever) always present, but only visible in some moments. ", right? But for example here: texample.net/tikz/examples/animated-definite-integral the person didnt used the same logic... its because of the: \begin{frame}[fragile]{Animated Integral}? What i still dont understand 100% is why i need the tikzset... – Luke Apr 20 '14 at 15:43
@Luke You're welcome! Regarding your comment on visible on, you're right. The style makes the elements always present, but only visible on the specified slides. The example you linked doesn't use this because there the bounding box is always large enough to contain the elements drawn, so there's no risk that new elements will make the image "jump"; in your concrete example, without something like visible on, the bounding box would change from slide to slide causing undesired "jumps". \tikzset is where the styles (in particular, visible on) are defined. – Gonzalo Medina Apr 20 '14 at 16:39
@Luke Also, don't forget that you can accept answers that you consider solved your problem by clicking the checkmark to their left. In case of doubt, please see How do you accept an answer?. – Gonzalo Medina Apr 20 '14 at 16:40
Yeah that was a perfect answer, i was just waiting for this to mark as done. :) Thanks again for your time and your help. – Luke Apr 20 '14 at 17:55
@Luke We would need to see the problematic code (though using non-interger values in the increment might be problematic). I think the best thing to do here would be to open a fresh new question (perhaps indicating there that it's a follow-up and providing a link to this one) in which you include a minimal version of the doucment illustrating the problem. – Gonzalo Medina Apr 21 '14 at 3:18