% \iffalse
%%
%% (C) Copyright 1999 Achim Blumensath
%% All rights reserved.
%%
%% Not for general distribution. In its present form it is not allowed
%% to put this package onto CD or an archive without consulting the
%% the authors.
%% 
%<*dtx>
          \ProvidesFile{xtheorem.dtx}
%</dtx>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{xtheorem}
%<driver>\ProvidesFile{xtheorem.drv}
% \fi
%         \ProvidesFile{xtheorem.dtx}
          [1999/12/19 v0.1 theorem styles]
%
% \iffalse
%<*driver>
 \documentclass{ltxdoc}
 \usepackage{textcomp}

% \usepackage{xparse,xlists,xlists-samples}
% \usepackage{ldcdoc}

% next three definitions are big hacks to run the file
% without the above packages
%
\newcommand\NoValue{\texttt{\textbackslash NoValue}}

\newenvironment{TemplateInterfaceDescription}[1]
  {\subsection{The Template Type `#1'}%
   \begingroup\description
   \def\TemplateArgument##1##2{\item[Arg: ##1]##2\par}%
   \def\TemplateSemantics{\enddescription\endgroup
       \subsubsection*{Semantics:}}%
  }
  {\par\bigskip}

\newenvironment{TemplateDescription}[2]
  {\subsection{The Template `#2' (type #1)}%
   \subsubsection*{Attributes:}%
   \begingroup\description
   \def\TemplateKey##1##2##3##4{\item[##1 (##2)]##3%
     \ifx\TemplateKey##4\TemplateKey\else
%         \hskip0ptplus3em\penalty-500\hskip 0pt plus 1filll Default:~##4%
         \hfill\penalty500\hbox{}\hfill Default:~##4%
         \nobreak\hskip-\parfillskip\hskip0pt\relax
     \fi
     \par}%
   \def\TemplateSemantics{\enddescription\endgroup
       \subsubsection*{Semantics \& Comments:}}%
  }
  {\par\bigskip}

 \begin{document}
 \DocInput{xtheorem.dtx}
 \end{document}
%</driver>
% \fi
%
%
% \GetFileInfo{xtheorem.dtx}
%
% \title{The \textsf{xtheorem} package\thanks{This file
%         has version number \fileversion, last
%         revised \filedate.}}
% \author{AB}
% \date{\filedate}
%
%  \maketitle
%
% \tableofcontents
%
% \begin{abstract}
%   This is a conversion of the AMS theorem classes to the template
%   system. Only the interface was changed, the internals still use
%   \LaTeXe\ commands.
% \end{abstract}
%
% \section{Interfaces}
%
% \begin{TemplateInterfaceDescription}{theoremstyle}
%
%   \TemplateArgument{1}
%       {Text of the number of the theorem (e.g., \verb|A.1|), or
%        \NoValue\ to indicate that the theorem is unnumbered.}
%
%   \TemplateArgument{2}
%       {Name of the theorem (e.g. \verb|Theorem|, \verb|Lemma|).}
%
%   \TemplateArgument{3}
%       {Additional note (e.g. \verb|see [12]|), or \NoValue.}
%
%   \TemplateSemantics
%
%   This template starts a new environment, typesets the head of a
%   theorem, and sets fonts for the body of the theorem. The
%   environment is ended by an \verb|\@endtheorem| command.
%
%   If the first argument is given, it is printed as the number part.
%   The second argument contains the name of the theorem, and the
%   third one may contain an additional note.
%
%   For instance, the arguments \verb|2.1|, \verb|Lemma|, and
%   \verb|Rabin~1960| could produce the output
%
%   \medskip
%   \noindent\textbf{Lemma 2.1 }(Rabin 1960)\textbf.
%
%   \medskip\noindent
%   where the following text is set in italic.
%
% \end{TemplateInterfaceDescription}
%
%
% \begin{TemplateDescription}{theoremstyle}{std}
%
%   \TemplateKey{pre-skip}{l}
%      {Skip above the theorem.}
%      {|topsep|}
%   \TemplateKey{post-skip}{l}
%      {Skip below the theorem.}
%      {|topsep|}
%   \TemplateKey{body-style}{f0}
%      {Commands to be executed before the body of the theorem in
%       order to set fonts, etc.}
%      {italic}
%   \TemplateKey{head-style}{f0}
%      {Commands to set the font of the head.}
%      {bold}
%   \TemplateKey{note-style}{f0}
%      {Commands to set the font of the note.}
%      {medium upright font}
%   \TemplateKey{head-punct}{f0}
%      {Text after the head.}
%      {|.|}
%   \TemplateKey{head-format}{f3}
%      {Format of the head.}
%      {Name Number (Note)}
%   \TemplateKey{head-indent}{l}
%      {Indent of the head.}
%      {none}
%   \TemplateKey{head-sep}{l}
%      {Space after the head.}
%      {5pt $\pm$ 1pt}
%   \TemplateKey{post-head-action}{f0}
%      {Command to be executed after the head (e.g., a line break).}
%      {none}
%
%   \TemplateSemantics
%   This are all parameters provided by the AMS package, except `swaphead'
%   which can be simulated by \verb|head-format|.
%
% \end{TemplateDescription}
%
% \paragraph{Problem.}
% The current solution for `head-format' is very clumsy. Perhaps it would
% be better to pass just the name of a template which does the layout
% instead of the actual code.
%
% \medskip
% \DescribeMacro\newtheorem
% The \verb|\newtheorem| command defines a new type of theorem. It takes
% the following arguments:
%
% \medskip
% \verb|\newtheorem|[|*|]$\langle$style$\rangle$\relax
%   $\langle$name$\rangle$[share counter]$\langle$label$\rangle$\relax
%   [count relative]
%
% \medskip\noindent
% and defines an environment named `name'. The arguments have the
% following meanings:
% \begin{description}
% \item[|*|] If present the theorem is unnumbered, otherwise it is
%   numbered.
% \item[style] The name of an instace of the `theoremstyle' template
%   which is used to typeset the theorem.
% \item[name] The name of the new environment.
% \item[share counter] (optional) The label of another defined theorem.
%   If present both kinds of theorem share a common counter.
% \item[label] The name of the theorem which should appear in its head.
% \item[count relative] If present the counter is reset to 1 everything
%   the counter with this name is changed (e.g., |section| or |chapter|).
% \end{description}
%
% For instance, the following definitions create environments named
% |Thm|, |Prop|, |Lem|, |Cor|, |Def|, |Rem|, and |Exam|. The first
% five share a common counter which is relative to the current
% section, the last two are unnumbered.
% \begin{verbatim}
% \newtheorem{plain}{Thm}{Theorem}[section]
% \newtheorem{plain}{Prop}[Thm]{Proposition}
% \newtheorem{plain}{Lem}[Thm]{Lemma}
% \newtheorem{plain}{Cor}[Thm]{Corollary}
% \newtheorem{definition}{Def}[Thm]{Definition}
% \newtheorem*{remark}{Rem}{Remark}
% \newtheorem*{remark}{Exam}{Example}
% \end{verbatim}
%
% \section{Implementation}
%
%    \begin{macrocode}
%<*package>
\RequirePackage{xparse}
\RequirePackage{template}

\IgnoreWhiteSpace
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareTemplateType{theoremstyle}{3}

\newskip\TSpre@skip
\newskip\TSpost@skip
\newdimen\TShead@indent
\newskip\TShead@sep

\DeclareTemplate{theoremstyle}{std}{3}
{
  pre-skip    =l  [\DelayEvaluation{\topsep}]     \TSpre@skip,
  post-skip   =l  [\DelayEvaluation{\topsep}]     \TSpost@skip,
  body-style  =f0 [\itshape]                      \TSbody@style,
  head-style  =f0 [\bfseries]                     \TShead@style,
  note-style  =f0 [\fontseries\mddefault\upshape] \TSnote@style,
  head-punct  =f0 [.]                             \TShead@punct,
  head-format =f3 [\IfNoValueF{#1}{#1\IfNoValueF{#2}{\space}}
                   \IfNoValueF{#2}{\textup{#2}}
                   \IfNoValueF{#3}{\space{\TSnote@style(#3)}}]
                                                  \TShead@format,
  head-indent      =l  [0pt]                      \TShead@indent,
  head-sep         =l  [5pt plus 1pt minus 1pt]   \TShead@sep,
  post-head-action =f0 []                         \TSpost@head@action
}
{
  \DoParameterAssignments
  \ifhmode\unskip\unskip\par\fi
  \normalfont
  \trivlist
  \let\thmheadnl\relax
  \@topsep\TSpre@skip
  \@topsepadd\TSpost@skip
  \IfNoValueF{#1}
    {\refstepcounter{#1}}
  \deferred@thm@head{
    \TShead@style
    \hskip\TShead@indent
    \IfNoValueTF{#1}
      {\TShead@format{#2}{#1}{#3}}
      {\TShead@format{#2}{\csname the#1\endcsname}{#3}}
    \TShead@punct
    \TSpost@head@action
    \hskip\TShead@sep
  }
  \TSbody@style
  \ignorespaces
}
%    \end{macrocode}
% These functions are used by the template above. They are copied
% straight away from amsclass.dtx.
%    \begin{macrocode}
\let\adjust@parskip@nobreak=\@nbitem
%    \end{macrocode}
%
%    \begin{macrocode}
\newtoks\dth@everypar
\dth@everypar={
  \@minipagefalse
  \global\@newlistfalse
  \if@inlabel
    \global\@inlabelfalse
    \begingroup
      \setbox\z@\lastbox
      \ifvoid\z@ \kern-\itemindent \fi
    \endgroup
    \unhbox\@labels
  \fi
  \if@nobreak
    \@nobreakfalse
    \clubpenalty\@M
  \else
    \clubpenalty\@clubpenalty
    \everypar{}
  \fi
}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\deferred@thm@head#1{%
  \if@inlabel \indent \par \fi % eject a section head if one is pending
  \if@nobreak
    \adjust@parskip@nobreak
  \else
    \addpenalty\@beginparpenalty
    \addvspace\@topsep
    \addvspace{-\parskip}%
  \fi
  \global\@inlabeltrue
  \everypar\dth@everypar
  \sbox\@labels{\normalfont#1}%
  \ignorespaces
}
%    \end{macrocode}
% The usual styles `plain', `definition', and `remark'.
%    \begin{macrocode}
\DeclareInstance{theoremstyle}{plain}{std}{}

\DeclareInstance{theoremstyle}{definition}{std}
{
  body-style = \normalfont
}

\DeclareInstance{theoremstyle}{remark}{std}
{
  pre-skip   = \DelayEvaluation{0.5\topsep},
  post-skip  = \DelayEvaluation{0.5\topsep},
  body-style = \normalfont,
  head-style = \itshape
}
%    \end{macrocode}
% The command to end a theorem.
%    \begin{macrocode}
\def\@endtheorem{\endtrivlist\@endpefalse }
%    \end{macrocode}
%
% |newtheorem| just checks all cases and defines the appropriate
% environment.
%    \begin{macrocode}
\DeclareDocumentCommand{\newtheorem}{smmomo}
{
  \expandafter\@ifdefinable\csname #3\endcsname
  {
    \global\expandafter\let\csname end#3\endcsname\@endtheorem
    \IfBooleanTF{#1}
    {
      \expandafter\DeclareDocumentCommand\csname #3\endcsname{o}
        {\UseInstance{theoremstyle}{#2}{\NoValue}{#5}{##1}}
    }
    {
      \IfNoValueTF{#4}
      {
        \IfNoValueTF{#6}
        {
          \newcounter{#3}
        }
        {
          \newcounter{#3}[#6]
          \expandafter\xdef\csname the#3\endcsname{
            \expandafter\noexpand\csname the#6\endcsname
            \@thmcountersep\@thmcounter{#3}}
        }
        \expandafter\DeclareDocumentCommand\csname #3\endcsname{o}
          {\UseInstance{theoremstyle}{#2}{#3}{#5}{##1}}
      }
      {
        \@ifundefined{c@#4}
        {
          \@nocounterr{#4}
        }
        {
          \expandafter\xdef\csname the#3\endcsname{
            \expandafter\noexpand\csname the#4\endcsname}
          \expandafter\DeclareDocumentCommand\csname #3\endcsname{o}
            {\UseInstance{theoremstyle}{#2}{#4}{#5}{##1}}
        }
      }
    }
  }
}
%    \end{macrocode}
%
%    \begin{macrocode}
\endinput
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
%
% \Finale
%
\endinput
