# 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. –  I am who I say I am 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

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}


## Update for animate 2013/04/23

Since 2013/04/23 (or before?) package animate supports quoting of file names (thanks mozartstraße for the hint):

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


Thus the patch is not needed. However, I think, quotes do not belong to the user interface, there are already curly braces to delimit the file name argument. Thus the following updated patch automatically adds quotes, if spaces are detected. If quotes are already given, then the file name argument is not changed.

\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
}%
}%
\@ifpackagelater{animate}{2013/04/23}{%
\def\anigrf@testspace#1 #2\@nil{%
\def\anigrf@tmp{#2}%
}%
\def\anigrf@testquote#1"#2\@nil{%
\def\anigrf@tmp{#2}%
}%
\newcommand{\anigrf@animategraphics}[3][]{%
% do nothing if the file name contains quotes
\edef\anigrf@file{#3}%
\@onelevel@sanitize\anigrf@file
\expandafter\anigrf@testquote\anigrf@file"\@nil
\ifx\anigrf@tmp\@empty % file name without quote(s)
% test for space in the file name
\@firstofone{\expandafter\anigrf@testspace\anigrf@file} \@nil
\ifx\anigrf@tmp\@empty % file name without space(s)
% do nothing, no spaces found
\expandafter\expandafter\expandafter\@firstoftwo
\else % file name with space(s)
\expandafter\expandafter\expandafter\@secondoftwo
\fi
\else % file name with quote(s)
\expandafter\@firstoftwo
\fi
{\anigrf@@animategraphics{#1}{#2}{#3}}%
{\anigrf@@animategraphics{#1}{#2}{"#3"}}%
}%
\newcommand{\anigrf@@animategraphics}[5]{%
\ifx\anigrf@opt\@empty
\org@animategraphics{#2}{#3}{#4}{#5}%
\else
\org@animategraphics[{#1}]{#2}{#3}{#4}{#5}%
\fi
\endgroup
}%
}{%
\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}

-
Please see my answer. –  I am who I say I am Jul 2 '13 at 11:59
@mozartstraße: Thanks, I have updated the answer. –  Heiko Oberdiek Jul 2 '13 at 15:05

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. –  I am who I say I am 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. –  I am who I say I am Oct 8 '12 at 12:34
The latest animate package can accept a path with spaces but it must be enclosed with quotes.
Did you try different engines (latex/dvips/ps2pdf, xelatex, pdflatex, ...)? –  AlexG Jul 2 '13 at 14:16
@AlexG: It only works with pdflatex. It fails with xelatex. I don't test it with latex-dvips-ps2pdf because I don't think \animategraphics can import dvi or ps. –  I am who I say I am Jul 2 '13 at 14:24
Of course, it can import Postscript in dvi mode, like \includegraphics. –  AlexG Jul 2 '13 at 14:26
@AlexG: It fails to compile with latex-dvips-ps2pdf. All of my tests did not use Oberdiek's solution. I just use the animate package. –  I am who I say I am Jul 2 '13 at 14:37