20

How can I improve the code for this animation so that I can write more words? I wanted a smarter code.

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{0pt}%

\begin{document}
\foreach \t in {
    L,
    La,
    LaT,
    LaTe,
    LaTeX
    }{
    \begin{tikzpicture}[scale=1]
        \clip[fill=white] (0,-1) rectangle (6,1);
        \node[right] at (1,0) {\Huge \t};
    \end{tikzpicture}
}
\end{document}

enter image description here

Alan Munn
  • 218,180
Regis Santos
  • 14,463
  • Do I need a special version of tikz to run your MWE? I have 2.10 and am getting the error message "Package tikz Error: Extra options not allowed for clipping path command." – Stephan Lehmke Jul 14 '12 at 04:21
  • I receive the same error, but compilation goes fine by removing the option fill=white to the \clip. – Claudio Fiandrino Jul 14 '12 at 07:21

1 Answers1

14

My humble attempt with the xstring package:

\documentclass{article}

\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{0pt}

\usepackage{xstring}

\def\mytext{Hello world}
\StrLen{\mytext}[\mylen] 

\begin{document}

\foreach \t in {0,...,\mylen}{%
    \begin{tikzpicture}[scale=1]
        \clip[] (0,-1) rectangle (6,1);
        \node[right] at (1,0) {\Huge \StrLeft{\mytext}{\t}};
    \end{tikzpicture}
}%

\end{document}

We can also enclose the code into a new command \typewriter:

\newcommand{\typewriter}[1]{%
\StrLen{#1}[\mylen]
\foreach \t in {0,...,\mylen}{%
    \begin{tikzpicture}[scale=1]
        \clip[] (0,-1) rectangle (6,1);
        \node[right] at (1,0) {\Huge \StrLeft{#1}{\t}};
    \end{tikzpicture}
}%
}%

And then call it:

\typewriter{Hello world}

There we go. :)

Paulo Cereda
  • 44,220
  • 1
    I forgot to mention that I also ran into the error pointed out by Stephan and Claudio, so I also removed fill=white from my code. :) – Paulo Cereda Jul 14 '12 at 13:14