% \begin{meta-comment}
%
% $Id: mdwthm.dtx,v 1.2 2002/02/24 12:35:55 mdw Exp $
%
% Standard theorem typesetting
%
% (c) 2001 Mark Wooding
%
%----- Revision history -----------------------------------------------------
%
% $Log: mdwthm.dtx,v $
% Revision 1.2  2002/02/24 12:35:55  mdw
% Fix handling of \chapter, in case some idiot has said `\csname
% chapter\endcsname'.
%
% Revision 1.1  2002/02/03 20:49:03  mdw
% Checkin for new build system.
%
%
% \end{meta-comment}
%
% \begin{meta-comment} <general public licence>
%%
%% mdwthm package -- standard theorem stuff
%% Copyright (c) 2001 Mark Wooding
%%
%% This program is free software; you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published by
%% the Free Software Foundation; either version 2 of the License, or
%% (at your option) any later version.
%% 
%% This program 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.  See the
%% GNU General Public License for more details.
%% 
%% You should have received a copy of the GNU General Public License
%% along with this program; if not, write to the Free Software Foundation,
%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
% \end{meta-comment}
%
% \begin{meta-comment} <Package preambles>
%<+package>\NeedsTeXFormat{LaTeX2e}
%<+package>\ProvidesPackage{mdwthm}
%<+package>                [2001/09/16 1.0 theorem typesetting]
%<+thmstyle>\ProvidesFile{mdwmargin.thm}
%<+thmstyle>             [2001/09/16 1.0 numbers-in-the-margin theorem style]
% \end{meta-comment}
%
% \CheckSum{145}
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
%
% \begin{meta-comment}
%
%<*driver>
\input{mdwtools}
\describespackage{mdwthm}
\mdwdoc
%</driver>
%
% \end{meta-comment}
%
%^^A-------------------------------------------------------------------------
% \section{User guide}
%
% \subsection{Environments provided}
%
% \DescribeEnv{theorem}
% \DescribeEnv{lemma}
% \DescribeEnv{proposition}
% \DescribeEnv{corollary}
% \DescribeEnv{definition}
% \DescribeEnv{example}
% \DescribeEnv{fact}
% \DescribeEnv{remark}
% \DescribeEnv{note}
% The package defines a collection of theorem environments, declared in the
% usual way.  All of them except \env{claim} share the same counter.  If the
% document class has a |\chapter| command, then they are numbered within the
% chapter; otherwise there is a single numbering system throughout the
% document.
%
% Every environment has a $*$-version, which doesn't print a number.  This
% might be useful every now and then.
%
% The environments \env{theorem}, \env{lemma}, \env{proposition},
% \env{claim}and \env{corollary} are set using the `plain' style, with the
% body text in \textit{italics}.  The environments \env{definition} and
% \env{example} are set in the `definition' style, with body text in roman.
% The remaining environments, \env{fact}, \env{remark}, and \env{note} are
% set in the `remark' style.
%
% \DescribeEnv{claim}
% \DescribeEnv{proof}
% The \env{claim} environment has its own counter, which is reset at the
% beginning of the \env{proof} environment.
%
% \subsection{The \package{mdwmargin} style}
%
% This package also provides a pretty `number-hanging-in-the-margin' theorem
% style collection, available under the name \env{mdwmargin}.
%
% \implementation
%
%
%^^A-------------------------------------------------------------------------
% \section{Implementation of the environments}
%
%    \begin{macrocode}
%<*package>
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{amsthm}}
\ProcessOptions\relax
\RequirePackage{amsthm}
%    \end{macrocode}
%
% \subsection{The simple bits}
%
% \begin{environment}{theorem}
%
% Do the right thing with the numbering rules.
%
%    \begin{macrocode}
\toks@{\newtheorem{theorem}{Theorem}}
\@tempswatrue%
\ifx\if@article\iftrue\@tempswafalse\fi
\ifx\chapter\@@undefined\@tempswafalse\fi
\ifx\chapter\relax\@tempswafalse\fi
\if@tempswa\toks@\expandafter{\the\toks@[chapter]}\fi
\the\toks@
\newtheorem*{theorem*}{Theorem}
%    \end{macrocode}
%
% \end{environment}
%
% \begin{environment}{lemma}
% \begin{environment}{proposition}
% \begin{environment}{corollary}
% \begin{environment}{claim}
% \begin{environment}{definition}
% \begin{environment}{example}
% \begin{environment}{fact}
% \begin{environment}{remark}
%
% Most of this is very easy.
%
%    \begin{macrocode}
\theoremstyle{plain}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem*{lemma*}{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem*{proposition*}{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem*{corollary*}{Corollary}
\newtheorem{fact}[theorem]{Fact}
\newtheorem*{fact*}{Fact}
\newtheorem{claim}{Claim}
\newtheorem*{claim*}{Claim}
%    \end{macrocode}
%
%    \begin{macrocode}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem*{definition*}{Definition}
\newtheorem{example}[theorem]{Example}
\newtheorem*{example*}{Example}
%    \end{macrocode}
%
%    \begin{macrocode}
\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}
\newtheorem*{remark*}{Remark}
%    \end{macrocode}
%
% \end{environment}
% \end{environment}
% \end{environment}
% \end{environment}
% \end{environment}
% \end{environment}
% \end{environment}
% \end{environment}
%
% \begin{environment}{note}
%
% Someone else might also have a \env{note} environment.  Be careful.
%
%    \begin{macrocode}
\ifx\note\@@undefined
  \newtheorem{note}[theorem]{Note}
  \newtheorem*{note*}{Note}
\fi
%    \end{macrocode}
%
% \end{environment}
%
% \begin{environment}{proof}
%
% Now to hack the proof environment.
%
%    \begin{macrocode}
\renewenvironment{proof}[1][\proofname]{%
  \setcounter{claim}{0}%
  \par\normalfont%
  \trivlist\advance\itemindent\labelsep\let\makelabel\textit%
  \advance\@topsep-\parskip%
  \advance\@topsepadd-\parskip%
  \item[#1\@proofdot]%
}{%
  \qed\endtrivlist%
}
\def\@proofdot{.}
%    \end{macrocode}
% \end{environment}
%
% Done.
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
%^^A-------------------------------------------------------------------------
% \section{Implementation of the theorem style}
%
%    \begin{macrocode}
%<*thmstyle>
%    \end{macrocode}
%
% \begin{macro}{\thmhead@margin}
%
% Firstly, a macro to typeset the theorem environment header, with the number
% in the margin.
%
%    \begin{macrocode}
\def\thmhead@margin#1#2#3{%
  \thmnumber{\llap{#2{\normalsize\quad}}}%
  \thmname{#1}%
  \thmnote{ \the\thm@notefont(#3)}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thm@headfont@margin}
%
% Now to decide on the header font.  Definitely we want boldface; if the
% documentclass (or \package{mdwfonts}) has decided on a different font
% family for the headings, then we use that.
%
%    \begin{macrocode}
\def\thm@headfont@margin{%
  \fontseries{b}%
  \ifx\headdefault\@@undfined\else\fontfamily\headdefault\fi%
  \selectfont%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\qedsymbol}
%
% Use the nice symbol if available.
%
%    \begin{macrocode}
\AtBeginDocument{\ifx\square\@@undefined\else\def\qedsymbol{$\square$}\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\hack@thm}
%
% Now we hack the standard theorem styles to do what we want.  This is a
% tedious enough game with |\expandafter|.
%
%    \begin{macrocode}
\def\hack@thm#1#2{%
  \expandafter\def\expandafter#1\expandafter{#1%
    \let\thmhead\thmhead@margin%
    \thm@headfont{\thm@headfont@margin}%
    \thm@headpunct{}%
    \advance\thm@postskip-\parskip%
    \def\after@thm{#2}%
  }
}
\def\@endtheorem{\after@thm\endtrivlist\@endpefalse}
\let\@proofdot\relax
\hack@thm\th@plain{}
\hack@thm\th@definition{\qed}
\hack@thm\th@remark{\qed}
%</thmstyle>
%    \end{macrocode}
% \end{macro}
%
% \hfill Mark Wooding, \today
% \Finale
%
\endinput
