# How to deal with zero padding using multiframe in the animate package?

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.

-
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. –  aberration 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

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

\begin{document}
\begin{animateinline}[controls]{6}
\multiframe{101}{i=0+1}{%
The pic \i
}
\end{animateinline}
\end{document}

-

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.

-

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

## Code: Test \PadDigits Macro:

\documentclass{article}
\usepackage{pgffor}

% #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
}
\end{document}


## Code: Use with \includegraphics:

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

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

@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