% \iffalse % egplot.dtx - Encapsulated gnuplot for LaTeX(2e)
% Copyright (C) 1998 by Axel.Probst@bam.de
%
% Egplot is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by 
% the Free Software Foundation; either version 2, or (at your option)
% any later version.
%
% Egp is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
%
% \fi
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%% \CheckSum{449}
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% \changes{v1.00}{1998/06/17}{First released version}
% \changes{v1.01}{1998/06/25}{Fixed bug with \texttt{german} option and 
%                             negative tic mark values}
% \changes{v1.02}{1998/07/08}{Added options \texttt{gnuplot35} and
%                             \texttt{gnuplot36beta} to allow output of
%                             syntax of \GP{}-3.5.\\
%                             Added numbering of \GP{} files to allow multiple
%                             \texttt{egpfile} environments w/o explicitly
%                             defined names in one document.}
%
% \MakeShortVerb{\|}
%
%
% \title{%
%   \EGP: \\
%   Encapsulated \GP{} for \LaTeX\thanks{%
%      This is \texttt{\filename}, version \fileversion, date \filedate.}}
%
% \author{%
%   Axel Probst\thanks{e-mail:
%     \texttt{Axel.Probst@bam.de}}\\
%   \hfil \\
%   c/o\\
%   Federal Institute for Materials Research and Testing\\
%   Unter den Eichen 87\\
%   D-12205 Berlin \\
%   Germany}
%
% \maketitle
% \begin{abstract}
%   The \EGP{} package allows to encapsulate \GP{} commands in \LaTeX{}
%   sources.  This is very useful for keeping illustrations in sync
%   with the text. It also frees the user from inventing descriptive
%   names for PostScript files. Additionally the package provides commands that
%   enable the user to let \GP{} do calculations and insert the result values
%   into the generated output.
% \end{abstract}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{multicols}{2}
% \tableofcontents
% \end{multicols}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \vfill
% \section*{Copying}
%
% \EGP{} is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by 
% the Free Software Foundation; either version 2, or (at your option)
% any later version.
%
% \EGP{} is distributed in the hope that it will be useful, but
% \emph{without any warranty}; without even the implied warranty of
% \emph{merchantability} or \emph{fitness for a particular purpose}.
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% \def\topfraction{0.9}
% \def\bottomfraction{0.9}
% \def\textfraction{0.1}
% \egpfigprelude{set terminal postscript eps monochrome dashed "Helvetica" 17^^J
%                set title ""; set grid^^J}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Introduction}
% \label{sec:intro}
%
% When adding illustrations to documents, one faces two bookkeeping
% problems:
% \begin{enumerate}
%   \item How to encourage oneself to keep the illustrations in sync
%     with the text, when the document is updated?
%   \item How to make sure that the illustrations appear on the right
%     spot? 
% \end{enumerate}
% For both problems, the best solution is to encapsulate the figures
% in the \LaTeX{} source:
% \begin{enumerate}
%   \item It is much easier to remember to update an illustration if
%     one doesn't have to switch files in the editor.
%   \item One does not have to invent illustrative filenames, if
%     the computer keeps track of them.
% \end{enumerate}
% This concept of integrating the image generating commands into the \LaTeX{}
% source was implemented for \MF{} by Thorsten
% Ohl\footnote{e-mail:\texttt{Thorsten.Ohl@Physik.TU-Darmstadt.de}} in the
% \EMP-package.
% The \EGP{} package now allows the encapsulation of \GP~\cite{GP} into
% \LaTeX~\cite{LaTeX-Graphics-Companion,LaTeX,LaTeX-Companion}. Although \GP{}
% provides several output formats that are suitable for the inclusion into
% \LaTeX{} the \EGP{} package is only intended for use with the Postscript
% terminal of \GP{} so far.
%
% In addition to the image inclusion commands \EGP{} provides the user with
% commands to let \GP{} do calculations and include the results into the
% document. Unfortunately these features are implemented with the UN*X text utils
% and so they are only usable if these are installed on the system. If the user
% does not provide a name for the \GP{} file the
% names for the PostScript and the result values files are built by appending
% the number of the \GP{} file, the figure/calculation number and a three
% letter extension (|.eps| or |.val|) to |\jobname|. So the user has to
% choose a |\jobname| that is short enough so that the generated
% filenames fit into the conventions of certain operating systems.
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Usage}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Options}
% \label{sec:options}
% 
% \DescribeMacro{Options}
% Besides the options of the |graphicx| package \EGP{} recognizes the following
% options:
% \begin{description}
% \item[|german|:] If |german| is specified the calculated values and the tic labels of the
%                  diagrams are changed to show a `,' as decimal point
%                  character. The default is a `.'. This feature is also
%                  implemented with UN*X text utils and is only available if
%                  they are installed on the system.
% \item[|gnuplot35|:] If |gnuplot35|~(default) is specified the \GP{} commands
%                     generated by |egplot| will be compatible with the syntax
%                     of the official \GP{} version~3.5. Of course the user
%                     has to look for the right syntax in his \GP{} code
%                     himself. Special care has to be taken for the
%                     |\egpprelude{|\dots|}| and the |\egpfigepilog{|\dots|}|
%                     commands since these are used to implement the missing
%                     |reset| command of \GP{}-3.5.
% \item[|gnuplot36beta|:] If |gnuplot36beta| is specified the \GP{} commands
%                     generated by |egplot| will use the features of the
%                     beta version \GP{}~3.6beta. As mentioned above the user
%                     has to look for the right syntax in his \GP{} code
%                     himself.
% \end{description}
%
% \subsection{Commands and Environments}
% \label{sec:commands}
%
% \subsubsection{Miscellaneous}
% \label{sec:misc}
%
% \DescribeEnv{egpfile}
% All descriptions that should go into one \GP{} file are placed
% inside a |egpfile| environment which takes the name of the \GP{}
% file as an optional argument:
% \begin{quote}
% \begin{flushleft}
%   |\begin{egpfile}[|\meta{\GP-file}|]|\\
%   \qquad\ldots\\
%   |\end{egpfile}|
% \end{flushleft}
% \end{quote}
% The default \GP-filename is |\jobname.gp|.
%
% \DescribeEnv{egpcmds}
% \DescribeMacro{\egpwrite}
% Write \GP{} commands to the current file outside of a figure. 
% The |\egpwrite| command is intended for short one line commands.
% \begin{quote}
% \begin{flushleft}
%   |\begin{egpcmds}|\\
%     \qquad\meta{\GP-commands}\\
%   |\end{egpcmds}|
% \end{flushleft}
% \end{quote}
%
% \DescribeMacro{\egpprelude}
% \DescribeMacro{\egpaddtoprelude}
% Define and add to the set of commands that are prepended to the top of
% every \GP{} file. It is intended for the global definition of variables
% or functions.
%
% The default is empty.
%
% \subsubsection{Figures}
% \label{sec:images}
%
% \DescribeEnv{egp}
% \DescribeEnv{egpx}
% \DescribeEnv{egpdef}
% The |egp| as the |egpx| environment contains the description of a single
% figure that will be placed at the location of the environment.
% The |egpdef| environment only defines a figure but does not include it into
% the document. This is useful, because these environments use the
% |verbatim| package and can therefore \emph{not} be used as an
% argument to other macros. The \meta{name} that is assigned to the figure is
% used for later inclusion with the |\egpuse{|\meta{name}|}| command.
% For the |egp| and |egpx| environment the assignment of the \meta{name} is
% optional.
% The required argument of the |egpx| environment accepts any set of keys
% accepted by the |\includegraphics| command of the |graphicx| package.
% \begin{quote}
% \begin{flushleft}
%   |\begin{egp}[|\meta{name}|]|\\
%     \qquad\meta{\GP-commands}\\
%   |\end{egp}|
% \end{flushleft}
% \end{quote}
% \begin{quote}
% \begin{flushleft}
%   |\begin{egpx}[|\meta{name}|]{|\meta{key val list}|}|\\
%     \qquad\meta{\GP-commands}\\
%   |\end{egpx}|
% \end{flushleft}
% \end{quote}
% \begin{quote}
% \begin{flushleft}
%   |\begin{egpdef}{|\meta{name}|}|\\
%     \qquad\meta{\GP-commands}\\
%   |\end{egpdef}|
% \end{flushleft}
% \end{quote}
%
% \DescribeMacro{\egpuse}
% Reuse a previously defined figure.
% The optional argument of the |\egpuse| command accepts any set of the keys
% that is accepted by the |\includegraphics| command of the |graphicx| package.
% \begin{quote}
% \begin{flushleft}
%   |\egpuse[|\meta{key val list}|]{|\meta{name}|}|\\
% \end{flushleft}
% \end{quote}
%
% \DescribeMacro{\egpfigprelude}
% \DescribeMacro{\egpaddtofigprelude}
% Define and add to a \GP{} prelude that is prepended to the output of
% every |egp|, |egpx| or |egpdef| environment. 
% The default is:
% \begin{quote}
% \qquad{}|set terminal postscript eps monochrome dashed "Helvetica" 17|
% \end{quote}
% In fact this is the command where the terminal for the \GP{}-|plot| command
% is set. So the user has to take care that (Encapsulated) PostScript output is
% generated.
%
% \DescribeMacro{\egpfigepilog}
% \DescribeMacro{\egpaddtofigepilog}
% Define and add to a \GP{} epilog that is appended to the output of
% every |egp|, |egpx| or |egpdef| environment. 
% This command can be used for e.g. |replot|ting the figure to the screen or
% |reset|ing to the defaults after every figure.
%
% The defaults are as follows:
% \begin{quote}
%      \begin{tabular}{lll} 
%       Option:      &    none, |gnuplot35|        &       |gnuplot36beta| \\
%                    &    |load "reset.gp"|        &       |reset|         \\
%      \end{tabular}
% \end{quote}
%
% \subsubsection{Calculating}
% \label{sec:calculations}
%
% In addition to the commands and environments to generate and include \GP{}
% figures the \EGP{}-package provides commands to use \GP{} for the 
% calculation of arbitrary arithmetic expressions. Since the \GP{}-|plot|
% command is used for this feature every expression that is accepted by this
% command is possible.  But this may also lead to unexpected results if the
% expression contains the variable~$x$ which is used as the independent
% variable of the \GP{}-|plot| command. As stated
% above~(cf.~p.~\pageref{sec:intro}) the UN*X text utils are used for the
% implementation and so the calculation commands can only be used on systems
% where these are installed.
%
% \DescribeMacro{\egpcalc}
% Let \GP{} calculate the value of a \meta{\GP-expression}. The result is written to a
% file. 
% The optional argument assigns a name to be used with |\egpuseval{|\meta{name}|}|.
% \begin{quote}
% \begin{flushleft}
%   |\egpcalc[|\meta{name}|]{|\meta{\GP-expression}|}|\\
% \end{flushleft}
% \end{quote}
%
% \DescribeMacro{\egpuseval}
% Insert a previously defined calculation result. 
%
% \DescribeMacro{\egpshowval}
% Does the same as the |\egpcalc|-command but additionally the result is placed 
% in the output at the position of the |\egpshowval|-command.
%
% \DescribeMacro{\egpassign}
% The first argument is the name of a \meta{\GP-variable} or \meta{\GP-user
% function} which is assigned the second argument which is a
% \meta{\GP-expression}. The result is placed in the output as for the
% |\egpshowval| command. 
% 
% \subsection{Procedure}
% \label{sec:procedure}
%
% After \LaTeX{} has done it's job for the first time you have to invoke \GP{}
% on the generated file~(default: |\jobnameX.gp|, where |X| is a number). Then
% another \LaTeX{} run is necessary to include the figures and the results into
% the output.
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{Examples}
% \label{sec:examples}
%
% For a simple example, let's draw the function
% $f(x)=\sin({\sqrt{x^2+y^2}})/\sqrt{x^2+y^2}$.
%    \begin{macrocode}
%<*sample>
\begin{egpfile}
\begin{center}
\begin{egpx}[sombrero]{width=0.8\linewidth}
     set hidden3d
     set nogrid
     set samples 1000
     set isosamples 35
     splot [-10:10] [-10:10] sin(sqrt(x*x+y*y))/sqrt(x*x+y*y)
\end{egpx}
\end{center}
%    \end{macrocode}
\begin{egpfile}
\begin{center}
\begin{egpx}[sombrero]{width=0.8\linewidth}
     set hidden3d
     set nogrid
     set samples 1000
     set isosamples 35
     splot [-10:10] [-10:10] sin(sqrt(x*x+y*y))/sqrt(x*x+y*y)
\end{egpx}
\end{center}
%
% Additionally we define a figure that will not be shown here but at the place
% of the appropriate |\egpuse| command.
% 
%    \begin{macrocode}
\begin{egpdef}{kleinbottle}
     set hidden3d
     set parametric
     set nokey
     set nogrid
     set noborder
     set noxtics
     set noytics
     set noztics
     set xrange [-10:10]
     set yrange [-10:10]
     set zrange [-3:3]
     set urange [0:2*pi]
     set vrange [0:2*pi]
     set isosamples 39,60
     set view 60,120
     set title "Klein bottle"
     splot  (2*sin(u)*cos(v/2)-sin(2*u)*sin(v/2)+8)*cos(v), \
            (2*sin(u)*cos(v/2)-sin(2*u)*sin(v/2)+8)*sin(v), \
            2*sin(u)*sin(v/2)+sin(2*u)*cos(v/2)
\end{egpdef}
%    \end{macrocode}
\begin{egpdef}{kleinbottle}
     set hidden3d
     set parametric
     set nokey
     set nogrid
     set noborder
     set noxtics
     set noytics
     set noztics
     set xrange [-10:10]
     set yrange [-10:10]
     set zrange [-3:3]
     set urange [0:2*pi]
     set vrange [0:2*pi]
     set isosamples 39,60
     set view 60,120
     set title "Klein bottle"
     splot  (2*sin(u)*cos(v/2)-sin(2*u)*sin(v/2)+8)*cos(v), \
            (2*sin(u)*cos(v/2)-sin(2*u)*sin(v/2)+8)*sin(v), \
            2*sin(u)*sin(v/2)+sin(2*u)*cos(v/2)
\end{egpdef}
%
% Since we have given a name to each diagram, we can now use them with 
%    \begin{macrocode}
\begin{figure}
  \begin{center}
     \fbox{\egpuse[scale=0.4]{sombrero}}
     \fbox{\egpuse[scale=0.4]{kleinbottle}}
  \caption{Two examples taken from the \GP{} demo}\label{fig:demo}
  \end{center}
\end{figure}
%    \end{macrocode}
%
% and the result is shown in figure~\ref{fig:demo}.
%
\begin{figure}
  \begin{center}
     \fbox{\egpuse[scale=0.4]{sombrero}}
     \fbox{\egpuse[scale=0.4]{kleinbottle}}
  \caption{Two examples taken from the \GP{} demo}\label{fig:demo}
  \end{center}
\end{figure}
%
% To calculate the value of $f(\pi/4)$ we issue the command
%
% $f(\pi/4)=\egpshowval[sin_quarter_pi]{sin(pi/4)}$
%    \begin{macrocode}
$f(\pi/4)=\egpshowval[sin_quarter_pi]{sin(pi/4)}$
%    \end{macrocode}
% and get $\frac{\sqrt{2}}{2}=\fbox{\egpuseval{sin_quarter_pi}}\footnote{I
% couldn't figure out how to remove the trailing space, yet. Any hints ?}$
%    \begin{macrocode}
and get $\frac{\sqrt{2}}{2}=\fbox{\egpuseval{sin_quarter_pi}}$.
\end{egpfile}
%    \end{macrocode}
%
\end{egpfile}
%    \begin{macrocode}
%</sample>
%    \end{macrocode}
%
% \section{Acknowledgements}
% I would like to thank Thorsten Ohl for submitting the \EMP{} package to CTAN.
% By using it as a template I managed it to adapt the idea of integrating the
% image generating commands into \LaTeX{} for \GP{}. A lot of code of the
% \EMP{} package was reused with only marginal changes.  This is also caused by
% the fact that I am far away from understanding all of the code of \EMP{}.
%
% \section{Todo}
% In additon to optimising \EGP{} it would be nice if the features that are
% provided through the use of UN*X text utils were implemented in
% \TeX{}/\LaTeX{}. Another interesting feature to implement in following
% versions of \EGP{} is the possibility to use other output formats 
% provided by \GP{}, especially the |pslatex| and |pstricks| terminals but also
% the |png| terminal for inclusion into PDF could be useful.
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{thebibliography}{99}
%   \bibitem{LaTeX-Graphics-Companion} Michel Goossens, Sebastian
%     Rahtz, and Frank Mittelbach, \textit{The \LaTeX{} Graphics
%     Companion}, Addison-Wesley, Reading MA, 1997.
%   \bibitem{LaTeX} Leslie Lamport, \textit{\LaTeX{} --- A
%     Documentation Preparation System},
%     Addison-Wesley, Reading MA, 1985.
%   \bibitem{LaTeX-Companion} Michel Goossens, Frank Mittelbach, and
%     Alexander Samarin, \textit{The \LaTeX{} Companion},
%     Addison-Wesley, Reading MA, 1994.
%   \bibitem{EMP} Thorsten Ohl, \texttt{emp}, available from
%     CTAN (cf.~p.~\pageref{pg:CTAN}), in the \texttt{macros/latex/contrib/supported/emp}
%     directory.
%   \bibitem{GP} Thomas Williams and Colin Kelley, \textit{gnuplot}, available
%   from \texttt{ftp.dartmouth.edu} in the \texttt{/pub/gnuplot} directory.
% \end{thebibliography}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section*{Distribution}
% \label{sec:distribution}
%
% \EGP{} is available by anonymous internet ftp from any of the
% Comprehensive \TeX{} Archive Network (CTAN) hosts 
% \label{pg:CTAN}
% \begin{quote}
%   |ftp.tex.ac.uk|, |ftp.dante.de|
% \end{quote}
% in the directory
% \begin{quote}
%   |macros/latex/contrib/supported/egplot|
% \end{quote}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \StopEventually{\PrintIndex\PrintChanges}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Implementation}
%
%    \begin{macrocode}
%<*style>
\def\fileversion{v1.02a}
\NeedsTeXFormat{LaTeX2e}
\gdef\filename{egplot.sty}%
\gdef\filedate{1998/07/08}%
\gdef\filemaintainer{Axel Probst}%
%    \end{macrocode}
%
% And now the standard procedure:
%    \begin{macrocode}
\ProvidesPackage{egplot}[\filedate\space\fileversion\space
  Encapsulated gnuplot LaTeX Package (\filemaintainer)]
%    \end{macrocode}
%
% Load the required packages:
%    \begin{macrocode}
\RequirePackage{verbatim}
\RequirePackage{ifthen}
%    \end{macrocode}
%
% Now the options are specified:
%    \begin{macrocode}
\newboolean{egp@german}
\setboolean{egp@german}{false}
\DeclareOption{german}{%
        \setboolean{egp@german}{true}}
\newboolean{egp@oldgp}
\setboolean{egp@oldgp}{true}
\DeclareOption{gnuplot35}{%
        \setboolean{egp@oldgp}{true}}
\DeclareOption{gnuplot36beta}{%
        \setboolean{egp@oldgp}{false}}
%    \end{macrocode}
%
% Every option we don't understand is sent down to |graphicx|:
%    \begin{macrocode}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{graphicx}}
\ProcessOptions
\RequirePackage{graphicx}[1994/12/15]
%    \end{macrocode}
%
% \begin{macro}{\egpwrite}
% Write out the argument to the \GP{} file.
%    \begin{macrocode}
{\catcode`\#=11\gdef\egpcomment{#}}
\def\egpwrite#1{%
  \if@egpio
    \immediate\write\@outegp{#1}%
  \fi
  \ignorespaces}
\newif\if@egpio
\@egpiotrue
\newwrite\@outegp
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egpfile}
% This environment encloses each \GP{} input file.  The single optional
% argument gives the name of the file.
%    \begin{macrocode}
\newcounter{egpfilenum}            % 1998-03-07
\setcounter{egpfilenum}{0}         % 1998-03-07
\newcommand{\egpfile}[1][\jobname\theegpfilenum_]{%
  \def\theegpfile{#1}%
  \ifthenelse{\equal{\theegpfile}{\jobname\theegpfilenum_}}{%
     \stepcounter{egpfilenum}%
     \def\theegpfilename{\jobname\theegpfilenum.gp}}{%
     \def\theegpfilename{\theegpfile.gp}}
%    \end{macrocode}
% Open the \GP{} file.  
%    \begin{macrocode}
  \if@egpio
    \immediate\openout\@outegp=\theegpfilename\relax
    \egpwrite{\egpcomment\space \theegpfilename -- %
              do not edit, generated automatically by \jobname.tex^^J}
%    \end{macrocode}
% append the defined prelude and write it out: 
%    \begin{macrocode}
    \expandafter\ifx\expandafter*\the\egp@prelude*\else
      \egpwrite{\the\egp@prelude;}%
    \fi
  \fi
%    \end{macrocode}
% Count the figures and the calculations
%    \begin{macrocode}
  \setcounter{egpfig}{0}
  \setcounter{egpcalc}{0}}
\let\theegpfile\relax
\newcounter{egpfig}
\newcounter{egpcalc}
%    \end{macrocode}
% Standard preludes for the whole file and for every figure and the per figure
% epilog:
%    \begin{macrocode}
\newtoks\egp@prelude
\newtoks\egp@figprelude
\newtoks\egp@figepilog
%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egpprelude}
%   \begin{macro}{\egpfigprelude}
%     \begin{macro}{\egpfigepilog}
%       \begin{macro}{\egpaddtoprelude}
%         \begin{macro}{\egpaddtofigprelude}
%           \begin{macro}{\egpaddtofigepilog}
% Define and add to the file or figure prelude and the figure epilog.
%    \begin{macrocode}
\def\egpprelude#1{\egp@prelude={#1}}
\def\egpfigprelude#1{\egp@figprelude={#1}}
\def\egpfigepilog#1{\egp@figepilog={#1}}
\def\egpaddtoprelude#1{\egp@prelude=\expandafter{\the\egp@prelude^^J#1}}
\def\egpaddtofigprelude#1{\egp@figprelude=\expandafter{\the\egp@figprelude^^J#1}}
\def\egpaddtofigepilog#1{\egp@figepilog=\expandafter{\the\egp@figepilog^^J#1}}
%    \end{macrocode}
%           \end{macro}
%         \end{macro}
%       \end{macro}
%     \end{macro}
%   \end{macro}
% \end{macro}
%
% \begin{macro}{\endegpfile}
% And here is how we close the |egpfile| environment:
%    \begin{macrocode}
\def\endegpfile{%
  \egpwrite{\egpcomment\space the end.}%
  \let\theegpfile\relax
  \if@egpio
    \immediate\closeout\@outegp
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egp}
%   \begin{macro}{\egpx}
%     \begin{macro}{\egpdef}
% Here are the environments to define and to define and include the \GP{} diagrams.
%    \begin{macrocode}
\newcommand{\egp}[1][*]{%
  \def\egp@@name{#1}%
  \egp@}
\newcommand{\egpx}[2][*]{%
   \def\egp@@name{#1}%
   \egp@x{#2}}
\newcommand{\egpdef}[1]{%
  \def\egp@@name{#1}%
  \egp@def}
%    \end{macrocode}
%     \end{macro}
%   \end{macro}
% \end{macro}
%
% \begin{macro}{\egp@}
%   \begin{macro}{\egp@x}
%     \begin{macro}{\egp@def}
% And here the real work is done.
%    \begin{macrocode}
\def\egp@{%
  \egp@start%
  \ifthenelse{\boolean{egp@oldgp}}
     {\egpwrite{\egpcomment\space --- \theegpfile\theegpfig.eps ---}}
     {\egpwrite{print 'generating picture ---- \theegpfile\theegpfig.eps'}}
  \egpwrite{set output '\theegpfile\theegpfig.eps'}
  \egp@includegraphics{\theegpfile}{\theegpfig}%
  \egpcmds}
\def\egp@x#1{%
  \egp@start%
  \ifthenelse{\boolean{egp@oldgp}}
     {\egpwrite{\egpcomment\space --- \theegpfile\theegpfig.eps ---}}
     {\egpwrite{print 'generating picture ---- \theegpfile\theegpfig.eps'}}
  \egpwrite{set output '\theegpfile\theegpfig.eps'}
  \egp@includegraphicx[#1]{\theegpfile}{\theegpfig}%
  \egpcmds}
\def\egp@def{%
  \egp@start%
  \ifthenelse{\boolean{egp@oldgp}}
     {\egpwrite{\egpcomment\space --- \theegpfile\theegpfig.eps ---}}
     {\egpwrite{print 'generating picture ---- \theegpfile\theegpfig.eps'}}
  \egpwrite{set output '\theegpfile\theegpfig.eps'}
  \egpcmds}
%    \end{macrocode}
%     \end{macro}
%   \end{macro}
% \end{macro}
%
% \begin{macro}{\egp@start}
%    \begin{macrocode}
\def\egp@start{%
  \egp@checkfile
%    \end{macrocode}
% We can't use |\stepcounter| because of the |amstext| option of
% AMS-\LaTeX{} disables it sometimes.
%    \begin{macrocode}
  \global\expandafter\advance\csname c@egpfig\endcsname \@ne
  \egp@@def{\egp@@name}%
%    \end{macrocode}
% Start the \GP{} figure:
%    \begin{macrocode}
   \expandafter\ifx\expandafter*\the\egp@figprelude*\else
    \egpwrite{\the\egp@figprelude}%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egp@checkfile}
% Make sure that a \GP{} file is open, otherwise \emph{really}
% obscure error messages are possible:
%    \begin{macrocode}
\def\egp@checkfile{%
  \ifx\theegpfile\relax
    \errhelp={Outside an egpfile environment, I have no clue as to where^^J%
              the gnuplot commands should go.   I will use egpdefault.gp^^J%
              for this graph, but you'd better fix your code!}%
    \errmessage{I detected a egp environment outside of egpfile}%
    \egpfile[egpdefault]
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egp@includegraphics}
%   \begin{macro}{\egp@includegraphicx}
% Include the Postscript files that were generated by \GP{}
%    \begin{macrocode}
\def\egp@includegraphics#1#2{%
  \leavevmode
  \IfFileExists{#1#2.eps}%
    {\includegraphics{#1#2.eps}}%
    {\typeout{%
      egp: File #1#2.eps\space not found:^^J%
      egp: Process \theegpfilename\space with gnuplot and then %
           reprocess this file.}}}
\newcommand{\egp@includegraphicx}[3][scale=1]{%
  \leavevmode
  \IfFileExists{#2#3.eps}%
    {\includegraphics[#1]{#2#3.eps}}%
    {\typeout{%
      egp: File #2#3.eps\space not found:^^J%
      egp: Process \theegpfilename\space with gnuplot and then %
           reprocess this file.}}}
%    \end{macrocode}
%   \end{macro}
% \end{macro}
%
% \begin{macro}{\egpcmds}
% Write to the file:
%    \begin{macrocode}
\def\egpcmds{%
  \begingroup
    \@bsphack
    \let\do\@makeother\dospecials
    \catcode`\^^M\active
    \def\verbatim@processline{\egpwrite{\the\verbatim@line}}%
    \verbatim@start}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endegpcmds}
%    \begin{macrocode}
\def\endegpcmds{%
    \@esphack
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endegp}
%   \begin{macro}{\endegpx}
%     \begin{macro}{\endegpdef}
% If the |german| option is used the decimal point character is changed to be
% |{,}|. This is done to avoid the additional space \LaTeX{} inserts after the
% `,' in math mode. This is implemented by using some of the UN*X text utils
% and therefore these have to be availlable on the system to benefit from this
% feature.
%    \begin{macrocode}
\def\endegp{%
  \endegpcmds
  \ifthenelse{\boolean{egp@german}}{%
       \egpwrite{!sed -e '/[0-9]*[.][0-9]*)\ .show/s/[.]/,/' %
                 \theegpfile\theegpfig.eps >\theegpfile\theegpfig.tmp}
       \egpwrite{!cp \theegpfile\theegpfig.tmp \theegpfile\theegpfig.eps}
       \egpwrite{!rm -f \theegpfile\theegpfig.tmp}}
       {}
   \expandafter\ifx\expandafter*\the\egp@figepilog*\else
    \egpwrite{\the\egp@figepilog}%
  \fi
  \egpwrite{}}
\def\endegpx{\endegp}
\def\endegpdef{\endegp}
%    \end{macrocode}
%     \end{macro}
%   \end{macro}
% \end{macro}
%
% \begin{macro}{\egp@@def}
%    \begin{macrocode}
\def\egp@@def#1{%
  \global\e@namedef{egp@k:f:#1}{\theegpfile}%
  \global\e@namedef{egp@k:c:#1}{\theegpfig}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\e@namedef}
%    \begin{macrocode}
\def\e@namedef#1{\expandafter\edef\csname #1\endcsname}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egpuse}
% Reuse a previously defined figure. The figure is referred to by the name
% given on the |egp|, |egpx| or |egpdef| environment.
%    \begin{macrocode}
\newcommand{\egpuse}[2][scale=1]{%
  \@ifundefined{egp@k:f:#2}%
   {\typeout{egp: \string\egpuse: `#2' undefined!}}%
   {\egp@includegraphicx[#1]{\@nameuse{egp@k:f:#2}}{\@nameuse{egp@k:c:#2}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egpcalc}
% Calculate the expression in the required argument.
%    \begin{macrocode}
\newcommand{\egpcalc}[2][*]{%
   \def\egp@@name{#1}%
   \def\egp@expression{#2}
   \egp@calc}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egp@calc}
% Write the commands to the \GP{} file.
% To get the calculated results in a file the \GP{} |table| terminal is used.
% The number of samples is set to the lowest possible value and the |zero|
% tolerance is set to 0.
%    \begin{macrocode}
\def\egp@calc{%
   \egp@checkfile
   \global\expandafter\advance\csname c@egpcalc\endcsname \@ne
   \egpc@@def{\egp@@name}%
   \ifthenelse{\boolean{egp@oldgp}}
      {\egpwrite{\egpcomment\space --- \theegpfile\theegpcalc.val ---}}
      {\egpwrite{print 'calculating value ----- \theegpfile\theegpcalc.val'}}
   \egpwrite{set term table; set output '\theegpfile\theegpcalc.tmp'^^J%
             set samples 2^^J%
             set zero 0^^J%
             plot [0:0] \egp@expression}
%    \end{macrocode}
%
% Here intensive usage of UN*X text utils is made to extract the calculated
% value out of the file \GP{} generated. 
%
% If the |german| option is used the decimal point character is changed to be
% `|{,}|'. This is done to avoid the additional space \LaTeX{} inserts after the
% `,' in math mode.
%
% Maybe someone is able to implement all this in \TeX{} what would make this
% package much more portable.
%
%    \begin{macrocode}
   \ifthenelse{\boolean{egp@german}}{%
       \egpwrite{!tail -3 \theegpfile\theegpcalc.tmp | head -1 |%
                 cut -f 2 -d' ' | sed -e 's/[.]/{,}/' %
                 >\theegpfile\theegpcalc.val}}
       {\egpwrite{!tail -3 \theegpfile\theegpcalc.tmp | head -1 |%
                  cut -f 2 -d' ' >\theegpfile\theegpcalc.val}}
   \egpwrite{!rm -f \theegpfile\theegpcalc.tmp}
%   \ifthenelse{\boolean{egp@oldgp}}{%
%       \egpwrite{load "reset.gp"}}{%
%       \egpwrite{reset}}
   \egpwrite{}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egpc@@def}
%    \begin{macrocode}
\def\egpc@@def#1{%
  \global\e@namedef{egp@k:f:#1}{\theegpfile}%
  \global\e@namedef{egp@k:v:#1}{\theegpcalc}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egp@includevalue}
% With this command the generated result is read into the \LaTeX{} file.
% Unfortunately a trailing \verb*+\ + is shown after the included value what is
% caused --- as I think --- by the |\input| command. There should be a way to
% avoid this but I don't know how.  Any wizards out there?
%    \begin{macrocode}
\newcommand{\egp@includevalue}[2]{%
% \InputIfFileExists{#1#2.val}{\ignorespaces}%
  \IfFileExists{#1#2.val}%
               {\input{#1#2.val}}%
               {\typeout{%
                  egp: File #1#2.val\space not found:^^J%
                  egp: Process \theegpfilename\space with gnuplot and then %
                       reprocess this file.}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egpshowval}
% Calculate and include the result during the \LaTeX{} run.
%    \begin{macrocode}
\newcommand{\egpshowval}[2][*]{%
   \def\egp@@name{#1}%
   \def\egp@expression{#2}%
   \egp@calc%
   \egp@includevalue{\theegpfile}{\theegpcalc}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egpuseval}
% Include a previously defined value.
%    \begin{macrocode}
\newcommand{\egpuseval}[1]{%
  \@ifundefined{egp@k:f:#1}%
   {\typeout{egp: \string\egpuseval: `#1' undefined!}}%
   {\egp@includevalue{\@nameuse{egp@k:f:#1}}{\@nameuse{egp@k:v:#1}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\egpassign}
%    \begin{macrocode}
\newcommand{\egpassign}[2]{%
   \egpwrite{#1=#2}\egpshowval{#1}}
%    \end{macrocode}
% \end{macro}
%
% Define the file prelude:
% If the user specifies that the official version \GP{}-3.5 is used a file with
% the name |reset.gp| is generated at the start of the \GP{} run. Wherever a
% |reset| is done in the \GP{}-3.6 file this file is |load|ed instead.
%    \begin{macrocode}
\ifthenelse{\boolean{egp@oldgp}}
   {\egpprelude{save "reset.gp"}}
   {\relax}
%    \end{macrocode}
%
% Define the default prelude for the figures:
%    \begin{macrocode}
\egpfigprelude{set terminal postscript eps monochrome dashed "Helvetica" 17}
%    \end{macrocode}
% To get e.g. Computer Modern as font for the axis tics you can specify the
% name of a CM-Type-1 font file as fontname option of the \GP{} postscript
% terminal. 
% For example:
% \begin{quote}
% \begin{flushleft}
% |\egpfigprelude{set terminal postscript eps monochrome dashed "CMSS17" 20}|
% \end{flushleft}
% \end{quote}
% To see the correct font in the Postscript file you have to use the
% appropriate fontmap when calling dvips or you have to download the file
% |cmss17.pfb| as header file. The error message of dvips can then be
% ignored.\\
%
%
%
% Reset all options to their default values after every |egp|, |egpx| and
% |egpdef| environment.
% As mentioned above the file |reset.gp| that is generated at the start of the
% \GP{} run is |load|ed to implement the new |reset| command of \GP{}-3.6beta
% if the user didn't specify |gnuplot36beta| as package option.
%    \begin{macrocode}
\ifthenelse{\boolean{egp@oldgp}}{%
   \egpfigepilog{load "reset.gp"}}{%
   \egpfigepilog{reset}}
%    \end{macrocode}
%
% You can configure \EGP{} by putting the appropriate commands in the file
% egplot.cfg that has to be located where \TeX{} can find it.
%    \begin{macrocode}
\InputIfFileExists{egplot.cfg}
                  {\typeout{egp: Using configuration file 'egplot.cfg'}}
                  {}
%</style>
%    \end{macrocode}
%
% \Finale
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \appendix
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Driver File}
%
%    \begin{macrocode}
%<*driver>
\documentclass[a4paper]{article}
\usepackage{doc}
\usepackage{multicol}
%    \end{macrocode}
%
%    \begin{macrocode}
\IfFileExists{mflogo.sty}%
  {\usepackage{mflogo}%
   \def\GP{\textsf{gnuplot}}%
   \def\EGP{\textsf{egplot}}%
   \def\EMP{\textlogo{EMP}}}%
  {\def\GP{\textsf{gnuplot}}%
   \def\EMP{\textsf{EMP}}%
   \def\EGP{\textsf{egplot}}}
%    \end{macrocode}
%
%    \begin{macrocode}
\usepackage[gnuplot35]{egplot}
%\usepackage[gnuplot36beta]{egplot}
%    \end{macrocode}
%    \begin{macrocode}
\setlength{\parindent}{0pt}
\def\manindex#1{\SortIndex{#1}{#1}}
%<manual>\OnlyDescription
\EnableCrossrefs
\RecordChanges
\CodelineIndex
\DoNotIndex{\def,\gdef,\long,\let,\begin,\end,\if,\ifx,\else,\fi}
\DoNotIndex{\immediate,\write,\newwrite,\openout,\closeout,\typeout}
\DoNotIndex{\font,\jobname,\documentclass,\char,\catcode,\ }
\DoNotIndex{\CodelineIndex,\DocInput,\DoNotIndex,\EnableCrossrefs}
\DoNotIndex{\filedate,\filename,\fileversion,\logo,\manfnt}
\DoNotIndex{\NeedsTeXFormat,\ProvidesPackage,\RecordChanges,\space}
\DoNotIndex{\begingroup,\csname,\edef,\endcsname,\expandafter}
\DoNotIndex{\usepackage,\@ifundefined,\ignorespaces,\item,\leavevmode}
\DoNotIndex{\newcounter,\newif,\par,\parindent}
\DoNotIndex{\relax,\setcounter,\stepcounter,\the,\advance}
\DoNotIndex{\CurrentOption,\DeclareOption,\documentstyle}
\DoNotIndex{\endgroup,\global,\hfuzz,\LaTeX,\LaTeXe}
\DoNotIndex{\macrocode,\@makeother,\OnlyDescription,\PassOptionsToPackage}
\DoNotIndex{\ProcessOptions,\RequirePackage,\string,\textsf,\unitlength}
\DoNotIndex{\@bsphack,\@esphack,\@nameuse,\@ne,\active,\do,\dospecials}
\DoNotIndex{\errhelp,\errmessage,\ifcase,\IfFileExists,\includegraphics}
\DoNotIndex{\manindex,\SortIndex,\newcommand,\newtoks,\or,\origmacrocode}
\DoNotIndex{\alpha,\displaystyle,\frac,\sin,\texttt}
%    \end{macrocode}
% Cut the line breaking some slack for macro code which might contain
% long lines (it doesn't really hurt if they stick out a bit).
%    \begin{macrocode}
\let\origmacrocode\macrocode
\def\macrocode{\hfuzz 5em\origmacrocode}
\begin{document}
  \DocInput{egplot.dtx}
\end{document}
%</driver>
%    \end{macrocode}
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
