# Updating floating point number in animation

I'm trying to have an animated tikz graph with floating point values. While I can use floating point values as parameters for the graph, I can't seem to find a way to do so for displayed equations. Below is a MWE using beamer. The green line is animated properly - but the parameter does not appear in simplified form. The blue line shows the parameter appearing the way I would like it to (as a simplified number) but it is not updated in the tikz picture, even though it is updated outside that picture.

\documentclass[12pt]{beamer}

\usepackage{animate}
\usepackage{fp}
\usepackage{tikz}

\begin{document}

\newcounter{step}
\setcounter{step}{1}

\newcommand{\scalevalues}{ %
\FPmul{\scaled}{\thestep}{0.1}
\FPtrunc{\scaled}{\scaled}{1}
}

\scalevalues
\scaled % will print 0.1 above picture

\begin{animateinline}[loop, poster = first, controls, palindrome]{10}
\whiledo{\thestep < 21}{
\begin{tikzpicture}
%\useasboundingbox (-1,-1) rectangle (5,5);
%
\draw[->] (-1,0) -- (5,0) node[anchor=west]{\color{gray}x};
\draw[->] (0,-1) -- (0,5) node[anchor=south]{\color{gray}y};
%
\draw[-, thick, blue] (\scaled,-1) -- (5,4) node[anchor=east]{$\scaled x$};
\draw[-, thick, green] (0.1*\thestep,-1) -- (2,3) node[anchor=east]{$0.1*\thestep x$};
\end{tikzpicture}
\stepcounter{step}
\scalevalues
\scaled % will print updated values below picture
\ifthenelse{\thestep < 21}{
\newframe
}{
\end{animateinline}\relax
}
}

\end{document}


You have to carry out the calculation somehow but TikZ don't quite get that it's a computation it has to perform. So you can make it obvious by

{$\pgfmathparse{0.1*\thestep}\pgfmathprintnumber[fixed,precision=1]\pgfmathresult x$}


in that node.

• Wow, that was fast! Thanks... I have to wait a few minutes until I can accept it. – André Oct 6 '12 at 21:11
• @André No rush. Maybe someone else comes along with a better answer :) – percusse Oct 6 '12 at 21:11

The correct way of building a loop around a parameterized tikzpicture inside the animateinline environment is to use the \multiframe command. This simplifies the code and saves you from possible pitfalls:

\documentclass[12pt]{beamer}

\usepackage{animate}
\usepackage{tikz}

\begin{document}

\begin{animateinline}[loop, poster = first, controls, palindrome]{10}
\multiframe{21}{rNum=0.1+0.1}{
\begin{tikzpicture}
%\useasboundingbox (-1,-1) rectangle (5,5);
%
\draw[->] (-1,0) -- (5,0) node[anchor=west]{\color{gray}x};
\draw[->] (0,-1) -- (0,5) node[anchor=south]{\color{gray}y};
%
\draw[-, thick, blue] (\rNum,-1) -- (5,4) node[anchor=east]{$\pgfmathprintnumber[fixed,precision=1]{\rNum}x$};
\draw[-, thick, green] (\rNum,-1) -- (2,3) node[anchor=east]{$\pgfmathprintnumber[fixed,precision=1]{\rNum}x$};
\end{tikzpicture}
}
\end{animateinline}\relax

\end{document}

• Please. In your answer, you use ..loop.. together ...palindrome...When I load it without ...loop.., it works fine also. Why?, palindrome is prioritised !!. If you cared, please tell me about ..poster...Here you use poster = first ? Why? I have read animate's document but now my english is bad to understand it.... – user173875 Dec 21 '18 at 1:21
• It is not my code actually. I just improved it. Of course, palindrome and loop are mutually exclusive options. Take either the one or the other. – AlexG Dec 21 '18 at 6:03
• palindrome is prioritised compare with loop when the both are existed? – user173875 Dec 21 '18 at 6:22
• Maybe. To make it clear: It is an error to use both at the same time. – AlexG Dec 21 '18 at 6:33
• And about poster= first ? Can we use poster= last for this case? – user173875 Dec 21 '18 at 6:51