% Defie thirteen verbatim environments:
%  Verbatim  - indented like quote environment, \small font.
%  nVerbatim - Like "verbatim", but numbered.
%  example   - like original, side by side
%  example*  - one above other
%  example** - one above other, with frames
%  Example   - Rendered as PS, like original, side by side
%  Example*  - Rendered as PS, one above other
%  Example** - Rendered as PS, one above other, with frames
%  Example*** - Rendered as PS, with *no* verbatim text
%  GridPSExample   - PSTricks (gridded) picture, like original, side by side
%  PSExample   - PSTricks picture, like original, side by side
% The example environments have the following steps in common:
% 1. Save example verbatim to numbered example file
% 2. Input file verbatim  or EPS file
% 3. Input again 
% All of them save their text as files in the current directory

% " can be used to add lines that affect the output of the example,
% but do not appear in the verbatim listing.

\DefineVerbatimEnvironment{Verbatim}{LVerbatim}{fontsize=\small}
\DefineVerbatimEnvironment{nVerbatim}{LVerbatim}{fontsize=\small,numbers=left}

\newcounter{epscounter}
\c@epscounter0
\newwrite\exepsfile
\renewcommand\theepscounter{\arabic{chapter}-\arabic{section}-\arabic{epscounter}}
\immediate\openout\exepsfile \jobname.expslist\relax

\def\EPSinput#1{%
\IfFileExists{\ex@@prefix#1.eps}%
 {\includegraphics{\ex@@prefix#1.eps}}%
 {\underline{\ex@@prefix#1}}%
}
\newcount\pst@ex@count
\pst@ex@count0

\def\SideBySideEx#1{% 
\global\advance\pst@ex@count by 1
  \trivlist\item[]%
  \leavevmode
   \label{\thechapter.\the\pst@ex@count}%
   \ifthenelse{\isodd{\pageref{\thechapter.\the\pst@ex@count}}}{%
   \makebox[\textwidth][l]{\@@Display{#1}\hspace{5mm}
    \BVerbatimInput{\ex@@prefix\theepscounter}}%
      }{%
   \makebox[\textwidth][r]{\@@Display{#1}\hspace{5mm}
     \BVerbatimInput{\ex@@prefix\theepscounter}}%
    }%
  \endtrivlist
}
\def\@@Display#1{%
 \begin{minipage}[b]{.5\textwidth}#1\end{minipage}%
}

\def\ex@@prefix{}
\newcommand{\BeginExample}{%
  \multiply\topsep by 2
  \global\advance\c@epscounter\@ne
  \edef\@tempa{
    \write\@auxout{\string
      \newlabel{ex-\theepscounter}{{\theepscounter}{\noexpand\the\c@page}}}}
  \@tempa
  \nobreak
  \edef\@tempa{
    \write\exepsfile{\string
      \exampleonpage{ex-\theepscounter}{\noexpand\the\c@page}}}
  \@tempa
  \VerbatimEnvironment
  \catcode`\<=12
  \begin{VerbatimOut}{\ex@@prefix\theepscounter}}
\newcommand{\EndExample}{%
  \end{VerbatimOut}%
}

\def\EndRuleExample{\vskip 7pt \hrule height 1.5pt\relax}

\newif\ifgrid
\gridfalse

\def\PSExample(#1,#2)(#3,#4){%
    \def\PictureSize{(#1,#2)(#3,#4)}%
    \def\VOMIT##1{}\BeginExample
   }
\def\endPSExample{%
  \EndExample
  \SideBySideEx{%
    \ifgrid\vskip 8pt \else\relax\fi
    \expandafter\pspicture\PictureSize
    \ifgrid\usergrid\else\relax\fi
    \input{\theepscounter}%
    \endpspicture
    \ifgrid\vskip 8pt \fi
 }%
}

\newenvironment{GridPSExample}{\gridtrue\PSExample}{\endPSExample}

% side by side
\newenvironment{example}{\def\VOMIT##1{}\BeginExample} %
  {\EndExample\SideBySideEx{\input{\theepscounter}}}

% one above the other
\newenvironment{example*}%
  {\def\VOMIT##1{}\BeginExample}%
  {%
    \EndExample
    \VerbatimInput[gobble=0,fontsize=\small]{\theepscounter}%
      \begin{center}
        \leavevmode\input{\theepscounter}\par
      \end{center}
}

% with a frame, one above the other
\newenvironment{example**}%
{\def\VOMIT##1{}\BeginExample}%
{%
\EndExample
\VerbatimInput[gobble=0,fontsize=\small,
      frame=topline,framerule=1.5pt]{\theepscounter}%
\begin{center}
\input{\theepscounter}
        \EndRuleExample
\end{center}
}

% now the ones which write stuff for an EPS file

% side by side
\newenvironment{Example}{\def\ex@@prefix{a}\BeginExample} %
 {\EndExample
\def\FancyVerbStopString{STOPSTOPSTOP}%
\def\FancyVerbStartString{GOGOGO}%
\SideBySideEx{\EPSinput{\theepscounter}}}

% first verbatimcode, then actual example
\newenvironment{Example*}{\def\ex@@prefix{b}\BeginExample}%
  {\EndExample
\def\FancyVerbStopString{STOPSTOPSTOP}%
\def\FancyVerbStartString{GOGOGO}%
  \begin{center}\leavevmode
    \VerbatimInput[gobble=0,fontsize=\small]{\ex@@prefix\theepscounter}%
 \EPSinput{\theepscounter}%
 \end{center}%
}

% one above the other, framed
\newenvironment{Example**}%
{\def\ex@@prefix{b}\BeginExample}%
{%
\EndExample
\def\FancyVerbStopString{STOPSTOPSTOP}%
\def\FancyVerbStartString{GOGOGO}%
    \VerbatimInput[gobble=0,fontsize=\small,
      frame=topline,framerule=1.5pt]{\ex@@prefix\theepscounter}%
\EPSinput{\theepscounter}
\EndRuleExample
}

% no verbatim text at all, just to run the thing separately
\newenvironment{Example***}%
{\def\ex@@prefix{b}\BeginExample}%
{%
\def\FancyVerbStopString{STOPSTOPSTOP}%
\def\FancyVerbStartString{GOGOGO}%
\EndExample
\centerline{\EPSinput{\theepscounter}}
}

\def\CurrentPackages{}

\def\FVE@VerbatimOut{\VOMIT{STOPSTOPSTOP}%
\immediate\closeout\FV@OutFile\endgroup\@esphack}

\def\VOMIT#1{%
      \begingroup
       \let\protect\@unexpandable@protect
       \edef\reserved@a{\immediate\write\FV@OutFile{#1}}
       \reserved@a
      \endgroup
}

\def\FVB@VerbatimOut#1{%
  \@bsphack
  \begingroup
    \FV@UseKeyValues
    \FV@DefineWhiteSpace
    \def\FV@Space{\space}%
    \FV@DefineTabOut
    \def\FV@ProcessLine{\immediate\write\FV@OutFile}%
    \immediate\openout\FV@OutFile #1\relax
    \VOMIT{\string\documentclass{ppex\ex@@prefix}}%
    \VOMIT{\string\usepackage{\CurrentPackages}}%
    \VOMIT{GOGOGO}%
    \let\FV@FontScanPrep\relax
    \FV@Scan}

\def\SixPt{\fontsize{6}{7\p@}\selectfont}
\DefineVerbatimEnvironment{verbatimzzz}{LVerbatim}{fontsize=\SixPt}
\def\verbfile#1{\VerbatimInput[fontsize=\small]{#1}}
\endinput