%% mpmulti.sty                                     12 Jun 2000
%%------------------------------------------------------------
%% History:
%%   Initial version                               14 Apr 2000
%%   Introduced keyword parameters                 12 Jun 2000
%%
%% Purpose:
%%     include multiple metapost pictures and overlay them,
%%     inserting a transition effect between them
%%
%% Synopsis:
%%
%% \multiinclude[pause=transitioncommand,
%%               graphics={option for includegraphics}]{basefilename}
%%
%%     Include all metapost generated graphics found in
%%     files "filename.0", "filename.1" etc. using the specified
%%     options (if any).
%%     Insert the transitioncommand between the files (defaults
%%     to \pause).
%%
%% 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@mpgroptions\@mpgroptionsfalse
%% We describe the keys
\define@key{mpm}{pause}[\pause]{\global\def\mpm@pause{#1}\relax}
\define@key{mpm}{graphics}{\@mpgroptionstrue
  \global\def\mpm@graphics{\string#1}}
%%
%% 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{}
  %% get the arguments
  \setkeys{mpm}{#1}
  %% Insert the first part of the figure.
  \if@mpgroptions
  \edef\@mpm@do@include{\noexpand\includegraphics[\mpm@graphics]{#2.0}}% 
  \@mpm@do@include
  \else
  \includegraphics{#2.0}% 
  \fi
  \@mpm@count=1\relax   % start counting
  \def\@mpmdoit{% Do it by conditional tail recursion.
  %% Select the next filename and advance counter
  \edef\@mpmfilename{#2.\the\@mpm@count}%
  \advance\@mpm@count by 1\relax
  %% 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@mpgroptions
      \edef\@mpm@do@include{\noexpand
        \includegraphics[\mpm@graphics]{\@mpmfilename}}%
      \@mpm@do@include
      \else
      \includegraphics{\@mpmfilename}%
      \fi}%
    \let\@mpmnext\@mpmdoit}{%
    %% 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.
  \@mpmdoit
  }%
