% Define verbatim and example environments:
%  verbatim  - indented like quote environment
%  Verbatim  - indented like quote environment, \small font.
%  nVerbatim - Like "Verbatim", but numbered.
%  example   - code and output, 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
%  Escape - 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 \EXdir directory
% (this needs a trailing / as part of the definition)
%
\RequirePackage{ifthen}
\RequirePackage{graphicx}
\RequirePackage{calc}
%
% variable features
%
\def\EXdir{}
\def\EXprefix{}
\def\EXclass{article}
%
\@ifundefined{fullwidth}{\let\fullwidth\textwidth}{}
\@ifundefined{thechapter}{\newcounter{chapter}}{}
%
\newcounter{eps@counter}[section]
\renewcommand\theeps@counter{%
 \arabic{chapter}-\arabic{section}-\arabic{eps@counter}}
\newcounter{side@count}
%\newwrite\exepsfile
%\immediate\openout\exepsfile \EXdir\jobname.expslist\relax

\def\EPSinput#1{%
\IfFileExists{\EXdir\EXprefix#1.eps}%
 {\includegraphics{\EXdir\EXprefix#1.eps}}%
 {\underline{\EXdir\EXprefix#1}}%
}

\def\SideBySideEx#1{%
\trivlist\item[]%
  \leavevmode
   \global\advance\c@side@count by 1
   \label{\thechapter.\theside@count}%
   \ifthenelse{\isodd{\pageref{\thechapter.\theside@count}}}{%
   \makebox[\fullwidth][l]{\@@Display{#1}\hspace{5mm}
    \BVerbatimInput[fontsize=\small]{\EXdir\EXprefix\theeps@counter}}%
      }{%
   \makebox[\fullwidth][r]{\@@Display{#1}\hspace{5mm}
     \BVerbatimInput[fontsize=\small]{\EXdir\EXprefix\theeps@counter}}%
    }%
  \endtrivlist
}
\newsavebox{\@display}
\def\@@Display#1{%
 \savebox{\@display}{%
\begin{minipage}[b]{.5\fullwidth}#1\end{minipage}%
}
 \raisebox{\depth}{\usebox{\@display}}%
}

\newcommand{\BeginExample}{%
  \multiply\topsep by 2
  \global\advance\c@eps@counter\@ne
  \edef\@tempa{%
    \write\@auxout{\string
      \newlabel{ex-\theeps@counter}{{\theeps@counter}{\noexpand\the\c@page}}}}
  \@tempa
  \nobreak
  \VerbatimEnvironment
  \catcode`\<=12
  \begin{VerbatimOut}{\EXdir\EXprefix\theeps@counter.tex}%
}
\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\Write@Ex##1{}\BeginExample
   }
\def\endPSExample{%
  \EndExample
  \SideBySideEx{%
    %\ifgrid\vskip 8pt \else\relax\fi
    \expandafter\pspicture\PictureSize
    \ifgrid\usergrid\else\relax\fi
    \input{\EXdir\theeps@counter}%
    \endpspicture
    %\ifgrid\vskip 8pt \fi
 }%
}

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

\def\PSTInlineExample(#1,#2){%
    \def\PictureSize{(-.2,-.5)(#1,#2)}%
    \def\Write@Ex##1{}\BeginExample
   }
\def\endPSTInlineExample{%
  \EndExample
  \expandafter\pspicture\PictureSize
  \usergrid
  \input{\EXdir\theeps@counter}%
  \endpspicture
 \hspace{16pt}%
 \BVerbatimInput[gobble=0,fontsize=\small]{\EXdir\theeps@counter}%
}
% side by side
\newenvironment{example}{\def\Write@Ex##1{}\BeginExample} %
  {\EndExample\SideBySideEx{\input{\EXdir\theeps@counter}}}

% one above the other
\newenvironment{example*}%
  {\def\Write@Ex##1{}\BeginExample}%
  {%
    \EndExample
    \VerbatimInput[gobble=0,fontsize=\small]{\EXdir\theeps@counter}%
      \begin{center}
        \leavevmode\input{\EXdir\theeps@counter}\par
      \end{center}
}
% one above the other
\newenvironment{numexample*}%
  {\def\Write@Ex##1{}\BeginExample}%
  {%
    \EndExample
    \VerbatimInput[gobble=0,numbers=left,
        fontsize=\small]{\EXdir\theeps@counter}%
      \begin{center}
        \leavevmode\input{\EXdir\theeps@counter}\par
      \end{center}
}

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

% now the ones which write stuff for an EPS file
% for side by side examples, the text width of the separate file
% needs to be half the normal width.
\def\EX@extra{}
\def\HalfWidth{%
 \def\EX@extra{%
 \string\makeatletter
 \string\@tempdima\string\textwidth
 \string\textwidth.5\string\@tempdima
 \string\addtolength{\string\textwidth}{-6mm}
 \string\makeatother}
}
\def\Setup@Escape{%
 \edef\FancyVerbStopString{\string\end{document}}%
 \edef\FancyVerbStartString{\string\begin{document}}%
}
% side by side
\newenvironment{Example}{%
 \HalfWidth
 \BeginExample} %
 {\EndExample
\Setup@Escape
\SideBySideEx{\EPSinput{\theeps@counter}}}

% first verbatimcode, then actual example
\newenvironment{Example*}{\BeginExample}%
  {\EndExample
  \Setup@Escape
  \begin{center}\leavevmode
    \VerbatimInput[gobble=0,fontsize=\small]{\EXdir\EXprefix\theeps@counter}%
 \EPSinput{\theeps@counter}%
 \end{center}%
}

% one above the other, framed
\newenvironment{Example**}%
{\BeginExample}%
{%
\EndExample
\Setup@Escape
    \VerbatimInput[gobble=0,fontsize=\small,
      frame=topline,framerule=1.5pt]{\EXdir\EXprefix\theeps@counter}%
\EPSinput{\theeps@counter}
\EndRuleExample
}

% no verbatim text at all, just to run the thing separately
\newenvironment{Escape}%
{\BeginExample}%
{%
\EndExample
\Setup@Escape
\EPSinput{\theeps@counter}%
}

\def\CurrentPackages{}

\def\FVE@VerbatimOut{\Write@Ex{\string\end{document}}%
\immediate\closeout\FV@OutFile\endgroup\@esphack}

\def\Write@Ex#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
    \Write@Ex{\string\documentclass{\EXclass}}%
    \Write@Ex{\string\nonstopmode}%
    \Write@Ex{\string\usepackage{\CurrentPackages}}%
    \Write@Ex{\string\pagestyle{empty}}%
    \Write@Ex{\EX@extra}%
    \Write@Ex{\string\begin{document}}%
    \let\FV@FontScanPrep\relax
    \FV@Scan}
%
% compatibility with Michel, with moreverb, and verbatim package
%
\def\SixPt{\fontsize{6}{7\p@}\selectfont}
%
% input files verbatim, numbered and unnumbered
%
\def\verbfile#1{\VerbatimInput[fontsize=\small]{#1}}
\let\verbatiminput\verbfile
\def\listinginput#1#2{\VerbatimInput[fontsize=\small,firstnumber=#1,numbers=left]{#2}}
%
% verbatim in various sizes
\DefineVerbatimEnvironment{sssverbatim}{LVerbatim}{fontsize=\SixPt}
\DefineVerbatimEnvironment{ssverbatim}{LVerbatim}{fontsize=\scriptsize}
\DefineVerbatimEnvironment{fverbatim}{LVerbatim}{fontsize=\footnotesize}
\DefineVerbatimEnvironment{sverbatim}{LVerbatim}{fontsize=\small}
\DefineVerbatimEnvironment{Verbatim}{LVerbatim}{fontsize=\normalsize}
\DefineVerbatimEnvironment{verbatim}{LVerbatim}{fontsize=\normalsize}
% numbered
\DefineVerbatimEnvironment{nVerbatim}{LVerbatim}{fontsize=\small,numbers=left}
\endinput