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

2
  • 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." Commented Jul 14, 2012 at 4:21
  • I receive the same error, but compilation goes fine by removing the option fill=white to the \clip. Commented Jul 14, 2012 at 7:21

1 Answer 1

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. :)

1
  • 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. :) Commented Jul 14, 2012 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .