%%
%% This is file `mpgraphics.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% mpgraphics.dtx  (with options: `mpgraphics.sty')
%% 
%%   ______________________________________
%%   Copyright © 2011-2013  Persian TeX Group
%% 
%%   License information appended.
%% 
\NeedsTeXFormat{LaTeX2e}
\newcommand{\mpgraphics@date}{2013/04/04}
\newcommand{\mpgraphics@version}{v0.3}
\ProvidesPackage{mpgraphics}[\mpgraphics@date\space \mpgraphics@version\space
inline and display metapost figures in LaTeX]
\newcommand{\mpg@graphicsextension}{\ifPDFTeX\ifpdf pdf\else eps\fi\else\ifXeTeX pdf\fi\fi}
\newcommand{\mpg@graphicsformatname}{\ifPDFTeX\ifpdf PDF\else EPS\fi\else\ifXeTeX PDF\fi\fi}
\newcommand{\mpg@formatname}{\ifPDFTeX\ifpdf pdflatex\else latex\fi\else\ifXeTeX xelatex\fi\fi}
\RequirePackage{graphicx,color,moreverb,xkeyval,ifplatform,iftex,ifpdf}
\@ifundefined{verbatim@out}{\newwrite\verbatim@out}{}%
\newif\if@mpg@debug\@mpg@debugfalse
\expandafter\newif\csname if@mpg@no@\mpg@graphicsextension\endcsname
\newif\if@mpg@noprocess \@mpg@noprocessfalse
\newif\if@mpg@On
\DeclareOptionX{metapost}{%
  \def\mpg@metapost@opts{%
    -tex=latex
    -jobname="\mpgfigname"
    -interaction=batchmode
    #1}}
\DeclareOptionX{epstopdf}{\def\mpg@epstopdf@opts{#1}}
\define@choicekey{mpgraphics.sty}{compilation}[\@tempa\@tempb]{on,off}{%
  \ifcase\@tempb\relax
    \@mpg@Ontrue
  \or
    \@mpg@Onfalse
  \fi}
\define@choicekey{mpgraphics.sty}{debug}[\@tempa\@tempb]{on,off}{%
  \ifcase\@tempb\relax
    \@mpg@debugtrue
  \or
    \@mpg@debugfalse
  \fi}
\newcounter{mpg@runs}
\DeclareOptionX{runs}{%
  \setcounter{mpg@runs}{#1}% support calc
  \ifnum\c@mpg@runs > \z@
  \else
    \mpg@PackageWarning{The number of runs must be at least one.}%
    \c@mpg@runs\@ne
  \fi}
\newcommand{\mpgOff}{\@mpg@Onfalse}
\newcommand{\mpgOn}{\@mpg@Ontrue}
\ExecuteOptionsX{%
       metapost={},%
       epstopdf={},%
       compilation=on,%
       debug=off,%
       runs=1%
}
\ProcessOptionsX
\def\mpg@exe{\immediate\write18}
\def\OnlyIfFileExists#1#2{\IfFileExists{#1}{#2}{}}
\def\NotIfFileExists#1#2{\IfFileExists{#1}{}{#2}}
\def\mpg@convert#1#2#3{%
  \OnlyIfFileExists{#2}{%
    \mpg@exe{\csname mpg@cmd@#1\endcsname{#2}{#3}}%
    \NotIfFileExists{#3}{\mpg@Warning{Creation of #3 failed.}}}}
\def\mpg@cmd@metapost#1#2{mpost \mpg@metapost@opts\space
  "\unexpanded{\input} #1"}
\def\mpg@cmd@extrametapost#1#2{mpost \mpg@metapost@opts\space
  "\unexpanded{\input} #1"}
\def\mpg@cmd@epstopdf#1#2{epstopdf \mpg@epstopdf@opts\space --outfile "#2" "#1"}
\def\mpg@nl{^^J\space\space\space\space}
\newcommand\mpg@PackageError[2]{%
  \PackageError{mpgraphics}{\mpg@nl #1^^J}{#2}}
\newcommand\mpg@Warning[2]{\if@mpg@debug\PackageWarning{#1}{#2}\fi}
\newcommand\mpg@PackageWarning[1]{%
  \PackageWarning{mpgraphics}{\mpg@nl #1^^JThis warning occured}}
\newcommand\mpg@WarningNoLine[2]{\if@mpg@debug\PackageWarningNoLine{#1}{#2}\fi}
\ifshellescape
  \PackageWarningNoLine{mpgraphics}
   {****************************************\MessageBreak
    Using \csname write\endcsname18 capability \MessageBreak
    for producing \mpg@graphicsformatname-figures.  \MessageBreak
    ****************************************}
\else
  \mpg@PackageError{%
     "shell escape" (or "write18") is not enabled;\mpg@nl
     mpgraphics will not work!}
    {You need to run "\mpg@formatname\space -shell-escape"\mpg@nl
     on your LaTeX document Or turn compilation off}
\fi
\newenvironment{mpdefs}{\MPDEFSverbatimwrite{mpdefs.mp}}
   {\endMPDEFSverbatimwrite}
\newenvironment{ltxpreamble}{\verbatimwrite{ltxpreamble.ltx}}
   {\endverbatimwrite}
\newcommand{\mpgfigname}{\jobname-fig\thempgfig}
\def\MPGverbatimwrite#1{%
  \@bsphack
  \immediate\openout \verbatim@out #1
  \BeforeMPGStream%
  \let\do\@makeother\dospecials
  \catcode`\^^M\active \catcode`\^^I=12
  \def\verbatim@processline{%
    \immediate\write\verbatim@out
      {\the\verbatim@line}}%
  \verbatim@start}
\def\endMPGverbatimwrite{%
  \immediate\write\verbatim@out{endfig;}
  \immediate\write\verbatim@out{end;}
  \immediate\closeout\verbatim@out
  \@esphack}
\def\MPDEFSverbatimwrite#1{%
  \@bsphack
  \immediate\openout \verbatim@out #1
  \BeforeMPDEFSStream%
  \let\do\@makeother\dospecials
  \catcode`\^^M\active \catcode`\^^I=12
  \def\verbatim@processline{%
    \immediate\write\verbatim@out
      {\the\verbatim@line}}%
  \verbatim@start}
\def\endMPDEFSverbatimwrite{%
  \immediate\closeout\verbatim@out
  \@esphack}
\def\BeforeMPGStream
   {\message{Opening MPGStream=\mpgfigname.mp}%
    \ifPDFTeX\ifpdf%
    \immediate\write\verbatim@out{prologues:=3;}\else
    \immediate\write\verbatim@out{prologues:=2;}\fi\else\ifXeTeX
    \immediate\write\verbatim@out{prologues:=3;}\fi\fi
    \immediate\write\verbatim@out{outputtemplate:= "\@percentchar j.eps";}
    \immediate\write\verbatim@out{verbatimtex}
    \immediate\write\verbatim@out{\string\documentclass{article}}
    \IfFileExists{ltxpreamble.ltx}{\immediate\write\verbatim@out{\string\input{ltxpreamble.ltx}}}{}
    \immediate\write\verbatim@out{\string\begin{document}}
    \immediate\write\verbatim@out{etex;}
    \IfFileExists{mpdefs.mp}{\immediate\write\verbatim@out{input mpdefs;}}{}
    \immediate\write\verbatim@out{beginfig(\thempgfig);}
    }
\def\BeforeMPDEFSStream
   {\message{Opening MPGStream=mpdefs.mp}%
    \immediate\write\verbatim@out{verbatimtex}
    \immediate\write\verbatim@out{\string\documentclass{article}}
    \IfFileExists{ltxpreamble.ltx}{\immediate\write\verbatim@out{\string\input{ltxpreamble.ltx}}}{}
    \immediate\write\verbatim@out{\string\begin{document}}
    \immediate\write\verbatim@out{etex;}
    }
\newenvironment{mpdisplay}{\stepcounter{mpgfig}%
  \xdef\MPGCutFile{\mpgfigname.mp}
  \MPGverbatimwrite{\MPGCutFile}}
 {\endMPGverbatimwrite%
  \MPGgraphicsinclude%
   \global\@mpg@noprocessfalse\noindent}
\newenvironment{mpinline}{\stepcounter{mpgfig}%
  \xdef\MPGCutFile{\mpgfigname.mp}
  \MPGverbatimwrite{\MPGCutFile}}
 {\endMPGverbatimwrite%
  \MPGinlinegraphicsinclude%
   \global\@mpg@noprocessfalse\noindent}
\AtEndDocument{\endMPGhook%
      \csname if@mpg@no@\mpg@graphicsextension\endcsname
        \PackageWarningNoLine{mpgraphics}
        {******************************************\MessageBreak
         Some \mpg@graphicsformatname\space files of images were not found.\MessageBreak
         ******************************************}
       \fi
}
\gdef\endMPGhook{}
\newcommand{\mpgnoprocess}{\global\@mpg@noprocesstrue
  \PackageWarning{mpgraphics}
        {******************************************\MessageBreak
           Figure Number: \mpgfigname\space is not processed \MessageBreak
         ******************************************\MessageBreak}
}
\newcounter{mpgfig}
\newif\if@mpgGINwidth
\newif\if@mpgGINheight
\newif\if@mpgGINscale
\long\gdef\MPGgraphicsProcess{%
  \@ifundefined{Fig\thempgfig}
  {\mpg@WarningNoLine{mpgraphics}
    {******************************************\MessageBreak
     ************ Processing Fig: \thempgfig\space**********\MessageBreak
    ******************************************}
  }
  {\mpgnoprocess}
   \ifshellescape\if@mpg@noprocess\relax\else\if@mpg@On
    \IfFileExists{\mpgfigname.mp}{%
    \loop\ifnum\c@mpg@runs > \@ne
    \mpg@convert{extrametapost}{\mpgfigname.mp}{\mpgfigname.eps}%
          \advance\c@mpg@runs\m@ne
    \repeat
    \mpg@convert{metapost}{\mpgfigname.mp}{\mpgfigname.eps}%
  \mpg@WarningNoLine{mpgraphics}
    {******************************************\MessageBreak
     \mpgfigname.mp converted to \mpgfigname.eps\MessageBreak
     ******************************************}
    }{}
   \IfFileExists{\mpgfigname.eps}{%
   \ifPDFTeX\ifpdf%
   \mpg@convert{epstopdf}{\mpgfigname.eps}{\mpgfigname.pdf}%
   \mpg@WarningNoLine{mpgraphics}
    {******************************************\MessageBreak
     \mpgfigname.eps converted to \mpgfigname.pdf\MessageBreak
     ******************************************}\else\fi\else\ifXeTeX%
   \mpg@convert{epstopdf}{\mpgfigname.eps}{\mpgfigname.pdf}%
   \mpg@WarningNoLine{mpgraphics}
    {******************************************\MessageBreak
     \mpgfigname.eps converted to \mpgfigname.pdf\MessageBreak
     ******************************************}\fi\fi
}{%
     \mpg@PackageError{%
      metapost encountered error(s) while compiling your\mpg@nl
      metapost code and so it did not produce the figure!}
      {Fix your metapost code, delete all metapost and\mpg@nl
       LaTeX auxillary files and then run "\mpg@formatname\space -shell-escape"\mpg@nl
       on your document again}
}\else\fi
    \ifwindows%
    \IfFileExists{\mpgfigname.log}{\immediate\write18{del \mpgfigname.log}}{}%
    \IfFileExists{\mpgfigname.mp}{\immediate\write18{del \mpgfigname.mp}}{}%
    \ifPDFTeX\ifpdf%
    \IfFileExists{\mpgfigname.eps}{\immediate\write18{del \mpgfigname.eps}}{}\else\fi\else\ifXeTeX%
    \IfFileExists{\mpgfigname.eps}{\immediate\write18{del \mpgfigname.eps}}{}\fi\fi%
    \IfFileExists{\mpgfigname.mpx}{\immediate\write18{del \mpgfigname.mpx}}{}%
    \IfFileExists{texnum.mpx}{\immediate\write18{del texnum.mpx}}{}%
    \IfFileExists{makempx.log}{\immediate\write18{del makempx.log}}{}%
    \IfFileExists{mpxerr.log}{\immediate\write18{del mpxerr.log}}{}%
    \IfFileExists{mpxerr.tex}{\immediate\write18{del mpxerr.tex}}{}%
    \else
    \IfFileExists{\mpgfigname.log}{\immediate\write18{rm \mpgfigname.log}}{}%
    \IfFileExists{\mpgfigname.mp}{\immediate\write18{rm \mpgfigname.mp}}{}%
    \ifPDFTeX\ifpdf%
    \IfFileExists{\mpgfigname.eps}{\immediate\write18{rm \mpgfigname.eps}}{}\else\fi\else\ifXeTeX%
    \IfFileExists{\mpgfigname.eps}{\immediate\write18{rm \mpgfigname.eps}}{}\fi\fi%
    \IfFileExists{\mpgfigname.mpx}{\immediate\write18{rm \mpgfigname.mpx}}{}%
    \IfFileExists{texnum.mpx}{\immediate\write18{rm texnum.mpx}}{}%
    \IfFileExists{makempx.log}{\immediate\write18{rm makempx.log}}{}%
    \IfFileExists{mpxerr.log}{\immediate\write18{rm mpxerr.log}}{}%
    \IfFileExists{mpxerr.tex}{\immediate\write18{rm mpxerr.tex}}{}%
   \fi\fi
   \fi
}
\long\gdef\MPGgraphicsinclude{\MPGgraphicsProcess%
  \IfFileExists{\mpgfigname.\mpg@graphicsextension}%
     {\begin{center}
     \bgroup\fboxsep\@MPGboxsep\fboxrule\@MPGboxrule%
      \color{\@MPGgraphiccolor}%
      \fcolorbox{\@MPGgraphiclinecolor}{\@MPGgraphicbackground}%
     {\if@mpgGINwidth%
       \includegraphics[width=\@MPGgraphicwidth]{\mpgfigname.\mpg@graphicsextension}\else%
      \if@mpgGINheight%
       \includegraphics[height=\@MPGgraphicheight]{\mpgfigname.\mpg@graphicsextension}\else%
      \if@mpgGINscale%
       \includegraphics[scale=\@MPGgraphicscale]{\mpgfigname.\mpg@graphicsextension}\else%
       \includegraphics{\mpgfigname.\mpg@graphicsextension}\fi\fi\fi%
     }\egroup\end{center}%
      \global\@mpgGINwidthfalse\let\@MPGgraphicwidth\relax
      \global\@mpgGINheightfalse\let\@MPGgraphicheight\relax
      \global\@mpgGINscalefalse\let\@MPGgraphicscale\relax
      }{\csname @mpg@no@\mpg@graphicsextension true\endcsname}
   \gdef\@MPGgraphiclinecolor{white}
   \gdef\@MPGgraphicbackground{white}
   \gdef\@MPGboxsep{0pt}
   \gdef\@MPGboxrule{0pt}
}
\long\gdef\MPGinlinegraphicsinclude{%
  \MPGgraphicsProcess%
  \IfFileExists{\mpgfigname.\mpg@graphicsextension}%
     {\bgroup\fboxrule0pt%
       \normalcolor\fbox{\includegraphics{\mpgfigname.\mpg@graphicsextension}}%
       \egroup}%
      {\csname @mpg@no@\mpg@graphicsextension true\endcsname}%
}
\def\configure[#1][#2]{\setkeys{#1}{#2}
  \mpg@Warning{mpgraphics}{Reconfigured #1 parameter(s)\MessageBreak
   #2\MessageBreak}}
\define@key{mpggraphic}{width}     {\gdef\@MPGgraphicwidth{#1}%
                                    \global\@mpgGINwidthtrue}
\define@key{mpggraphic}{height}    {\gdef\@MPGgraphicheight{#1}%
                                    \global\@mpgGINheighttrue}
\define@key{mpggraphic}{scale}     {\gdef\@MPGgraphicscale{#1}%
                                    \global\@mpgGINscaletrue}
\define@key{mpggraphic}{color}     {\gdef\@MPGgraphiccolor{#1}}
\define@key{mpggraphic}{linecolor} {\gdef\@MPGgraphiclinecolor{#1}}
\define@key{mpggraphic}{background}{\gdef\@MPGgraphicbackground{#1}}
\define@key{mpggraphic}{linewidth} {\gdef\@MPGboxrule{#1}}
\define@key{mpggraphic}{rulesep}   {\gdef\@MPGboxsep{#1}}
\gdef\@MPGgraphiccolor{black}
\gdef\@MPGgraphiclinecolor{white}
\gdef\@MPGgraphicbackground{white}
\gdef\@MPGboxrule{0pt}
\gdef\@MPGboxsep{0pt}
\@ifundefined{c@arraylength}{\newcounter{arraylength}}{}%
\@ifundefined{c@ArrayIndex}{\newcounter{ArrayIndex}}{}%
\@ifundefined{c@zeroCtr}{\newcounter{zeroCtr}}{}%
\@ifundefined{c@recordCtr}{\newcounter{recordCtr}}{}
\setcounter{recordCtr}{1}
\@ifundefined{c@Ctr}{\newcounter{Ctr}}{}
\def\DeclareArray#1{\Array{#1}[0]{}}%
\def\Array#1[#2]#3{%
      \expandafter\gdef\csname #1#2\endcsname{#3}%
      \expandafter\gdef\csname #1\endcsname[##1]{\csname #1##1\endcsname}}%
\def\getArraylength#1{\setcounter{arraylength}{0}%
   \loop\expandafter\ifx\csname #1\thearraylength\endcsname\relax%
   \else\stepcounter{arraylength}\repeat}%
\def\addToArray#1#2{\setcounter{arraylength}{0}%
   \loop\expandafter\ifx\csname #1\thearraylength\endcsname\relax%
   \else\stepcounter{arraylength}\repeat%
   \Array{#1}[\thearraylength]{#2}}%
\def\clearArray#1{\getArraylength{#1}%
   \loop\ifnum\c@arraylength >0%
   \global\expandafter\let\csname #1\thearraylength\endcsname\relax%
   \addtocounter{arraylength}{-1}\repeat}%
\long\def\ArrayIterator#1#2{%
   \setcounter{ArrayIndex}{1}\getArraylength{#1}%
   \setcounter{zeroCtr}{\c@arraylength}%
   \loop\ifnum\c@ArrayIndex<\c@zeroCtr{#2}%
   \stepcounter{ArrayIndex}\repeat%
}%
\def\@nnil{\@nil}
\def\@empty{}
\def\@cvrstop#1\@@#2{}
\long\def\cvr@delimfor#1#2#3{\DeclareArray{#1}\clearArray{#1}%
   \long\def\@icvrloop##1#2##2\@@##3{\def##3{##1}\ifx ##3\@nnil%
   \expandafter\@cvrstop \else\addToArray{#1}{##1}%
    \relax\expandafter\@icvrloop\fi##2\@@##3}%
   \long\def\@cvrloop##1#2##2#2##3\@@##4{\addToArray{#1}{##1}%
   \def##4{##1}\ifx ##4\@nnil \else%
    \def##4{##2}\def\y@y{##2}\ifx\y@y\@nnil\else%
         \addToArray{#1}{##2}\fi\ifx ##4\@nnil \else%
      \@icvrloop ##3\@@##4\fi\fi}%
  \expandafter\def\expandafter\@fortmp\expandafter{#3}%
  \ifx\@fortmp\@empty \else%
    \expandafter\@cvrloop#3#2\@nil#2\@nil\@@\@ee@\fi}%
\@ifundefined{c@f@irstCtr}{\newcounter{f@irstCtr}}{}%
\@ifundefined{c@s@econdCtr}{\newcounter{s@econdCtr}}{}%
\long\gdef\NoProcess[#1]{%
   \long\def\@i@@noprocess##1,##2\@@##3{\def##3{##1}\ifx ##3\@nnil%
   \expandafter\@cvrstop \else
      \expandafter\hyphencheck##1-@-*[*]
    \relax\expandafter\@i@@noprocess\fi##2\@@##3}%
   \long\def\@@@noprocess##1,##2,##3\@@##4{
      \expandafter\hyphencheck##1-@-*[*]
   \def##4{##1}\ifx ##4\@nnil \else%
    \def##4{##2}\def\y@y{##2}\ifx\y@y\@nnil\else%
      \expandafter\hyphencheck##2-@-*[*]
         \fi\ifx ##4\@nnil \else%
      \@i@@noprocess ##3\@@##4\fi\fi}%
  \expandafter\def\expandafter\@fortmp\expandafter{#1}%
  \ifx\@fortmp\@empty \else%
    \expandafter\@@@noprocess#1,\@nil,\@nil\@@\@ee@\fi}%
\def\d@d#1[*]{}
\def\hyphencheck#1-#2-#3{\def\r@r{@}\def\s@s{*}\edef\c@c{#3}
   \ifx\c@c\r@r
   \setcounter{f@irstCtr}{#1}
   \setcounter{s@econdCtr}{#2}
   \stepcounter{s@econdCtr}
   \loop\ifnum\thes@econdCtr > \thef@irstCtr%
      \expandafter\edef\csname Fig\thef@irstCtr\endcsname{TRUE}
      \stepcounter{f@irstCtr}
   \repeat%
   \else\ifx\c@c\s@s%
      \expandafter\edef\csname Fig#1\endcsname{TRUE}
   \fi\fi\d@d}
%% 
%% Copyright © 2011-2013  Persian TeX Group <persian-tex@tug.org>
%% 
%% It may be distributed and/or modified under the LaTeX Project Public License,
%% version 1.3c or higher (your choice). The latest version of
%% this license is at: http://www.latex-project.org/lppl.txt
%% 
%% This work is “maintained” (as per LPPL maintenance status)
%% by Persian TeX Group.
%% 
%%
%% End of file `mpgraphics.sty'.
