%% mpmulti.sty                                     09 Sep 2002
%%------------------------------------------------------------
%% History:
%%   Initial version                               14 Apr 2000
%%   Introduced keyword parameters                 12 Jun 2000
%%   Introduced new parameters (start,format), changed the
%%     global code to take several file counting strings (eg
%%     bla-*.mps, foo-*.jpg, etc.), added compatibility with
%%     \graphicspath (Jean-Christophe Dubacq)      06 Dec 2001
%%   Generalized the format option, added the end
%%   handling and documented extended features in here
%%                                                 09 Sep 2002
%%
%% Purpose:
%%     include multiple metapost pictures and overlay them,
%%     inserting a transition effect between them
%%
%% Synopsis:
%%
%% \multiinclude[pause=transitioncommand,
%%               graphics={option for includegraphics},
%%               format=filenameformat,
%%               start=number,
%%               end=number]{basefilename}
%%
%%     Include all metapost generated graphics found in
%%     files with the basename "filename", which
%%     have a number attached or in the extension.
%%     Insert the transitioncommand between the files (defaults
%%     to \pause).
%%     Further options allow to specify additional specifications
%%     for includegraphics, can change the number to start with
%%     (default 0) or the number to end with (default 1000000).
%%     By default the filenames are as created by mpost in the format
%%     "filename.n", where "n" represents the number.
%%     The option format with the values like "mps", "png" or "jpg" this
%%     can be changed to "filename-n.format", e.g. "example-0.mps"
%%     instead of "example.0".
%%     Complaints are only written, if the first file is not found.
%%     Otherwise including will silently stop.
%%
%% Requires:
%%     pause.sty for definitions of transition commands.
%%     The keyval package for options processing.
%%
%%
\RequirePackage{pause}
\RequirePackage{keyval}
%%
%% The command should process the optional arguments.
%%
\def\multiinclude{%
  \@ifnextchar [\@mpmulti{\@mpmulti[]}}
%%
\newif\if@mpm@groptions\@mpm@groptionsfalse
%% We describe the keys and their default values
\define@key{mpm}{format}[\@mpm@defaultformat]{%
  \global\def\@mpm@format##1{##1-\the\@mpm@count.#1}}
\define@key{mpm}{start}[0]{\global\def\@mpm@start{#1}\relax}
\define@key{mpm}{end}[1000000]{\global\def\@mpm@end{#1}\relax}
\define@key{mpm}{pause}[\pause]{\global\def\@mpm@pause{#1}\relax}
\define@key{mpm}{graphics}{\@mpm@groptionstrue
  \global\def\@mpm@graphics{\string#1}}
\def\@mpm@defaultformat#1{#1.\the\@mpm@count}
\let\@mpm@format=\@mpm@defaultformat
%%
%% We need a local counter
%%
\newcount\@mpm@count
%%
%% Implement the basic functionality.
%% Try to include the first file unconditionally.
%% This will produce an error message, if no such file can be found.
%% Afterwards we are going to check for more files and stop, when we
%% do not find another file. As long as we find files, these are
%% overlapped to the previous parts.
%%
\def\@mpmulti[#1]#2{%
  %% reset the options 
  \def\@mpm@pause{\pause}%
  \global\def\@mpm@graphics{}%
  \global\def\@mpm@start{0}%
  \global\def\@mpm@end{1000000}%
  %% get the arguments
  \setkeys{mpm}{#1}%
  %% Insert the first part of the figure
  %% and make sure we look also in other places according to path.
  \let\@mpm@oldinp@th\input@path\let\input@path\Ginput@path
  \@mpm@count=\@mpm@start
  \if@mpm@groptions
  \edef\@mpm@do@include{\noexpand\includegraphics[\@mpm@graphics]{\@mpm@format{#2}}}%
  \@mpm@do@include
  \else
  \includegraphics{\@mpm@format{#2}}%
  \fi
  \def\@mpmdoit{% Do it by conditional tail recursion.
  %% Select the next filename and advance counter
  \edef\@mpmfilename{\@mpm@format{#2}}%
  %% If the file exists,
  \IfFileExists{\@mpmfilename}{%
    %% insert the user defined transition (or the default),
    \@mpm@pause
    %% then process the next part and set up to try again.
    \llap{\if@mpm@groptions
      \edef\@mpm@do@include{\noexpand
        \includegraphics[\@mpm@graphics]{\@mpmfilename}}%
      \@mpm@do@include
      \else
      \includegraphics{\@mpmfilename}%
      \fi}%
    \ifnum\@mpm@count<\@mpm@end\relax    
      \advance\@mpm@count by 1\relax
      \let\@mpmnext\@mpmdoit
    \else
      \let\@mpmnext\relax
    \fi
  }{%
    %% If no more files exist, set up to stop.
    \let\@mpmnext\relax
  }%
  %% Whatever we had to do (without nesting all the IfFileExists)
  \@mpmnext
  }%
  %% Finally we must start it once, if there is more than one file
  %% supposed to be available.
  \ifnum\@mpm@count<\@mpm@end\relax
    \advance\@mpm@count by 1\relax   % start counting
    \@mpmdoit % and run the show
  \fi
  %% and reset the input path after all
  \let\input@path\@mpm@oldinp@th
}%