% \iffalse 
%<*internal>
\iffalse
%</internal>
%<*readme>
_________________
The mpgraphics package
v0.1

mpgraphics allows users to typeset metapost inline and display
figures in their LaTeX documents with only and only one run of 
LaTeX, or PDFLaTeX, or XelaTeX (no separate runs of mpost). 
mpgraphics achieves this by using shell scape or write 18 
feature so the whole process is automatic and the 
end user do not need to carry out the long process.

______________
Vafa Khalighi
vafakhlgh@gmail.com

Copyright © 2011
Distributed under the LaTeX Project Public License
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 “author-maintained” (as per LPPL maintenance status) 
by Vafa Khalighi.
%</readme>
%<*internal>
\fi
\begingroup
%</internal>
%<*batchfile>
\input docstrip.tex
\keepsilent
\preamble

  ______________________________________
  Copyright © 2011  Vafa Khalighi

  License information appended.


\endpreamble
\postamble

Copyright © 2011  Vafa Khalighi <vafakhlgh@gmail.com>

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 “author-maintained” (as per LPPL maintenance status) 
by Vafa Khalighi.



\endpostamble
\askforoverwritefalse
\generate{\file{mpgraphics.sty}{\from{\jobname.dtx}{mpgraphics.sty}}}
%</batchfile>
%<batchfile>\endbatchfile
%<*internal>
\generate{\file{\jobname.ins}{\from{\jobname.dtx}{batchfile}}}
\nopreamble\nopostamble
\generate{\file{README.txt}{\from{\jobname.dtx}{readme}}}
\generate{\file{mpgraphics-example.ltx}{\from{\jobname.dtx}{mpgraphics-example.ltx}}}
\endgroup
\immediate\write18{mv README.txt README}
\immediate\write18{makeindex -s gind.ist -o \jobname.ind  \jobname.idx}
\immediate\write18{makeindex -s gglo.ist -o \jobname.gls  \jobname.glo}
%</internal>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{url,wrapfig,graphicx}
\usepackage{fontspec}
\usepackage{calc}
\usepackage{pifont}
\usepackage{bbding}
\usepackage{bidicode}
\usepackage[numbered]{hypdoc}
\definecolor{bidiblue}{rgb}{0.1,0.2,0.8}
\definecolor{myred}{rgb}{0.65,0.04,0.07}
\hypersetup{linkcolor=bidiblue,urlcolor=bidiblue,citecolor=bidiblue}
\usepackage{bidi}
\setlength\columnseprule{.4pt}
\setmainfont[Mapping=tex-text,Numbers=OldStyle]{Minion Pro}
\setsansfont[Mapping=tex-text,Numbers=OldStyle]{Myriad Pro}
\newfontfamily\ParsiFont[Script=Arabic]{FreeFarsi}
\newcommand\cmdtype[1]{\textcolor{myred}{\texttt{#1}}}
\def\parsitext#1{\RLE{\ParsiFont#1}}
\def\XePersian{XePersian}
\def\mpg{\textsf{mpgraphics} package}
\def\mpost{\textsf{MetaPost}}
\lstset{backgroundcolor=\color{hellgelb},numbers=left,frame=single,escapeinside=~~,morekeywords={includegraphics,
textFR}
}
\makeatletter
\@addtoreset{CodelineNo}{subsection}
\pdfstringdefDisableCommands{%
\renewcommand\Lcs[1]{\textbackslash#1}
}
\renewcommand\tableofcontents{\relax
  \begin{multicols}{2}[\section*{\contentsname}]\small
    \@starttoc{toc}\relax
  \end{multicols}}
\makeatother
\newcounter{local}
\renewcommand\theenumi{\protect\setcounter{local}%
  {201+\the\value{enumi}}\protect\ding{\value{local}}}
\renewcommand\labelenumi{\theenumi}
\renewcommand\labelitemi{\HandRight}
\renewcommand\labelitemii{\HandRightUp}
\renewcommand\labelitemiii{\HandCuffRight}
\renewcommand\labelitemiv{\HandPencilLeft}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%\OnlyDescription
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintIndex
  \PrintChanges
\end{document}
%</driver>
%
%
% \fi
%
% \GetFileInfo{\jobname.sty}
% \CheckSum{0}
% \makeatletter
%\title{The \textsf{mpgraphics} Package\\ \href{http://bitbucket.org/vafa/mpgraphics}{\texttt{http://bitbucket.org/vafa/mpgraphics}}}
%\author{Vafa Khalighi\\ \href{mailto:vafakhlgh@gmail.com}{\texttt{vafakhlgh@gmail.com}}}
%\date{Version 0.1\qquad\today}
%\maketitle
%\begin{abstract}
%It is not possible to include \mpost{} graphics directly into your \LaTeX{} documents; you have got to process your \mpost{} code first with \mpost{} and then include the images in your \LaTeX{} document. This is difficult and time consuming and the situation even becomes worser when you have many \mpost{} graphics.
%
%\medskip
%The \mpg{} allows you to have all your \mpost{} codes directly into your \LaTeX{} document and if you run \LaTeX{}, or PDF\LaTeX{}, or \XeLaTeX{} with the \cmdtype{-shell-escape} option only once, then you can see your \mpost{} graphics in your \LaTeX{} output.
%
%\medskip
%This package works with PDF\LaTeX{} (both PDF and DVI modes) and \XeLaTeX{} formats.
%\end{abstract}
%\tableofcontents
%\section{Dedication}
%We dedicate the \mpg{} to the \textbf{Iranian mathematician}, \textbf{Jamshid Kashani}. Al-Kashi was one of the best mathematicians in the Islamic world. He was born in 1380, in Kashan, in central Iran. This region was controlled by Tamurlane, better known as Timur, who was more interested in invading other areas than taking care of what he had. Due to this, al-Kashi lived in poverty during his childhood and the beginning years of his adulthood.
%
%\begin{wrapfigure}{r}{0.6\textwidth}
%\centering
%\fbox{\includegraphics{jamshid_kashani}}
% \caption{Ghyath al-Din Jamshid Kashani}
%\end{wrapfigure}
%
%\medskip
%The situation changed for the better when Timur died in 1405, and his son, Shah Rokh, ascended into power. Shah Rokh and his wife, Goharshad, a Persian princess, were very interested in the sciences, and they encouraged their court to study the various fields in great depth. Their son, Ulugh Beg, was enthusiastic about science as well, and made some noted contributions in mathematics and astronomy himself. Consequently, the period of their power became one of many scholarly accomplishments. This was the perfect environment for al-Kashi to begin his career as one of the world’s greatest mathematicians.
%
%\medskip
%Eight years after he came into power in 1409, Ulugh Beg founded an institute in Samarkand which soon became a prominent university. Students from all over the Middle East, and beyond, flocked to this academy in the capital city of Ulugh Beg’s empire. Consequently, Ulugh Beg harvested many great mathematicians and scientists of the Muslim world. In 1414, al-Kashi took this opportunity to contribute vast amounts of knowledge to his people. His best work was done in the court of Ulugh Beg, and it is said that he was the king’s favourite student.
%
%\medskip
%Al-Kashi was still working on his book, called “Risala al-watar wa’l-jaib” meaning “The Treatise on the Chord and Sine”, when he died in 1429. Some scholars believe that Ulugh Beg may have ordered his murder, while others say he died a natural death. The details are unclear. 
%\section{Requirements}
%\begin{itemize}
%\item A \texttt{web2c} \TeX{} implementation such as \TeX Live or MiK\TeX{} is needed; the package does not work with commercial \TeX{} implementations such as PC\TeX.
%\item \LaTeX{}, or PDF\LaTeX{}, or \XeLaTeX{} (depending on which one you use) must be called with the \cmdtype{-shell-escape} option.
%\item A recent and working version of \textsf{epstopdf} program (available in \TeX Live and MiK\TeX) is needed.
%\item A recent version of \textsf{color}, \textsf{graphicx}, \textsf{ifpdf}, \textsf{ifplatform}, \textsf{iftex}, \textsf{moreverb}, and \textsf{xkeyval} packages are required.
%\end{itemize}
%\section{Basic Usage}
%\subsection{Placing \mpost{} graphics at the center}
%\begin{BDef}
%\LBEG{mpdisplay}\\
%\Larga{\mpost{} codes between \texttt{beginfig();} and \texttt{endfig;}}\\
%\LEND{mpdisplay}
%\end{BDef}
%\begin{itemize}
%\item \texttt{mpdisplay} environment places \mpost{} graphics in  display mode (at the center).
%\end{itemize}
%If your \mpost{}  code is something like this:
%
%\begin{lstlisting}[language=MetaPost,caption={\mpost{} code}]
%beginfig(1);
%u=1cm;
%draw (2u,2u)--(0,0)--(0,3u)--(3u,0)--(0,0);
%pickup pencircle scaled 4pt;
%for i=0 upto 2:
%for j=0 upto 2: drawdot (i*u,j*u); endfor
%endfor
%endfig;
%end;
%\end{lstlisting}
%
%Then the equivalent \LaTeX{} document should be like this:
%
%\begin{lstlisting}
%\documentclass{article}
%\usepackage{mpgraphics}
%\begin{document}
%\begin{mpdisplay}
%u=1cm;
%draw (2u,2u)--(0,0)--(0,3u)--(3u,0)--(0,0);
%pickup pencircle scaled 4pt;
%for i=0 upto 2:
%for j=0 upto 2: drawdot (i*u,j*u); endfor
%endfor
%\end{mpdisplay}
%\end{document}
%\end{lstlisting}
%
%\subsection{Placing \mpost{} graphics in inline mode}
%\begin{BDef}
%\LBEG{mpinline}\\
%\Larga{\mpost{} codes between \texttt{beginfig();} and \texttt{endfig;}}\\
%\LEND{mpinline}
%\end{BDef}
%\begin{itemize}
%\item \texttt{mpinline} environment places \mpost{} graphics in inline mode (just like inline maths).
%\end{itemize}
%If your \mpost{}  code is something like this:
%
%\begin{lstlisting}[language=MetaPost,caption={\mpost{} code}]
%beginfig(2);
%draw (20,20)--(0,0)--(0,30)--(30,0)--(0,0);
%endfig;
%end;
%\end{lstlisting}
%Then the equivalent \LaTeX{} document should be like this:
%
%\begin{lstlisting}
%\documentclass{article}
%\usepackage{mpgraphics}
%\begin{document}
%\begin{mpinline}
%draw (20,20)--(0,0)--(0,30)--(30,0)--(0,0)
%\end{mpinline}
%\end{document}
%\end{lstlisting}
%\section{Your Global \mpost{} definitions and inputs}
%\begin{BDef}
%\LBEG{mpdefs}\\
%\Larga{your global \mpost{} definitions and inputs}\\
%\LEND{mpdefs}
%\end{BDef}
%\begin{itemize}
%\item You can put your global \mpost{} definitions and inputs inside \texttt{mpdefs} environment.
%\end{itemize}
%If your \mpost{}  code is something like this:
%
%\begin{lstlisting}[language=MetaPost,caption={\mpost{} code}]
%input metaobj
%defaultfont:="ptmr8r";
%warningcheck:=0;
%beginfig(1)
%newDEllipse.a(btex some text etex);
%scaleObj(a,1.7);
%rotateObj(a,45);
%a.c=origin;
%drawObj(a);
%endfig;
%end;
%\end{lstlisting}
%Then the equivalent \LaTeX{} document should be like this:
%
%\begin{lstlisting}
%\documentclass{article}
%\usepackage{mpgraphics}
%\begin{document}
%\begin{mpdefs}
%input metaobj
%defaultfont:="ptmr8r";
%warningcheck:=0;
%\end{mpdefs}
%\begin{mpdisplay}
%newDEllipse.a(btex some text etex);
%scaleObj(a,1.7);
%rotateObj(a,45);
%a.c=origin;
%drawObj(a);
%\end{mpdisplay}
%\end{document}
%\end{lstlisting}
%\section{Your \LaTeX{} packages and macros used within \mpost{} graphics}
%\begin{BDef}
%\LBEG{ltxpreamble}\\
%\Larga{Your \LaTeX{} packages and macros used within \mpost{} graphics}\\
%\LEND{ltxpreamble}
%\end{BDef}
%\begin{itemize}
%\item You can put your \LaTeX{} packages and macros that needs to be used within \mpost{} graphics, in \texttt{ltxpreamble} environment.
%\end{itemize}
%If you want to use the previous \mpost{} code but you want that your labels to be typeset with fonts from \textsf{fourier} package, then your \LaTeX{} document should look like this:
%
%\begin{lstlisting}
%\documentclass{article}
%\usepackage{mpgraphics}
%\begin{document}
%\begin{mpdefs}
%input metaobj
%defaultfont:="ptmr8r";
%warningcheck:=0;
%\end{mpdefs}
%\begin{ltxpreamble}
%\usepackage{fourier}
%\end{ltxpreamble}
%\begin{mpdisplay}
%newDEllipse.a(btex some text etex);
%scaleObj(a,1.7);
%rotateObj(a,45);
%a.c=origin;
%drawObj(a);
%\end{mpdisplay}
%\end{document}
%\end{lstlisting}
%\section{Options of the package and few related commands}
%There are four options:
%\begin{description}
%\item[\OptArg*{epstopdf=\Largb{\Larga{options}}}:] Options can be appended to the \textsf{epstopdf} program if you wish\footnote{See options of \textsf{epstopdf} program.}. For example, to run \textsf{epstopdf} auxillary compilation with debugging information written to the console, use the following package option:
%\begin{BDef*}
%\Lcs{usepackage}\OptArg{epstopdf=\Largb{-debug}}\Largb{mpgraphics}
%\end{BDef*}
%By default, no option is appended to the \textsf{epstopdf} program.
%\item[\OptArg*{compilation=\Larga{on/off}}:] After your \mpost{} figures have been generated and you no longer wish to re-generate your \mpost{} figures, the package can be given the \OptArg*{compilation=off} option to save compilation time:
%\begin{BDef*}
%\Lcs{usepackage}\OptArg{compilation=off}\Largb{mpgraphics}
%\end{BDef*}
%By default, \OptArg*{compilation=on}. Also the following two commands are provided:
%\begin{BDef}
%\Lcs{mpgOff}\qquad\Lcs{mpgOn}
%\end{BDef}
%\begin{itemize}
%\item \Lcs{mpgOff} turns off compilation.
%\item \Lcs{mpgOn} turn on compilation.
%\end{itemize}
%A command \Lcs{NoProcess} is also available to facilitate suppressing of pdf/eps generation of those
%figures whose pdf/eps’s are already available. This might prove helpful when you have more figures
%to process and many of them are perfected and don’t need recompilation and translation everytime
%you run \LaTeX. The usage is:
%\begin{BDef}
%\Lcs{NoProcess}\Largs{\Larga{comma separated and/or hyphen separated ranges}}
%\end{BDef}
%For example, If you have ten figures and if you want to suppress the processing of the figure numbers 1, 2, 4 to
%8 you can issue the command at the top of the document as:
%\begin{BDef*}
%\Lcs{NoProcess}\Largs{1,2,4-8}
%\end{BDef*}
%\item[\OptArg*{metapost=\Largb{\Larga{options}}}:] The \mpost{} auxiliary compilation has some hard-coded options (\texttt{-tex=latex}, \texttt{-jobname="\Lcs{mpgfigname}"}, and \texttt{-interaction=batchmode}), and further options can be appended if you wish\footnote{See page 89 of \mpost{} manual.}. For example, to run \mpost{} auxillary compilation with more information written to the console, use the following package option:
%\begin{BDef*}
%\Lcs{usepackage}\OptArg{metapost=\Largb{-interaction=nonstopmode}}\Largb{mpgraphics}
%\end{BDef*}
%\item[\OptArg*{runs=\Larga{positive integer}}:] Sometimes with some \mpost{} macros such as \textsf{makecirc}, you find that you need to run \mpost{} more than once. The exact number will vary by exact application, and must
%be set explicitly. For example if you want to run \mpost{} twice, then You can have:
%\begin{BDef*}
%\Lcs{usepackage}\OptArg{runs=2}\Largb{mpgraphics}
%\end{BDef*}
% By default \mpost{} runs only once so we have \OptArg*{runs=1} by default.
%\end{description}
%\section{Controlling different aspects of \mpost{} graphics}
%There are few hooks to control different aspects of \mpost{} graphics:
%\begin{BDef}
%\Lcs{configure}\Largs{mpggraphic}\Largs{width=2in}\\
%\Lcs{configure}\Largs{mpggraphic}\Largs{height=3in}\\
%\Lcs{configure}\Largs{mpggraphic}\Largs{width=2in,height=3in}\\
%\Lcs{configure}\Largs{mpggraphic}\Largs{scale=.5}\\
%\Lcs{configure}\Largs{mpggraphic}\Largs{linecolor=red}\\
%\Lcs{configure}\Largs{mpggraphic}\Largs{linewidth=1pt}\\
%\Lcs{configure}\Largs{mpggraphic}\Largs{background=green}\\
%\Lcs{configure}\Largs{mpggraphic}\Largs{rulesep=6pt}
%\end{BDef}
%The functionality is same as the \texttt{width}, \texttt{height} and \texttt{scale} options as in the \Lcs{includegraphics} command.
%But the graphic will be restricted to aspect ratio. \texttt{linecolor}, \texttt{linewidth}, \texttt{rulesep} and \texttt{background} corresponds to the box surrounding the graphic. The default values are \texttt{white}, \texttt{0pt}, \texttt{0pt} and \texttt{white} respectively.
%\section{Acknowledgements}
%Many thanks to the authors of \textsf{pdftricks} (Radhakrishnan CV, Rajagopal CV, and Antoine Chambert-Loir) and \textsf{auto-pst-pdf} (Will Robertson and Johannes Große) packages. This package could not exist without their combined efforts over many years as \mpg{} borrows some ideas from \textsf{pdftricks} and \textsf{auto-pst-pdf} packages.
%
%Enrico Gregorio deserves special mention for his wonderful suggestions and contributions which improved this package a lot.
%
%Finally, I wish to thank John D. Hobby, Taco Hoekwater, and other \mpost{} developers for developing \mpost.
% \StopEventually{}
%
% \section{\textsf{\jobname} implementation}
%\iffalse
%<*mpgraphics.sty>
%\fi
%
%
%
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\newcommand{\mpgraphics@date}{2011/07/25}
\newcommand{\mpgraphics@version}{v0.1}
\newcommand{\mpgraphics@commit}{commit 6}
\ProvidesPackage{mpgraphics}[\mpgraphics@date\space \mpgraphics@version\space <\mpgraphics@commit>
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}
\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 -o "#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}
%    \end{macrocode}
% \iffalse
%</mpgraphics.sty>
%\fi
%
% \Finale
%
%
%\iffalse
%<*mpgraphics-example.ltx>
\documentclass{article}
\usepackage{mpgraphics}
\begin{document}
\begin{mpdisplay}
LaTeX_unitlength := 1.0pt;
w := 60*LaTeX_unitlength;
h := 40*LaTeX_unitlength;
numeric xmin, xmax, ymin, ymax;
xmin := 1/4; xmax := 6; ymax := 1/xmin; u := 1cm;
vardef f(expr x) = 1/x enddef;
xinc := 0.1;
path pts_f;
pts_f := (xmin,f(xmin))*u
for x=xmin+xinc step xinc until xmax:
.. (x,f(x))*u
endfor;
path hline[], vline[];
hline0 = (0,0)*u -- (xmax,0)*u;
vline0 = (0,0)*u -- (0,ymax)*u;
vline0.5 = (0.5,0)*u -- (0.5,ymax)*u;
vline4 = (4,0)*u -- (4,ymax)*u;
fill buildcycle(hline0, vline0.5, pts_f, vline4)
withcolor 0.8[blue,white];
draw hline0; draw vline0; % draw axes
draw (0.5,0)*u -- vline0.5 intersectionpoint pts_f;
draw (4,0)*u -- vline4 intersectionpoint pts_f;
draw pts_f withpen pencircle scaled 2;
label.bot(btex $x$ etex, (0.9xmax,0)*u);
label.lft(btex $y$ etex, (0,0.9ymax)*u);
label.urt(btex Hello $f(x)$ etex, (0.5,f(0.5))*u);
\end{mpdisplay}
\end{document}
%</mpgraphics-example.ltx>
%\fi
%
% \typeout{*************************************************************}
% \typeout{*}
% \typeout{* To finish the installation you have to move the following}
% \typeout{* file into a directory searched by TeX:}
% \typeout{*}
% \typeout{* \space\space\space mpgraphics.sty  file}
% \typeout{*}
% \typeout{*************************************************************}
%
\endinput
