5

In the animate package, using \multiframe, how to deal with zero padding? For example if I have 101 pictures from pic0000.jpg to pic0100.jpg and I want to do something like that:

\documentclass{article}
\usepackage{animate}
\usepackage[demo]{graphicx}
\begin{document}
\begin{animateinline}[controls]{6}
\multiframe{101}{i=0+1}{
  The pic \i
  \includegraphics[height=.45\textheight]{pic\i}
 }
\end{animateinline}
\end{document}

but with the variable I varying from 0000 to 0100.

3
  • animate has an internal macro for zero-padding you could use. See my answer below. – AlexG Dec 12 '12 at 13:19
  • Tricky but that look the more straightforward. – ucsky Dec 12 '12 at 16:32
  • It's versatile, as you can specify any number of digits as the template string without modifying/extending the macro. – AlexG Dec 12 '12 at 16:35
3

Internally, animate defines the macro \@anim@pad{<template>}{<number>} for zero-padding numbers according to a template which is a string of arbitrary digits of the desired width.

In the example below \i is formatted (zero-padded) to four digits width (0000...0100):

\documentclass{article}
\usepackage{animate}
\usepackage[demo]{graphicx}

\makeatletter
\let\zeropad\@anim@pad
\makeatother

\begin{document}
\begin{animateinline}[controls]{6}
\multiframe{101}{i=0+1}{%
  The pic \i
  \includegraphics[height=.45\textheight]{pic\zeropad{1234}{\i}}%
}
\end{animateinline}
\end{document}
1

Prepend the zeros as you go based on a condition:

\multiframe{101}{i=1+1}{%
  \includegraphics[height=50pt]{pic0\ifnum\number\i<100 0\fi\ifnum\number\i<10 0\fi\i}%
}

The first condition

\ifnum\number\i<100 0\fi

inserts a 0 if \i is less than 100. The second condition

\ifnum\number\i<10 0\fi

inserts a 0 if \i is less than 10. This establishes the correct formatting for your external files.

1

Here is one way to pad the numbers so that they are always four digits:

enter image description here


Code: Test \PadDigits Macro:

\documentclass{article}
\usepackage{pgffor}

\newcommand*{\PadDigits}[1]{%
    % #1 = number to pad
    \ifnum#1<10 
        000#1%
    \else
        \ifnum#1<100
            00#1%
        \else
        \ifnum#1<1000
            0#1%
        \else
            #1%
    \fi\fi\fi
}%
\begin{document}

\foreach \x in {0,9,99,999,1000} {%
    \par\noindent
    Padding \x = "\PadDigits{\x}"
}
\end{document}

Code: Use with \includegraphics:

\documentclass{article}
\usepackage{animate}
\usepackage[demo]{graphicx}

\newcommand*{\PadDigits}[1]{%
    % #1 = number to pad
    \ifnum#1<10 
        000#1%
    \else
        \ifnum#1<100
            00#1%
        \else
        \ifnum#1<1000
            0#1%
        \else
            #1%
    \fi\fi\fi
}%

\begin{document}
\begin{animateinline}[controls]{6}
\multiframe{101}{i=0+1}{%
  The pic \i
  \includegraphics[height=.45\textheight]{pic\PadDigits{\i}}%
 }
\end{animateinline}
2
  • Just did a test with the animate package and it's work well, do you want that I post the MWE as an answer? – ucsky Dec 12 '12 at 3:12
  • @aberration: Done. Note the addition of the trailing % -- just in case there was any shifting in the animation as you may have spurious spaces without those chars. – Peter Grill Dec 12 '12 at 3:24

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.