[Tex/LaTex] Typewriter Animation Using the ‘Animate’ Package

animate

I wanted to do something similar like here:

Improving typewriter animation

But using the animate package. I managed to get a very poor solution:

\documentclass{article}
\usepackage[latin1]{inputenc}

\usepackage{animate}
\usepackage{amsmath}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{animateinline}

\begin{document}

\begin{center}
\fboxsep1mm
    \begin{animateinline}[autoplay,loop]{2}
    % one frame per second
    $\phantom{\text{Alles Gute zum Geburtstag!}}$
    \newframe   
    $\text{Alles}\phantom{\text{ Gute zum Geburtstag!}}$
    \newframe       
    $\text{Alles Gute}\phantom{\text{ zum Geburtstag!}}$
    \newframe       
    $\text{Alles Gute zum}\phantom{\text{ Geburtstag!}}$
    \newframe   
    $\text{Alles Gute zum Geburtstag}\phantom{\text{!}}$
    \newframe   
    $\text{Alles Gute zum Geburtstag!}\phantom{\text{}}$                    
    \end{animateinline}
\end{center}

\end{document}

The \phantom is in my experience needed to reserve in all frames the same space. Otherwise the text gets distorted.

Here is the result:

http://www.bipede.de/Downloads/HB.pdf

Has someone an idea how to get the very cool solution in

Improving typewriter animation

to work here?

By the way, Alles Gute zum Geburtstag means Happy Birthday in German.

Best Answer

Character for character

The usage of the xstring macros is taken from the linked question.

The width of the text (in \Huge style) is saved to \mytextwidth.
As \StrLen counts from 0 to, in this case, 27, \multiframe needs to animate 28 frames, which is why \mylen+1 is calculated and saved to \frames.

This example uses the \multiframe macro because we can use variables (the other question used PGF's \foreach for this) and don't need to type every frame individually.

The \vphantom is needed because we start with no text (\icounter=0) and we need vertical height for. If you want to start with A use icounter=1+1 and re-replace \frames with \mylen in the \multiframe command.

\makebox makes everytime a box of the full text's width so that animate doesn't stretch the text. (We could have used also \StrRight, but this needs less calculation.)

Code (char for char)

\documentclass{article}
\usepackage[latin1]{inputenc}

\usepackage{animate}
\usepackage{calc}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{animateinline}

\usepackage{xstring}

\def\mytext{Alles Gute zum Geburtstag!}
\StrLen{\mytext}[\mylen]
\newlength{\mytextwidth}
\setlength{\mytextwidth}{\widthof{\Huge\mytext}}
\def\frames{\number\numexpr\mylen+1\relax} % \frames = \mylen + 1

\begin{document}


\begin{center}
\begin{animateinline}[autoplay,loop]{2}
  \multiframe{\frames}{icounter=0+1}{
    \makebox[\mytextwidth][l]{\Huge\vphantom{\mytext}\StrLeft{\mytext}{\icounter}}
  }
\end{animateinline}
\end{center}
\end{document}

Looks a lot like … (a.k.a. Output)

enter image description here

Word for word

Code

\documentclass{article}
\usepackage[latin1]{inputenc}

\usepackage{animate}
\usepackage{calc}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{animateinline}

\usepackage{xstring}

\def\mytext{Alles Gute zum Geburtstag!}
\StrCount{\mytext}{ }[\countSpaces]
\newlength{\mytextwidth}
\setlength{\mytextwidth}{\widthof{\Huge\mytext}}
\def\frames{\number\numexpr\countSpaces+2\relax} % \frames = \countSpaces + 2

\begin{document}
\begin{center}
\begin{animateinline}[autoplay,loop]{2}
  \multiframe{\frames}{icounter=0+1,icounterr=1+1}{%
    \makebox[\mytextwidth][l]{%
      \Huge\vphantom{\mytext}%
      \ifnum\icounter=0\relax\else
        \ifnum\icounterr=\frames\relax\mytext\else%
          \StrBefore[\icounter]{\mytext}{ }%
        \fi
      \fi
    }%
  }
\end{animateinline}
\end{center}
\end{document}
Related Question