# How to make \animategraphics happy with a file name with spaces?

% this filename is main.tex
% compile it with "pdflatex -shell-escape main" (without the quotes)

\documentclass{article}

\usepackage{filecontents}

% Create a PDF file that consist of some pages
\begin{filecontents*}{"heart animation.tex"}
\documentclass[border=12pt,pstricks]{standalone}
\usepackage{pst-plot}
\usepackage[nomessages]{fp}
\FPeval\Delta{round(2*pi/30:2)}

\def\x(#1){sin(#1)^3}
\def\y(#1){(13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t))/16}

\psset{algebraic,plotpoints=300}

\begin{document}
\multido{\n=0.00+\Delta}{31}{%
\begin{pspicture}[showgrid=false](-2,-2)(2,2)
\psparametricplot[origin={0,0.15},linecolor=red]{0}{\n}{\x(t)|\y(t)}
\end{pspicture}}

\end{document}
\end{filecontents*}

\immediate\write18{latex "heart animation.tex"}
\immediate\write18{dvips "heart animation.dvi"}
\immediate\write18{ps2pdf "heart animation.ps"}
% sometimes you need to disable auto rotate in ps2pdf. Please follow up if you really need it!
% delete auxiliary files generated by the 3 commands above.
\makeatletter
\@for\x:={tex,dvi,ps,log,aux,out,toc,nav,snm}\do{\immediate\write18{cmd /c if exist "heart animation.\x" del "heart animation.\x"}}
\makeatother

\usepackage{animate}
\begin{document}
%\animategraphics[controls,autoplay,loop,scale=<integer>]{<frame rate>}{<PDF filename without extension>}{<left blank>}{<left blank>}
\animategraphics[controls,autoplay,loop,scale=1]{10}{"heart animation"}{}{}
\end{document}


How to make \animategraphics happy with a file name with spaces?

-
Wouldn't renaming to heart_animation be a solution? – tohecz Sep 28 '12 at 11:55
@tohecz: I want to create a bullet proof system that is more general for any possible cases. – Click Me Sep 28 '12 at 11:56
Switching catcodes has all the problems of \verb you would not be able to use it inside another macro. If you do want to switch | to " just do something like \def\foo|#1|{"#1"} then \foo|abc def| expands to "abc def" – David Carlisle Sep 28 '12 at 12:10
Can you use simple quotes instead of double quotes in a .bat file? – egreg Sep 28 '12 at 12:15
@ガベージコレクタ In my experiment, animategraphics accepts spaces in a file name enclosed in double quotes, but instead of heart animation looks for heartanimation. – egreg Sep 28 '12 at 12:57
show 8 more comments

The problem is that \animategraphics uses LaTeX's \IfFileExists that uses the space as end marker of the file name. Then the result \@filef@und contains the file name including a final space. \animategraphics then removes the space via \zap@space that also kills spaces in between.

The following workaround redefines \animategraphics. It uses package grffile \grffile@IfFileExists instead of \IfFileExists, sets \@filef@und to the file name without the final space and neutralizes \zap@space.

\documentclass{article}

\usepackage{animate}
\usepackage{grffile}
\usepackage{letltxmacro}
\makeatletter
\@ifdefinable{org@animategraphics}{%
\LetLtxMacro\org@animategraphics\animategraphics
\renewcommand*{\animategraphics}{%
\begingroup
\@ifnextchar[{%
\def\anigrf@opt{[}%
\anigrf@animategraphics
}{%
\let\anigrf@opt\@empty
\anigrf@animategraphics
}%
}%
\newcommand{\anigrf@animategraphics}[5][]{%
\renewcommand{\IfFileExists}[3]{%
\grffile@IfFileExists{##1}{%
\let\@filef@und\grffile@file@found
\def\zap@space####1 \@empty{####1}%
##2%
}{##3}%
}%
\ifx\anigrf@opt\@empty
\org@animategraphics{#2}{#3}{#4}{#5}%
\else
\org@animategraphics[{#1}]{#2}{#3}{#4}{#5}%
\fi
\endgroup
}%
}
\makeatother

\begin{document}
\animategraphics[controls,autoplay,loop,scale=1]{10}{heart animation}{}{}%
\end{document}

-

For some reasons, animate doesn't like spaces in file names. In general they should be avoided whenever possible. What happens when you feed \animategraphics with the file name "heart animation", the package tries to includeheartanimation.pdf, removing spaces in the name.

A solution is to change the category code of the space before absorbing the second mandatory argument of \animategraphics

\documentclass{article}
\usepackage{animate}

\newcommand{\myanimategraphics}[2][]{%
\begingroup
\def\myagtempopt{\unexpanded{#1}}%
\def\myagtempmand{\unexpanded{#2}}%
\catcode\ =12 \myanimategraphicsaux
}
\def\myanimategraphicsaux#1#2#3{%
\begingroup\edef\x{\endgroup
\noexpand\animategraphics[\myagtempopt]{\myagtempmand}}%
\x{#1}{#2}{#3}\endgroup}

\begin{document}
\myanimategraphics{3}{"heart animation"}{}{}
\end{document}

-
 I will give you a bounty of 100 later. – Click Me Sep 28 '12 at 15:44 @ガベージコレクタ Don't worry: Heiko's answer deserves all credit. – egreg Sep 28 '12 at 15:57 The bounty of 100 has been awarded to your another answer. – Click Me Oct 8 '12 at 12:34