% \iffalse meta-comment
%
% Copyright (C) 1993 by LaTeX3 project.  All rights reserved.
% For additional copyright information see further down in this file.
% 
% This file is part of the LaTeX2e system (PRELIMINARY TEST RELEASE)
% ------------------------------------------------------------------
% 
%  This system 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.
% 
% 
% IMPORTANT NOTICE:
% 
% For error reports in case of UNCHANGED versions see readme files.
% 
% Please do not request updates from us directly.  Distribution is
% done through Mail-Servers and TeX organizations.
% 
% You are not allowed to change this file.
% 
% You are allowed to distribute this file under the condition that
% it is distributed together with all files mentioned in 00readme.l2e.
% 
% If you receive only some of these files from someone, complain!
% 
% You are NOT ALLOWED to distribute this file alone.  You are NOT
% ALLOWED to take money for the distribution or use of either this
% file or a changed version, except for a nominal charge for copying
% etc.
% \fi

\iffalse
% (C) Copyright Frank Mittelbach, Chris Rowley,
%               Alan Jeffrey and David Carlisle 1993.
% All rights reserved.
\fi
 
\def\fileversion{v0.2r}
\def\filedate{1993/12/19}
 
%
% \title{The main structure of documents}
% \author{Frank Mittelbach\\Chris Rowley\\Alan Jeffrey\\
% David Carlisle}
% \date{\filedate}
%
% \maketitle
%
% \section{Introduction}
%
% This |.tex| file implements the following declarations, which replace
% |\documentstyle| in  \LaTeX2e documents.
%
% Note that old documents containing |\documentstyle| will be run using
% a compatibility option---thus keeping everyone happy, we hope!
%
% The overall idea is that there are two types of `style files':
% `class files' which define elements and provide a default formatting
% for them;  and `packages' which provide extra functionality.  One 
% difference between \LaTeX2e and \LaTeX2.09 is that \LaTeX2e packages 
% may have options. Note that options to classes packages may be
% implemented such that they input files, but these file names are not
% necessarily directly related to the option name. 
%
% \section{User interface}
%
% |\documentclass[|\meta{main-option-list}|]{|^^A
%   \meta{class}|}[|\meta{version}|]|
%
% There must be exactly one such declaration, and it must come first.
% The \meta{main-option-list} is a list of options which can modify the
% formatting of elements which are defined in the \meta{class} file
% as well as in all following |\usepackage| declarations (see below).  
% The \meta{version} is a version number, beginning with a date in the 
% format |YYYY/MM/DD|.  If an older version of the class is found, a 
% warning is issued.
%
% \bigskip
%
% |\documentstyle[|\meta{main-option-list}|]{|^^A
%   \meta{class}|}[|\meta{version}|]|
%
% The |\documentstyle| declaration is kept in order to maintain upward 
% compatibility with \LaTeX2.09 documents.  It is similar to
% |\documentclass|, but it causes all options in
% \meta{main-option-list} that the \meta{class} does not use to be
% passed to |\RequirePackage| after the options have been processed.
% This maintains compatibility with the 2.09 behaviour. Also a flag is
% set to indicate that the document is to be processed in \LaTeX2.09
% compatibility mode.  As far as most packages are concerned, this
% only affects the warnings and errors \LaTeX\ generates. This flag
% does affect the definition of fonr commands, and |\sloppy|.
%
% \bigskip
%
% |\usepackage[|\meta{package-option-list}|]{|^^A
%    \meta{package-list}|}[|\meta{version}|]|
%
% There can be any number of these declarations. All packages in
% \meta{package-list} are called with the same options.
%
% Each \meta{package} file defines new elements (or modifies those defined
% in the \meta{class}), and thus extends the range of documents which
% can be processed.
% The \meta{package-option-list} is a list of options which can modify the
% formatting of elements defined in the \meta{package} file.
% The \meta{version} is a version number, beginning with a date in the 
% format |YYYY/MM/DD|.  If an older version of the package is found, a 
% warning is issued.
%
% Each package is loaded only once.  If the same package is requested 
% more than once, nothing happens, unless the package has been requested 
% with options that were not given the first time it was loaded, in which 
% case an error is produced.
%
% As well as processing the options given in the 
% \meta{package-option-list}, each package processes the 
% \meta{main-option-list}.  This means that options that affect all 
% of the packages can be given globally, rather than repeated for every 
% package.
%
% Note that class files have the extension |.cls|, packages have the 
% extension |.sty|.
%
% \DescribeEnv{filecontents}
% The environment |filecontents| is intended for passing the contents
% of packages, options, or other files along with a document in a
% single file.
% It has one argument, which is the name of the file to create. If that
% file already exists (maybe only in the current directory if the OS
% supports a notion of a `current directory' or `default directory')
% then nothing happens
% (except for an information message) and the body of the environment
% is bypassed. Otherwise, the body of the environment is written
% verbatim to the file name given as the first argument.
%
% The environment is allowed only before |\documentclass| to ensure
% that all packages or options necessary for this particular run are
% present when needed.
%
% \subsection{Option processing}
%
% When the options are processed, they are divided into two types: {\em 
% local\/} and {\em global}:
% \begin{itemize}
%
% \item For a class, the options in the |\documentclass| command are 
%    local.
%
% \item For a package, the options in the |\usepackage| command are 
%    local, and the options in the |\documentclass| command are global. 
%
% \end{itemize}
% The options for |\documentclass| and |\usepackage| 
% are processed in the following way: 
% \begin{enumerate}
%
% \item The local and global options that have been declared (using the 
%   |\DeclareOption| declaration described below) are processed 
%   first.
%
%  In the case of |\ProcessOptions|, they are processed in the order
%  that they were declared in the class or package.
%
%  In the case of |\ProcessOptions*|, they are processed in the order
%  that they appear in the option-lists. First the global options, and
%  then the local ones.
%
% \item Any remaining local options are dealt with using the default option 
%   (declared using the |\DeclareOption*| declaration described 
%   below).  For document classes, this usually does nothing, but
%   records the option on a list of unused options.
%   For packages, this usually produces an error. 
%
% \end{enumerate}
% Finally, when |\begin{document}| is reached, if there are any global
% options which have not been used by either the class or any package,
% the system will produce a warning.
%
%
% \section{Style file interface}
%
% \subsection{Class name and version}
%
% \DescribeMacro\ProvidesClass
% A class can identify itself with the 
% |\ProvidesClass{|\meta{name}|}[|\meta{version}|]| command.  The 
% \meta{version} should begin with a date in the format |YYYY/MM/DD|.
%
% \subsection{Package name and version}
%
% \DescribeMacro\ProvidesPackage
% A package can identify itself with the 
% |\ProvidesPackage{|\meta{name}|}[|\meta{version}|]| command.  The 
% \meta{version} should begin with a date in the format |YYYY/MM/DD|.
%
% \subsection{Requiring other packages}
%
% \DescribeMacro\RequirePackage
% Packages or classes can load other packages using\\
% |\RequirePackage[|\meta{options}|]{|\meta{name}|}[|^^A
%                    \meta{version}|]|.\\
% If the package has already been loaded, then nothing happens unless the
% requested options are not a subset of the options with which it was
% loaded, in which case an error is called.
%
% \DescribeMacro\LoadClass
%  Similar to |\RequirePackage|, but for classes, may not be used in
%  package files.
%
% \DescribeMacro\PassOptionsToPackage
% \DescribeMacro\PassOptionsToClass
% Packages can pass options to other packages using:\\ 
% |\PassOptionsToPackage{|\meta{options}|}{|\meta{package}|}|.\\  This adds 
% the \meta{options} to the options list of any future |\RequirePackage| 
% or |\usepackage| command.  For example:
% \begin{verbatim}
%    \PassOptionsToPackage{foo,bar}{fred}
%    \RequirePackage[baz]{fred}\end{verbatim}
% is the same as:
% \begin{verbatim}
%    \RequirePackage[foo,bar,baz]{fred}\end{verbatim}
%
% \DescribeMacro\@ifpackageloaded
% \DescribeMacro\@ifclassloaded
% To find out if a package has already been loaded, use\\
% |\@ifpackageloaded{|\meta{package}|}{|\meta{true}|}{|\meta{false}|}|.
%
% \DescribeMacro\@ifpackagelater
% \DescribeMacro\@ifclasslater
% To find out if a package has already been loaded with a version more 
% recent than \meta{version}, use
% |\@ifpackagelater{|\meta{package}|}{|\meta{version}|}{|^^A
% \meta{true}|}{|\meta{false}|}|.
%
% \DescribeMacro\@ifpackagewith
% \DescribeMacro\@ifclasswith
% To find out if a package has already been loaded with at least the options 
% \meta{options}, use
% |\@ifpackagewith{|\meta{package}|}{|\meta{options}|}{|^^A
% \meta{true}|}{|\meta{false}|}|.
%
% \subsection{Declaring new options}
%
% Options for classes and packages are built using the same macros.
%
% \DescribeMacro\DeclareOption To define a builtin option, use
% |\DeclareOption{|\meta{name}|}{|\meta{code}|}|.
%
% \DescribeMacro{\DeclareOption*} To define the default action to 
% perform for local options, use |\DeclareOption*{|\meta{code}|}|.
%
% {\em Note\/}: there should be no use of\\
%  |\RequirePackage|, |\DeclareOption|, |\DeclareOption*| or
%   |\ProcessOptions|\\
% inside |\DeclareOption| or |\DeclareOption*|.
%
% Possible uses for |\DeclareOption*| include:
%
% |\DeclareOption*{}|\\
%    Do nothing. Silently accept unknown options. (This suppresses the
%    usual warnings.)
%
% |\DeclareOption*{\@unkownoptionerror}|\\ 
%     Complain about unknown local options. (The initial setting for
%       package files.)
%
% |\DeclareOption*{\PassOptionsToPackage{\CurrentOption}|^^A
%                                     |{|\meta{pkg-name}|}|\\ 
% Handle the the current option by passing it on to the package
% \meta{pkg-name}, which will presumably be loaded via 
% |\RequirePackage| later in the file. This is useful for building
% `extension' packages, that perhaps handle a couple of new options,
% but then pass everything else on to an existing package.
%
% |\DeclareOption*{\InputIfFileExists{xx-\CurrentOption.yyy}{}|^^A
%                  |{\OptionNotUsed}}|\\  
%  Handle the option foo by loading the file |xx-foo.yyy| if it
%  exists, otherwise do nothing, but declare that the option was not used.
%  Actually the |\OptionNotUsed| declaration is only needed if this is
%  being used in class files, but does no harm in package files.
%
%
% \subsection{Safe Input Macros}
% \DescribeMacro{\InputIfFileExists}
%  |\InputIfFileExists{|\meta{file}|}{|\meta{then}|}{|\meta{else}|}|\\
% Inputs \meta{file} if it exists. Immediately before the input,
% \meta{then} is executed. Otherwise \meta{else} is executed.
%
% \DescribeMacro{\IfFileExists}
% As above, but does not input the file.
%
% One thing you might like to put in the \meta{else} clause is
%
% \DescribeMacro{\@missingfileerror}
% This starts an interactive request for a filename, supplying default
% extensions. Just hitting return causes the whole input to be skipped.
% (Perhaps we should let |x| denote quit the current run, as long as no
% one wants to input a file of that name.
%
% \DescribeMacro{\input}
% This has been redefined from the \LaTeX2.09 definition, in terms of
% |\InputIfFileExists| and |\@missingfileerror|.
%
%
% \DescribeMacro{\listfiles} Giving this declaration in the preamble
% causes a list of all files input via the `safe input' commands to be
% listed at the end. Any strings specified in the optional argument to
% |\ProvidesPackage| are listed alongside the file name. So files in
% standard (and other non-standard) distributions can put informative
% strings in this argument.
%
%
%
% \StopEventually{}
%
% \section{Implementation}
%
%    \begin{macrocode}
\makeatletter
%    \end{macrocode}
%
%
% \changes{0.2g}{1993/11/23}
%         {Various macros now moved to latex.tex.}
% \changes{0.2g}{1993/11/23}
%         {Warnings and errors now directly coded.}
% \changes{0.2h}{1993/11/28}
%         {Primitive filenames now terminated by space not \cmd\relax.}
% \changes{0.2h}{1993/11/28}
%         {Directory syntax checing moved to dircheck.dtx}
% \changes{0.2h}{1993/11/28}
%         {Assorted commands now in the kernel removed.}
% \changes{0.2i}{1993/12/03}
%         {\cmd\@onlypreamble: Many commands declared.}
% \changes{0.2i}{1993/12/03}
%         {Removed obsolete \cmd\@documentclass}
% \changes{0.2o}{1993/12/13}
%         {Removed setting \errcontextlines\ (now in latex.tex)}
% \changes{0.2p}{1993/12/15}
%         {Removed extra `.'s from \cmd{\@@warning}s}
%
% \begin{macro}{\if@compatibility}
%    The flag for compatibilty mode.
%    \begin{macrocode}
\newif\if@compatibility
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@compatibility}
%    The hook for compatibilty mode.
% \changes{0.2q}{1993/12/17}
%         {Macro added}
%    \begin{macrocode}
\let\@compatibility\relax
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\@declaredoptions}
%    This list is automatically built by |\DeclareOption|.
%    It is the list of options (separated by commas) declared in
%    the class or package file and it defines the order in which the
%    the corresponding |\ds@|\meta{option} commands are executed.
%    All local \meta{option}s which are not declared will be processed 
%    in the order defined by the optional argument of |\documentclass| or
%    |\usepackage|.
%    \begin{macrocode}
\let\@declaredoptions\@empty
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@classoptionslist}
%    List of options of the main class.
%    \begin{macrocode}
\let\@classoptionslist\relax
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@unusedoptionlist}
%    List of options of the main class that haven't been declared or 
%    loaded as class option files.
%    \begin{macrocode}
\let\@unusedoptionlist\@empty
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\CurrentOption}
%    Name of current package or option.
% \changes{0.2c}{1993/11/17}
%         {Name changed from \cmd\@curroption}
%    \begin{macrocode}
\let\CurrentOption\@empty
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@currname}
%    Name of current package or option.
%    \begin{macrocode}
\let\@currname\@empty
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\@currext}
%    The current file extension.
% \changes{0.2a}{1993/11/14}{Name changed from \cmd{\@currextension}}
%    \begin{macrocode}
\global\let\@currext=\@empty
%    \end{macrocode}
% \end{macro}

% 
% \begin{macro}{\@clsextension}
% \begin{macro}{\@pkgextension}
%    The two possible values of |\@currext|.
%    \begin{macrocode}
\def\@clsextension{cls}
\def\@pkgextension{sty}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@pushfilename}
% \begin{macro}{\@popfilename}
% \begin{macro}{\@currnamestack}
% Commands to push and pop the file name and extension. \\ 
% |#1| current name.      \\
% |#2| current extension. \\
% |#3| current catcode of |@|. \\
% |#4| Rest of the stack.
%    \begin{macrocode}
\def\@pushfilename{%
  \xdef\@currnamestack{%
    {\@currname}%
    {\@currext}%
    {\the\catcode`\@}%
    \@currnamestack}}
\@onlypreamble\@pushfilename
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@popfilename{\expandafter\@p@pfilename\@currnamestack\@nil}
\@onlypreamble\@popfilename
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@p@pfilename#1#2#3#4\@nil{%
  \gdef\@currname{#1}%
  \gdef\@currext{#2}%
  \catcode`\@#3\relax
  \gdef\@currnamestack{#4}}
%    \end{macrocode}
%
%    \begin{macrocode}
\gdef\@currnamestack{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@ptionlist}
%    Returns the option list of the file.
%    \begin{macrocode}
\def\@ptionlist#1{%
  \@ifundefined{opt@#1}\@empty{\csname opt@#1\endcsname}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@ifpackageloaded}
% \begin{macro}{\@ifclassloaded}
%   |\@ifpackageloaded{|\meta{name}|}|
%  Checks to see whether a file has been loaded.
%    \begin{macrocode}
\def\@ifpackageloaded{\@ifl@aded\@pkgetension}
\def\@ifclassloaded{\@ifl@aded\@clsextension}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@ifl@aded#1#2{%
  \expandafter\ifx\csname ver@#2.#1\endcsname\relax
    \expandafter\@secondoftwo
  \else
    \expandafter\@firstoftwo
  \fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@ifpackagelater}
% \begin{macro}{\@ifclasslater}
% |\@ifpackagelater{|\meta{name}|}{YYYY/MM/DD}|
% Checks that the package loaded is more recent than the given date.
%    \begin{macrocode}
\def\@ifpackagelater{\@ifl@ter\@pkgextension}
\def\@ifclasslater{\@ifl@ter\@clsextension}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@ifl@ter#1#2{%
  \expandafter\@ifl@t@r
    \csname ver@#2.#1\endcsname}
%    \end{macrocode}
%
% This internal macro is also used in |\NeedsTeXFormat|.
% \changes{0.2f}{1993/11/22}
%         {Added //00 so parsing never produces a runaway argument.}
%    \begin{macrocode}
\def\@ifl@t@r#1#2{%
  \ifnum\expandafter\@parse@version#1//00\@nil<%
        \expandafter\@parse@version#2//00\@nil
    \expandafter\@secondoftwo
  \else
    \expandafter\@firstoftwo
  \fi}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@parse@version#1/#2/#3#4#5\@nil{#1#2#3#4 }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@ifpackagewith}
% \begin{macro}{\@ifclasswith}
% |\@ifpackagewith{|\meta{name}|}{|\meta{option-list}|}|
% Checks that \meta{option-list} is a subset of the options
% \textbf{with} which \meta{name} was loaded.
%    \begin{macrocode}
\def\@ifpackagewith{\@if@ptions\@pkgextension}
\def\@ifclasswith{\@if@ptions\@clsextension}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@if@ptions#1#2{%
  \@expandtwoargs\@if@pti@ns{\@ptionlist{#2.#1}}}
%    \end{macrocode}
%
% Probably shouldnt use |\CurrentOption| here\ldots (changed to |\@tempb|.)
%    \begin{macrocode}
\def\@if@pti@ns#1#2{%
  \let\@tempa\@firstoftwo
  \@for\@tempb:=#2\do{%
    \expandafter\in@\expandafter{\@tempb}{#1}%
    \ifin@\else\let\@tempa\@secondoftwo\fi}%
  \@tempa}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ProvidesPackage}
%    Checks that the current filename is correct, and defines 
%    |\ver@filename|.
%    \begin{macrocode}
\def\ProvidesPackage#1{%
  \edef\@tempa{#1}%
  \ifx\@tempa\@currname\else
    \@@warning{You have requested \@cls@pkg\space`\@currname',^^J%
      \@spaces but the \@cls@pkg\space provides `#1'}%
  \fi
  \@ifnextchar[{\@pr@videpackage}{}}%]
\@onlypreamble\ProvidesPackage
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@pr@videpackage[#1]{%
  \expandafter\xdef\csname ver@\@currname.\@currext\endcsname{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ProvidesClass}
%    Like |\ProvidesPackage|, but for classes.
%    \begin{macrocode}
\let\ProvidesClass\ProvidesPackage
\@onlypreamble\ProvidesClass
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ProvidesFile}
%    Like |\ProvidesPackage|, but for arbitrary files.
% \changes{0.2l}{1993/12/07}
%         {Macro added}
%    \begin{macrocode}
\def\ProvidesFile#1{%
  \@ifnextchar[{\@providesfile{#1}}{\@providesfile{#1}[]}}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@providesfile#1[#2]{%
  \expandafter\xdef\csname ver@#1\endcsname{#2}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\PassOptionsToPackage}
% \begin{macro}{\PassOptionsToClass}
%    If the package has been loaded, we check that it was first loaded with 
%    the options.  Otherwise we add the option list to that of the package.
%    \begin{macrocode}
\def\@pass@ptions#1#2#3{%
  \expandafter\xdef\csname opt@#3.#1\endcsname{%
    \@ifundefined{opt@#3.#1}\@empty
      {\csname opt@#3.#1\endcsname,}%
    \zap@space#2 \@empty}}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\PassOptionsToPackage{\@pass@ptions\@pkgextension}
\def\PassOptionsToClass{\@pass@ptions\@clsextension}
\@onlypreamble\PassOptionsToPackage
\@onlypreamble\PassOptionsToClass
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\DeclareOption}
% \begin{macro}{\DeclareOption*}
%    Adds an option as a |\ds@| command, or the default |\default@ds| 
%    command.
% \changes{0.2c}{1993/11/17}
%         {Error checking added}
%    \begin{macrocode}
\def\DeclareOption{%
  \let\@fileswith@pti@ns\@badrequireerror
  \@ifstar{\@defdefault@ds}\@declareoption}
\def\@declareoption#1#2{%
   \xdef\@declaredoptions{\@declaredoptions,#1}%
   \@namedef{ds@#1}{#2}}
\def\@defdefault@ds#1{\def\default@ds{#1}}
\@onlypreamble\DeclareOption
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\OptionNotUsed}
% If we are in a class file, add |\CurrentOption| to the list of
% unused options. Otherwise, in a package file do nothing.
%    \begin{macrocode}
\def\OptionNotUsed{%
  \ifx\@currext\@clsextension
    \xdef\@unusedoptionlist{%
      \ifx\@unusedoptionlist\@empty\else\@unusedoptionlist,\fi
      \CurrentOption}%
  \fi}
\@onlypreamble\OptionNotUsed
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\default@ds}
% The default default option code. 
% Set by |\@onefilewithoptions| to be |\OptionNotUsed| for classes, or 
% |\@unknownoptionerror| for packages. This may be reset in either
% case with |\DeclareOption*|.
%    \begin{macrocode}
% \let\default@ds\OptionNotUsed
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ProcessOptions}
% \begin{macro}{\ProcessOptions*}
% |\ProcessOptions| calls |\ds@option| for each known package option,
% then calls |\default@ds| for each option on the local options list. 
% Finally resets all the declared options to |\relax|. The empty option
% does nothing, this has to be reset on the off chance it's set to
% |\relax| if an empty element gets into the |\@declaredoptions| list.
%
% The star form is similar but executes options given in the order
% specified in the document, not the order they are declared in the
% file. In the case of packages, global options are executed before
% local ones.
% \changes{0.2a}{1993/11/14}
%         {Stop adding the global option list inside class files.}
% \changes{0.2a}{1993/11/14}
%         {Optimise `empty option' code.}
% \changes{0.2b}{1993/11/15}
%         {Star form added.}
% \changes{0.2c}{1993/11/17}
%         {restoring \cmd{\@fileswith@pti@ns} added.}
%    \begin{macrocode}
\def\ProcessOptions{%
  \let\ds@\@empty
  \edef\@curroptions{\@ptionlist{\@currname.\@currext}}%
  \@ifstar\@xprocess@ptions\@process@ptions}
\@onlypreamble\@process@ptions
\@onlypreamble\@xprocess@ptions
\@onlypreamble\@process@pti@ns
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@process@ptions{%
  \@for\CurrentOption:=\@declaredoptions\do{%
    \ifx\CurrentOption\@empty\else
      \@expandtwoargs\in@\CurrentOption{%
         \ifx\@currext\@clsextension\else\@classoptionslist,\fi
         \@curroptions}%
      \ifin@
        \@use@ption
        \expandafter\let\csname ds@\CurrentOption\endcsname\@empty
      \fi
    \fi}%
  \@process@pti@ns}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@xprocess@ptions{%
  \ifx\@currext\@clsextension\else
    \@for\CurrentOption:=\@classoptionslist\do{%
      \ifx\CurrentOption\@empty\else
        \@expandtwoargs\in@\CurrentOption\@declaredoptions
        \ifin@
          \@use@ption
          \expandafter\let\csname ds@\CurrentOption\endcsname\@empty
        \fi
      \fi}%
  \fi
  \@process@pti@ns}
%    \end{macrocode}
%
% The common part of |\ProcessOptions| and |\ProcessOptions*|.
%    \begin{macrocode}
\def\@process@pti@ns{%
  \@for\CurrentOption:=\@curroptions\do{%
    \@ifundefined{ds@\CurrentOption}%
      {\@use@ption
       \default@ds}%
%    \end{macrocode}
% There should not be any non-empty definition of |\CurrentOption| at
% this point, as all the declared options were executed earlier. This is
% for compatibility with 2.09 styles which use |\def\ds@|\ldots\
% directly, and so have options which do not appear in
% |\@declaredoptions|. 
%    \begin{macrocode}
      \@use@ption}%
%    \end{macrocode}
% Clear all the definitions for option code. First set all the declared
% options to |\relax|, then reset the `default' and `empty' options. and
% the lst of declared options.
%    \begin{macrocode}
  \@for\CurrentOption:=\@declaredoptions\do{%
    \expandafter\let\csname ds@\CurrentOption\endcsname\relax}%
%    \end{macrocode}
%    \begin{macrocode}
  \let\@fileswith@pti@ns\@@fileswith@pti@ns}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@options}
% |\@options| is a synonym for |\ProcessOptions*| for upward 
% compatibility with \LaTeX2.09 style files.
%    \begin{macrocode}
\def\@options{\ProcessOptions*}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@use@ption}
% Execute the code for the current option.
% \changes{0.2g}{1993/11/23}
%         {Name changed from \cmd{\@executeoption}}
%    \begin{macrocode}
\def\@use@ption{%
  \csname ds@\CurrentOption\endcsname
  \@expandtwoargs\@removeelement\CurrentOption
  \@unusedoptionlist\@unusedoptionlist}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ExecuteOptions}
% |\ExecuteOptions{|\meta{option-list}|}| executes the code declared
% for each option.
% \changes{0.2d}{1993/11/18}
%         {Use \cmd{\CurrentOption} not\cmd{\@tempa}}
% \changes{0.2k}{1993/12/06}
%         {Preserve \cmd\CurrentOption}
%    \begin{macrocode}
\def\ExecuteOptions#1{%
  \def\@tempa##1\@nil{%
    \@for\CurrentOption:=#1\do{\csname ds@\CurrentOption\endcsname}%
    \edef\CurrentOption{##1}}%
  \expandafter\@tempa\CurrentOption\@nil}
%    \end{macrocode}
% \end{macro}
%
%    The top-level commands, which just set some parameters then call 
%    |\@fileswithoptions|.
% \begin{macro}{\documentclass}
% The main new-style class declaration.
%    \begin{macrocode}
\def\documentclass{%
  \let\documentclass\@twoclasseserror
  \let\usepackage\RequirePackage
  \@fileswithoptions\@clsextension}
\@onlypreamble\documentclass
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\documentstyle}
% 2.09 style class `style' declaration.
% \changes{0.2a}{1993/11/14}
%         {Added \cmd\RequirePackage\ \cmd\@unusedoptionlist\ stuff.}
% \changes{0.2b}{1993/11/15}
%         {Modified to match \cmd{\ProcessOption*}}
% \changes{0.2d}{1993/11/18}
%         {Modified \cmd\RequirePackage\ stuff.}
% \changes{0.2n}{1993/12/09}
%         {input 209 compatibility file.}
% \changes{0.2o}{1993/12/13}
%         {compatibility file now latex209.sty.}
% \changes{0.2q}{1993/12/17}
%         {Match Alan's new code.}
%    \begin{macrocode}
\def\documentstyle{%
  \makeatletter\input{latex209.cmp}\makeatother
  \documentclass}
\@onlypreamble\documentstyle
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\RequirePackage}
% Load package if not already loaded.
%    \begin{macrocode}
\def\RequirePackage{%
  \@fileswithoptions\@pkgextension}
\@onlypreamble\RequirePackage
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\LoadClass}
% Load class.
%    \begin{macrocode}
\def\LoadClass{%
  \ifx\@currext\@pkgextension
     \@latexerr
      {\noexpand\LoadClass in package file.}%
      {You may only use \noexpand\LoadClass in a class file}%
  \fi
  \@fileswithoptions\@clsextension}
\@onlypreamble\LoadClass
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\usepackage}
%    To begin with, |\usepackage| produces an error.  This is reset by 
%    |\documentclass|.
% \changes{0.2o}{1993/12/13}
%         {Fixed error handling}
%    \begin{macrocode}
\def\usepackage{%
  \@latexerr
    {\noexpand \usepackage before \noexpand\documentclass}
    {\noexpand \usepackage may only appear in the document preamble.}}
\@onlypreamble\usepackage
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NeedsTeXFormat}
% Check that the document is running on the correct system.
% \changes{0.2a}{1993/11/14}
%         {made more robust for alternative syntax for other formats.}
% \changes{0.2c}{1993/11/17}
%         {Name changed from \cmd\NeedsFormat}
% \changes{0.2d}{1993/11/18}
%         {\cmd\fmtname\ \cmd\fmtversion\ not \cmd{\@}\ldots}
%    \begin{macrocode}
\def\NeedsTeXFormat#1{%
  \def\@tempa{#1}%
  \ifx\@tempa\fmtname
    \expandafter\@needsformat
  \else
     \@latexerr{This file needs format^^J%
     \@spaces \@tempa\space but this is \fmtname}\@ehd
    \expandafter\@@end
  \fi}
\@onlypreamble\NeedsTeXFormat
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@needsformat{%
  \@ifnextchar[%]
    \@needsf@rmat
    {}}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\@needsf@rmat[#1]{%
    \@ifl@t@r\fmtversion{#1}{}%
    {\@@warning{You have requested version^^J%
     \@spaces `#1' of \fmtname,^^J%
     \@spaces but only version `\fmtversion'^^J%
     \@spaces is available}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\zap@space}
% |\zap@space foo|\meta{space}|\@empty| removes all spaces from |foo|
% that are not protected by |{ }| groups.
%    \begin{macrocode} 
\def\zap@space#1 #2{%
  #1%
  \ifx#2\@empty\else\expandafter\zap@space\fi
  #2}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@fileswithoptions}
% The common part of |\documentclass| and |\usepackage|.
%    \begin{macrocode} 
\def\@fileswithoptions#1{%
  \@ifnextchar[%]
    {\@fileswith@ptions#1}%
    {\@fileswith@ptions#1[]}}
\@onlypreamble\@fileswithoptions
%    \end{macrocode}
%
% \changes{0.2f}{1993/11/22}
%         {Made the default [] not [\cmd{\@unknownversion}]}
%    \begin{macrocode}
\def\@fileswith@ptions#1[#2]#3{%
  \@ifnextchar[%]
  {\@fileswith@pti@ns#1[#2]#3}%
  {\@fileswith@pti@ns#1[#2]#3[]}}
\@onlypreamble\@fileswith@ptions
%    \end{macrocode}
% Then we do some work.
%
% First of all, we define the global variables.
% Then we look to see if the file has already been loaded.  
% If it has, we check that it was first loaded with at least the current 
% options.
% If it has not, we add the current options to the package options,
% set the default version to be |0000/00/00|, and load the file if we
% can find it. 
% Then we check the version number.
%
% Finally, we restore the old file name, reset the default option,
% and we set the catcode of |@|.
%
% For classes, we can immediately process the file. For other types,
% |#2| could be a comma separated list, so loop through, processing
% each one separately.
% \changes{0.2q}{1993/12/17}
%         {Add \cmd{compatibility} hook}
%    \begin{macrocode}
\def\@fileswith@pti@ns#1[#2]#3[#4]{%
  \ifx#1\@clsextension
    \@ifundefined{@classoptionslist}%
      {\xdef\@classoptionslist{\zap@space#2 \@empty}}%
      {}%
    \@onefilewithoptions#3[#2][#4]#1%
    \@compatibility
  \else
    \edef\@tempa{\zap@space#3 \@empty}%
    \@for\@currpkg:=\@tempa\do{%
      \expandafter\@onefilewithoptions\@currpkg[#2][#4]#1}%
  \fi}
\@onlypreamble\@fileswith@pti@ns
%    \end{macrocode}
%
% Have the main argument as |#1|, so we only need one |\expandafter|
% above. 
% \changes{0.2a}{1993/11/14}
%         {Moved reseting of \cmd{\default@ds}, \cmd{\ds@} and 
%         \cmd{\@declaredoptions} here, from the end of 
%         \cmd\ProcessOptions.}
% \changes{0.2f}{1993/11/22}
%         {Made the initial version [] not [\cmd{\@unknownversion}]}
% \changes{0.2m}{1993/12/07}
%         {Reset \cmd\CurrentOption}
%    \begin{macrocode}
\def\@onefilewithoptions#1[#2][#3]#4{%
  \@pushfilename
  \xdef\@currname{#1}%
  \global\let\@currext#4%
  \expandafter\let\csname\@currname.\@currext-h@@k\endcsname\@empty
  \let\CurrentOption\@empty
  \@reset@ptions
  \makeatletter
  \@ifl@aded\@currext{#1}%
    {\@if@ptions\@currext{#1}{#2}{}
      {\@latexerr
          {Option clash for \@cls@pkg\space #1}%
          {The package #1 has already been loaded with the options:^^J%
           [\@ptionlist{#1.\@currext}]^^J%
           There has now been an attempt to load it with the options^^J%
           [#2]^^J%
           Adding the line:^^J%
           \string\usepackage[\@ptionlist{#1.\@currext},#2]{#1}^^J%
           to your document may fix this.^^J%
           Try typing \space <return> \space to proceed.}}}%
    {\@pass@ptions\@currext{#2}{#1}%
     \@pr@videpackage[]%
     \InputIfFileExists
       {\@currname.\@currext}%
       {}%
       {\@missingfileerror\@currname\@currext}}%
  \@ifl@ter\@currext{#1}{#3}{}
    {\@warning{You have requested version^^J%
       \@spaces `#3' of \@cls@pkg\space #1,^^J%
       \@spaces but only version 
         \csname ver@#1.\@currext\endcsname^^J%
       \@spaces is available.^^J}}%
  \csname\@currname.\@currext-h@@k\endcsname
  \expandafter\let\csname\@currname.\@currext-h@@k\endcsname\@undefined
%    \end{macrocode}
% \changes{0.2c}{1993/11/17}
%         {Added trap for two \cmd{\LoadClass} commands.}
%    \begin{macrocode}
  \ifx\@currext\@clsextension\let\LoadClass\@twoloadclasserror\fi
  \@popfilename
  \@reset@ptions}
\@onlypreamble\@onefilewithoptions
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@fileswith@pti@ns}
% Save the definition (for error checking).
% \changes{0.2c}{1993/11/17}
%         {Macro added}
%    \begin{macrocode}
\let\@@fileswith@pti@ns\@fileswith@pti@ns
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@reset@ptions}
% Reset the default option, and clear lists of declared options.
% \changes{0.2a}{1993/11/14}{macro added}
%    \begin{macrocode}
\def\@reset@ptions{%
  \global\ifx\@currext\@clsextension
    \let\default@ds\OptionNotUsed
   \else
    \let\default@ds\@unknownoptionerror
  \fi
  \global\let\ds@\@empty
  \global\let\@declaredoptions\@empty}
\@onlypreamble\@reset@ptions
%    \end{macrocode}
% \end{macro}
%
% \subsection{Hooks}
%
% Allow code do be saved to be executed at specific later times.
%
% Save things in macros, I considered using toks registers, (and
% |\addto@hook| from the NFSS code, that would require stacking the
% contents in the case of required packages, so just generate a new
% macro for each package.
% \begin{macro}{\@begindocumenthook}
% \begin{macro}{\@enddocumenthook}
% Stuff to appear at the begining or end of the document.
%    \begin{macrocode}
\let\@begindocumenthook\@empty
\let\@enddocumenthook\@empty
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\g@addto@macro}
% Globally add to the end of a macro.
% \changes{0.2a}{1993/11/14}{Made global}
%    \begin{macrocode}
\def\g@addto@macro#1#2{%
  \expandafter\gdef\expandafter#1\expandafter{#1#2}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\AtEndOfPackage}
% \begin{macro}{\AtEndOfClass}
% \begin{macro}{\AtBeginDocument}
% \begin{macro}{\AtEndDocument}
% The access functions.
% \changes{0.2a}{1993/11/14}
%         {Included extension in the generated macro name for package
%         and class hooks.}
%    \begin{macrocode}
\def\AtEndOfPackage{%
  \expandafter\g@addto@macro\csname\@currname.\@currext-h@@k\endcsname}
\let\AtEndOfClass\AtEndOfPackage
%    \end{macrocode}
%
%    \begin{macrocode}
\def\AtBeginDocument{\g@addto@macro\@begindocumenthook}
\def\AtEndDocument{\g@addto@macro\@enddocumenthook}
\@onlypreamble\AtBeginDocument
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\@cls@pkg}
%    The current file type.
% \changes{0.2i}{1993/12/03}
%         {Name changed to avoid clash with output routine.}
%    \begin{macrocode}
\def\@cls@pkg{%
  \ifx\@currext\@clsextension
    document class%
  \else
    package%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@unknownoptionerror}
% Bad option.
%    \begin{macrocode}
\def\@unknownoptionerror{%
  \@latexerr
    {Unknown option `\CurrentOption' for \@cls@pkg\space`\@currname'}%
    {The option `\CurrentOption' was not declared in 
     \@cls@pkg\space`\@currname'.^^J%
     Try typing \space <return>
     \space to proceed.}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@badrequireerror}
% |\RequirePackage| or |\LoadClass| occurs in the options section.
% \changes{0.2c}{1993/11/17}
%         {Macro added}
%    \begin{macrocode}
\def\@badrequireerror#1[#2]#3[#4]{%
  \@latexerr
    {\noexpand\RequirePackage or \noexpand\LoadClass in Options Section}%
    {\@cls@pkg\space \@currname\space is defective.^^J%
     It attempts to load #3 in
    the options section.}}
\@onlypreamble\@badrequireerror
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@twoloadclasserror}
% Two |\LoadClass| in a class.
% \changes{0.2c}{1993/11/17}
%         {Macro added}
%    \begin{macrocode}
\def\@twoloadclasserror{%
  \@latexerr
    {Two \noexpand\LoadClass commands.}%
    {You may only use one \noexpand\LoadClass in a class file}}
\@onlypreamble\@twoloadclasserror
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@twoclasseserror}
% Two |\documentclass| or |\documentstyle|.
% \changes{0.2h}{1993/11/28}
%         {Macro added}
%    \begin{macrocode}
\def\@twoclasseserror#1#{%
  \@latexerr
    {Two \noexpand\documentclass or \noexpand\documentstyle commands.}%
    {The document may only declare one class.}\@gobble}
\@onlypreamble\@twoclasseserror
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@missingfileerror}
% This `error' command avoids \TeX's primitive missing file loop.
%
% Missing file error. Prompt for a new filename, offering a default
% extension. 
% \changes{0.2e}{1993/11/21}
%         {Stop infinite looping on \cmd{\@er@ext}}
% \changes{0.2h}{1993/11/28}
%         {Use filename parser from dircheck}
%    \begin{macrocode}
\def\@missingfileerror#1#2{%
     \typeout{File `#1.#2' not found.^^J%
      New name (default extension: #2) or <RETURN>?}%
     \message{Enter file name: }%
      {\endlinechar\m@ne
       \global\read\m@ne to\@gtempa}%
    \ifx\@gtempa\@empty
    \else
      \filename@parse\@gtempa
      \edef\filename@ext{\ifx\filename@ext\relax#2\else\filename@ext\fi}%
     \edef\@tempa{%
       \noexpand\InputIfFileExists
         {\filename@area\filename@base.\filename@ext}%
         {}%
         {\noexpand\@missingfileerror
            {\filename@area\filename@base}{\filename@ext}}}%
      \@tempa
    \fi}
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Providing shipment}
%
% \begin{macro}{\two@digits}
% Prefix a number less than 10 with `0'.
%    \begin{macrocode}
\def\two@digits#1{\ifnum#1<10 0\fi\number#1}
%    \end{macrocode}
% \end{macro}
% 
%  \begin{macro}{\filecontents}
%  \begin{macro}{\endfilecontents}
%    This environment implements inline files.
% \changes{0.2h}{1993/11/28}
%         {Don't globally allocate a write stream (always use 15)}
% \changes{0.2r}{1993/12/19}{Different message when ignoring a file}
%    \begin{macrocode}
\begingroup%
\catcode`\^^M\active%
\gdef\filecontents#1{%
  \openin\@inputcheck#1 %
  \ifeof\@inputcheck%
    \typeout{^^JWriting \@currdir#1^^J}%
    \chardef\verbatim@out15 %
    \ch@ck7\verbatim@out\write%
  \else%
    \typeout{^^JFile `#1' already exists on the system.}%
    \typeout{^^JNot generating it from this source.^^J}%
    \def\immediate##1\relax{}%
  \fi%
  \immediate\openout\verbatim@out=#1\relax%
  \immediate\write\verbatim@out{%
  \@percentchar\@percentchar\space LaTeX2e file `#1'^^J%
  \@percentchar\@percentchar\space  generated by the %
         `filecontents' environment^^J%
  \@percentchar\@percentchar\space from source `\jobname' on %
         \number\year/\two@digits\month/\two@digits\day.^^J%
      \@percentchar\@percentchar}\relax%
  \let\do\@makeother\dospecials%
  \@vwrend%
  \catcode`\^^M\active%
  \def^^M##1^^M%
    {\def\@tempb{##1}%
     \ifx\@tempb\@tempa\expandafter\end\expandafter{\@currenvir}%
     \else\immediate\write\verbatim@out{##1}\relax\expandafter^^M\fi}}%
\endgroup%
\begingroup
\catcode`/=0
\catcode`<=\catcode`{
\catcode`>=\catcode`}
\catcode`|=\catcode`\%
/catcode`/\=12
/catcode`/{=12
/catcode`/}=12
/catcode`/%=12
/gdef/@percentchar<%>
/gdef/@vwrend</edef/@tempa<\end{/@currenvir}>>
/gdef/endfilecontents<|
  /immediate/write/verbatim@out<\endinput>/relax|
  /immediate/closeout/verbatim@out/relax>
/endgroup
\@onlypreamble\filecontents
\@onlypreamble\endfilecontents
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsection{Listing files}
%
% \begin{macro}{\@filelist}
% A list of files input so far.
%    \begin{macrocode}
\let\@filelist\@gobble
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\listfiles}
% A preamble command to cause |\end{document}| to list files input
% from the main file.
% \changes{0.2f}{1993/11/22}
%         {Removed checking for \cmd{\@unknownversion}} 
%    \begin{macrocode}
\def\listfiles{%
  \AtEndDocument{%
     \typeout{^^J^^J***File List***}%
     \@for\@tempa:=\@filelist\do{%
       \expandafter\let\expandafter\@tempb\csname ver@\@tempa\endcsname
       \typeout{\@tempa\ifx\@tempb\relax\else\@spaces\@tempb\fi}}      
     \typeout{***********^^J}}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@unknownversion}
% \changes{0.2f}{1993/11/22}
%         {Macro removed}
% The unknown version string as a macro for testing with |\ifx|.
% \end{macro}
%
