%
% Code examples environment
%
% Reuben Thomas   21/8/99-3/6/01
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{codex}
    [2001/06/03 v1.04 Code examples]

\RequirePackage{array}
\RequirePackage{longtable}

% Environment for code examples
\newlength{\codelength}\setlength{\codelength}{30ex}
\newlength{\commentlength}\setlength{\commentlength}{35ex}
\newlength{\codepairllength}\setlength{\codepairllength}{21ex}
\newlength{\codepairrlength}\setlength{\codepairrlength}{21ex}
\newcommand{\setcodepairlength}[1]%
    {\setlength{\codepairllength}{#1}\setlength{\codepairrlength}{#1}}
\newlength{\commentpairlength}\setlength{\commentpairlength}{21ex}
\newcommand{\codefont}{\small}
\newenvironment{codett}{\begin{ttfamily}\obeyspaces}{\end{ttfamily}}

\newenvironment{codex}%
    % A two-column table: code & comment \\
    {\begingroup\par\medskip\centering\codefont%
    \begin{tabular}%
    {>{\begin{codett}}p{\codelength}<{\end{codett}}p{\commentlength}}}%
    {\end{tabular}\par\medskip\endgroup}
\newenvironment{codex*}%
    % As codex, but allowing page breaks
    {\codefont%
    \begin{longtable}%
    {>{\begin{codett}}p{\codelength}<{\end{codett}}p{\commentlength}}}%
    {\end{longtable}}
\newenvironment{codexpair}%
    % A four-column table, \footnotesize, for side-by-side code
    % comparison: code 1 & comment 1 & code 2 & comment 2 \\
    {\begingroup\medskip\par\centering\begin{footnotesize}%
    \begin{tabular}%
    {>{\begin{codett}}p{\codepairllength}<{\end{codett}}%
    p{\commentpairlength}%
    >{\begin{codett}}p{\codepairrlength}<{\end{codett}}}}%
    {\end{tabular}\end{footnotesize}\par\medskip\endgroup}
\newenvironment{codexpair*}%
    % like codexpair, but allowing page breaks
    {\begin{footnotesize}%
    \begin{longtable}%
    {>{\begin{codett}}p{\codepairllength}<{\end{codett}}%
    p{\commentpairlength}%
    >{\begin{codett}}p{\codepairrlength}<{\end{codett}}}}%
    {\end{longtable}\end{footnotesize}}
