19

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

  • 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 4: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 7:21
13

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
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.