% Document Type: LaTeX
% Date: 20 June 1992
% File: auxdefs.sty
%
% Auxiliary LaTeX definitions which modify internals.
%
%
% Load this file only once!
%
\ifx\@auxdefsloaded\relax\endinput
\else\let\@auxdefsloaded\relax\fi

%
% Define ``*-form'' of \newenvironment and \renewenvironment.
% This form ignores spaces after the resulting \end{...} command.
% Usage:
%    \newenvironment*{NAME}[NARGS]{BEGIN_TEXT}{END_TEXT}
%    \renewenvironment*{NAME}[NARGS]{BEGIN_TEXT}{END_TEXT}
% See also \newoptenvironment (etc.) below.
%
\def\newenvironment{%
   \@ifnextchar *{\@@newenv{\global\@ignoretrue}}{\@@newenv{}*}}

\def\@@newenv#1*#2{%
   \@ifnextchar [{\@newenv{#1}{#2}}{\@newenv{#1}{#2}[0]}}

\long\def\@newenv#1#2[#3]#4#5{%
   \expandafter\newcommand\csname#2\endcsname[#3]{#4}%
   \expandafter\long\expandafter\def\csname end#2\endcsname{#5#1}}

\def\renewenvironment{%
   \@ifnextchar *{\@@renewenv{\global\@ignoretrue}}{\@@renewenv{}*}}

\def\@@renewenv#1*#2{%
   \@ifnextchar [{\@renewenv{#1}{#2}}{\@renewenv{#1}{#2}[0]}}

\long\def\@renewenv#1#2[#3]#4#5{%
   \expandafter\renewcommand\csname#2\endcsname[#3]{#4}%
   \expandafter\long\expandafter\def\csname end#2\endcsname{#5#1}}

%
% Allow user-defined commands and environments with an
% optional first arg.  Usage is
%    \newoptcommand{\COMMAND}{DEFAULT}[NARGS]{TEXT}
%    \renewoptcommand{\COMMAND}{DEFAULT}[NARGS]{TEXT}
%    \newoptenvironment{NAME}{DEFAULT}[NARGS]{B_TEXT}{E_TEXT}
%    \newoptenvironment*{NAME}{DEFAULT}[NARGS]{B_TEXT}{E_TEXT}
%    \renewoptenvironment{NAME}{DEFAULT}[NARGS]{B_TEXT}{E_TEXT}
%    \renewoptenvironment*{NAME}{DEFAULT}[NARGS]{B_TEXT}{E_TEXT}
% For example, after defining
%    \newoptcommand{\glug}{defval}[2]{%
%        First is ``#1'', 2nd is ``#2''.}
% the user can do
%    \glug{hahaha}\\
%    \glug[oof]{bzzzz}
% to get
%    First is ``defval'', 2nd is ``hahaha''.
%    First is ``oof'', 2nd is ``bzzzz''.
%
\def\newoptcommand#1#2{%
   \@ifnextchar [{\@optargdef#1#2}{\@optargdef#1#2[1]}}

\def\renewoptcommand#1#2{%
   \edef\@tempa{\expandafter\@cdr\string#1\@nil}%
   \@ifundefined{\@tempa}{%
      \@latexerr{\string#1\space undefined}\@ehc}{}%
   \@ifnextchar [{\@reoptargdef#1#2}{\@reoptargdef#1#2[1]}}

\long\def\@optargdef#1#2[#3]#4{%
   \@ifdefinable #1{\@reoptargdef#1#2[#3]{#4}}}

\long\def\@reoptargdef#1#2[#3]#4{%
   \@tempcnta#3\relax \@tempcntb \@ne
   \let#1\relax \let\@tempa\relax
   \edef\@tempb{\long\def\csname\string#1\endcsname
      [\@tempa\the\@tempcntb]}%
   \advance\@tempcntb \@ne \advance\@tempcnta \m@ne
   \@whilenum\@tempcnta>0\do{%
      \edef\@tempb{\@tempb\@tempa\the\@tempcntb}%
      \advance\@tempcntb \@ne \advance\@tempcnta \m@ne}%
   \let\@tempa=##\@tempb{#4}%
   \def#1{\@ifnextchar [{\csname\string#1\endcsname}{%
      \csname\string#1\endcsname[#2]}}}

\def\newoptenvironment{%
   \@ifnextchar *{\@@newoptenv{\global\@ignoretrue}}{%
      \@@newoptenv{}*}}

\def\@@newoptenv#1*#2#3{%
   \@ifnextchar [{\@newoptenv{#1}{#2}{#3}}{%
      \@newoptenv{#1}{#2}{#3}[0]}}

\long\def\@newoptenv#1#2#3[#4]#5#6{%
   \expandafter\newoptcommand\csname#2\endcsname{#3}[#4]{#5}%
   \expandafter\long\expandafter\def\csname end#2\endcsname{#6#1}}

\def\renewoptenvironment{%
   \@ifnextchar *{\@@renewoptenv{\global\@ignoretrue}}{%
      \@@renewoptenv{}*}}

\def\@@renewoptenv#1*#2#3{%
   \@ifnextchar [{\@renewoptenv{#1}{#2}{#3}}{%
      \@renewoptenv{#1}{#2}{#3}[0]}}

\long\def\@renewoptenv#1#2#3[#4]#5#6{%
   \expandafter\renewoptcommand\csname#2\endcsname{#3}[#4]{#5}%
   \expandafter\long\expandafter\def\csname end#2\endcsname{#6#1}}

%
% The "optional" environment encloses text that may or may not be
% commented out.  It takes an optional integer argument LEVEL
% (default = 1), specifying how optional its contents are:
%    \begin{optional}[LEVEL] ... \end{optional}
% Usage notes:
%    1. There can be no space between the "\begin" or "\end" and
%       the "{optional}".
%    2. This environment is not designed to handle very large
%       chunks of text; the \include and \input mechanisms are
%       more appropriate for this.
%    3. The '*' character should not directly follow a 
%       "\begin{optional}".  The '[' character can follow it
%       only to introduce the optional arg.
%
% There is a counter "keepoptional" which specifies up to what
% level of optional material should be retained.  Thus, if
% LEVEL > \value{keepoptional}, the contents of that "optional"
% environment will be omitted.  The default value of
% "keepoptional" is 0.
%
\newcounter{keepoptional}
\newcounter{optnestctr}

\newoptenvironment*{optional}{1}[1]{%
   \ifnum#1>\value{keepoptional}\relax
      \setcounter{optnestctr}{0}\@powerup\expandafter\@endopt\fi
   \ignorespaces}{}

\def\@powerup{\catcode`\{=12 \catcode`\}=12 \catcode`\\=12 \relax}
\def\@powerdown{\catcode`\{=1 \catcode`\}=2 \catcode`\\=0 \relax}
\begingroup \catcode`|=0 \catcode`[=1 \catcode`]=2 \@powerup
   |long|gdef|@endopt#1\end{optional}[%
      |@beginopt#1\begin{optional}*]%
   |long|gdef|@beginopt#1\begin{optional}[%
      |@ifstar[%
         |ifnum|value[optnestctr]>0|relax
            |addtocounter[optnestctr][-1]|let|@zzzap=|@endopt
         |else
            |@powerdown|end[optional]|let|@zzzap=|relax|fi
	 |@zzzap]%
      [%else
         |addtocounter[optnestctr][1]|@beginopt]]%
|endgroup

% end

