5
% 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?

  • 1
    Wouldn't renaming to heart_animation be a solution? – yo' Sep 28 '12 at 11:55
  • @tohecz: I want to create a bullet proof system that is more general for any possible cases. – kiss my armpit Sep 28 '12 at 11:56
  • 1
    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
  • What happens if you use package grffile and then a filename inside quotes: "foo bar" –  Sep 28 '12 at 12:19
  • 1
    @ガベージコレクタ 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
  • what happens with ...{heart\space animation}... –  Sep 28 '12 at 13:19
  • @Herbert: It does not works. :-) – kiss my armpit Sep 28 '12 at 15:46
  • You can't present this bounty to AleXG if he doesn't answer this question anyway and I don't think you can invite him for community work with rep points. He is quite active anyway. Just contact him :) – percusse Nov 16 '12 at 09:33
  • @percusse: It is enough for me if he gives a short answer saying "the requested feature has been implemented". :-) – kiss my armpit Nov 16 '12 at 09:38
  • I have tried to incorporate Heiko 's and egreg 's code suggestions. Unfortunately, while embedded spaces will work, other things get broken. So I gave up with it. Spaces in file names remain evil. – AlexG Nov 16 '12 at 11:11
  • @AlexG: What things get broken? – kiss my armpit Nov 16 '12 at 14:09
  • other engines (dvipdfmx, xetex), multipage input detection. – AlexG Nov 16 '12 at 14:15

3 Answers3

7

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}
Heiko Oberdiek
  • 271,626
4

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}
egreg
  • 1,121,712
1

The latest animate package can accept a path with spaces but it must be enclosed with quotes.

As a result, Heiko Oberdiek's solution no longer works (it produces compilation errors).

  • Did you try different engines (latex/dvips/ps2pdf, xelatex, pdflatex, ...)? – AlexG Jul 02 '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. – kiss my armpit Jul 02 '13 at 14:24
  • Of course, it can import Postscript in dvi mode, like \includegraphics. – AlexG Jul 02 '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. – kiss my armpit Jul 02 '13 at 14:37