\message{<Paul Taylor's right-justified end-of-proof mark, 16 February 1993>}
%%========================================================================
%************************************************************************%
%*									*%
%*	Maybe you already have a \qed macro to put a little square	*%
%*      on the right hand side at the end of a mathematical proof?	*%
%*									*%
%*			IT'S PROBABLY MINE!				*%
%*									*%
%*	     Please compare yours with \parag@pushright below.		*%
%*									*%
%*	This new file is compatible, it fixes the bugs, has extra	*%
%*	features, and is maintained and portable.  Please use it!	*%
%*									*%
%************************************************************************%
%========================================================================%

% COPYRIGHT (C) 1993 Paul Taylor, Imperial College, London SW7 2BZ, UK
% You may use this file for any purpose. You may copy it on condition that
% you do so in exactly the form in which it was supplied, make no charge
% for the copy and impose the same conditions on the recipient.
% This software is supplied ``as is'' without warranty, express or implied,
% including but not limited to merchantability and fitness for any particular
% purpose. No liability is accepted by the author, Imperial College or any
% third party for any direct or consequential damages, however caused.
% In all cases you must keep several backup versions of your files in a place
% to which your machine cannot write, and you must check documents visually
% before submitting the final version to a publisher or expensive printer.

% WARNING: If you use this with Frank Mittlebach's "theorem.sty" you
% MUST load theorem first, then QED, then do the \newtheorem commands.
% "theorem.sty" provides a number of typographical styles and is
% available by anonymous ftp from ftp.uni-stuttgart.de (129.69.1.12)
% in the directory         /pub/soft/tex/macros/latex/styles/base/theorem
% also Aston: ftp.tex.ac.uk:/pub/archive/macros/latex/styles/base/theorem

%	Authoritative version:	/tex/contrib/Taylor/tex/QED.sty
%	by anonymous FTP from:	theory.doc.ic.ac.uk (146.169.2.27)
%	author/maintained by:	Dr Paul Taylor <pt@doc.ic.ac.uk>

% WARNING: This file replaces some of the functions of "Paul.sty"
% and MUST NOT be used with it as serious conflicts will arise.
% A new version is available as /tex/contrib/Taylor/Paul.sty as above.

%=========================================================================

%	USER INTERFACE AND EXAMPLES (see also OPTIONS below)

%	\begin{Proof} ... \end{Proof}
%	or    \Proof  ... \endProof
%	or    \Proof  ... \endproof
%	or    \Proof  ... \qed		also \QED and \QEI

% WARNING: \qed which doesn't follow \Proof will not print the square.

% Each properly nested Proof/endProof pair gets a single
% \qedsymbol at the end, on the far right hand side.
% Unfortunately, this may still be in the wrong place
% if the proof ends with a display or itemisation.
% To solve this, \qed may be put *inside* the last substantive
% environment; the \qedsymbol will be put at that point
% (the visual end) and suppressed at the logical end of proof.

% Examples:
%	\begin{Proof}			\begin{Proof}
%	  ...				  ...
%	  \begin{eqnarray}		  \begin{itemize}
%	   ... &=& ... \\		    \item ...
%	   ..  &=& ... &\qed		    \item ...   \qed
%	  \end{eqnarray}		  \end{itemize}
%	\end{Proof}			\end{Proof}
% also works with $$...$$ and \[...\] displays, including fleqn.sty.
% (Actually the eqnarray example doesn't work; I consider this a LaTeX bug.)
% (Unfortunately some environments, such as picture, do not allow it at all.)

% Why can't we do this with \endProof? Because by that stage
% the damage has been done: the environments, paragraphs or
% displays have already been closed and their \hbox'es added
% to the current vertical list, and cannot be removed for
% alteration if this is the current page.

% Even if you don't need to put \qed inside an inner environment
% to avoid a blank line, it's still a good idea to do so, because
% otherwise it's possible that intervening end-of-environment
% material may allow TeX to put the mark on the next page.

% This file may be called QED.sty, QED.tex, qed.sty or qed.tex.
% It may be loaded either as a \documentstyle option or using \input,
% as it is careful to handle the \catcode of @ appropriately.

% If you don't use theorem.sty, this file instead provides options to use
% commands to begin theorems, instead of enclosing them in environments,
% and to suppress numbering and italicisation. A rudimentary version of
% \newtheorem for plain TeX is also implemented (always using commands
% rather than environments).

% At the end of the file some useful \newtheorem
% commands are listed but not executed.

% \Theorem\Proof\endProof\qed will print the second square, whereas
% \begin{Theorem}\end{Theorem}\begin{Proof}\end{Proof}\qed will not.

%=========================================================================
% Make @ letter, saving its old code to restore at the end of this file.
% Look for this towards the end of the file if you think something's missing!
% "plain.tex" defines \lq as a synonym for back quote (character constant);
% I use it to avoid corruption when sending TeX files by email via BITNET.
\edef\qedrestoreat{\noexpand\catcode\lq\noexpand\@=\the\catcode\lq\@}%%
\catcode\lq\@=11

% in LaTeX, \protect the user commands;
% in plain TeX, ensure that it is defined.
\ifx\protect\undefined\let\protect\relax\fi

%=========================================================================

%				OPTIONS

% These are the user commands.

\def\qed{\protect\@qed{$\qedsymbol$}}% suppressible printing of \qedsymbol
\def\pushright{\protect\@pushright}% takes an argument, which it always prints

% theoremata in Euclid needed proof:   "which   was  to be shown"
\def\QED{\protect\@qed{{\rm Q.E.D.}}}% "quod   erat  demonstrandum"

% but problemata needed construction:  "which   was  to be found"
\def\QEI{\protect\@qed{{\rm Q.E.I.}}}% "quod   erat  inveniendum"

% a LaTeX environment
\def\Proof{\protect\@Proof}\def\endProof{\protect\@endProof}%
\let\proof\Proof\let\endproof\endProof


% These are the user-settable parameters.

% The actual (maths) symbol used by \qed
\def\qedsymbol{\square}% PLEASE NOTE: this is in the AMS symbols font.
%\def\qedsymbol{\blacksquare}%  use one of these instead
%\def\qedsymbol{\hbox{\rm Q.E.D.}}%        if you prefer

% This is the text used by \Proof.
% A new paragraph is begun without indentation.
% Change it to {\kern\parindent\sc D\'emonstration: } if you like.
\def\TheWordProof{\bf Proof\enskip}

% his is the font command used for the text of proofs
\def\ProofFont{}

% The command \AutoQEDtrue causes a \qed in \end{Theorem}, so you do
%	\begin{Theorem} ... \begin{Proof} ... \end{Proof} \end{Theorem}
% but if you omit the proof, the statement gets a little square at the end.
\newif\ifAutoQED\AutoQEDfalse

% The remaining options are redundant with theorem.sty
% so are not defined if this has been loaded.
\newif\ifNumberResults
\ifx\theorem@style\undefined
   %
   % How to print the name (#1), number (#2) and attribution (#3) of a theorem.
   \NumberResultstrue
   % If the number (#2) is absent, "\unskip" removes the
   % unwanted space "\ " from before it (16 Feb 1993).
   \def\TheoremHeader#1#2#3{\bf #1\ifNumberResults\ #2\unskip\fi#3}
   %
   % This is the font command used for the text of theorem enunciations
   % change to {\rm} or {\small} if you like.
   \def\TheoremFont{\it}
   %
   % The command \NumberResultsfalse will suppress theorem numbering.
   %
   % The command \TheoremsAsCommands will allow you to write
   % \Theorem instead of \begin{Theorem} ... \end{Theorem}.
   % In this case italicisation is suppressed by default.
   % To do this with theorem.sty would involve rewriting the substyles.
   %
   % WARNING: if you subsequently redefine \TheoremFont, and use the command
   % instead of the environment, the font will remain in force afterwards!
\fi

%=========================================================================
%  If you already have a macro called \qed, \endproof or \pushright
%  which is laid out in a similar way to the following two definitions
%  (but with \hbox@pushright incorporated) then it is almost certainly
%  my code, which has been copied anonymously via numerous people since
%  my Cambridge PhD thesis in August 1986. Belated acknowledgements to
%  Chris Thompson of the Cambridge University Computing Service for his
%  help with my early attempts at TeX, in particular this problem.

%  The complexity of the macro necessary to get a little box on the
%  right-hand-side at the end of a proof is amazing.  It really does
%  have to be this long!  Otherwise you're liable to get it at the
%  beginning of the next line, or even on the next page. 

\def\parag@pushright#1{{% set up
    \parfillskip=0pt            % so \par doesnt push \square to left
    \widowpenalty=10000         % so we dont break the page before \square
    \displaywidowpenalty=10000  % ditto
    \finalhyphendemerits=0      % TeXbook exercise 14.32
    %
    %                   horizontal
    %\leavevmode                % not needed since we now only use it in hmode
    \hbox@pushright		% this used to be incorporated
    #1%                         % the end-of-proof mark (or whatever)
    %
    %                     vertical
    \par}}%                     % build paragraph with the above parameters

\def\hbox@pushright{% horizontal
    \unskip                     % remove previous space or glue
    \nobreak                    % don't break lines
    \hfil                       % ragged right if we spill over
    \penalty50                  % discouragement to do so
    \hskip.2em                  % ensure some space
    \null                       % anchor following \hfill
    \hfill                      % push \square to right
}%

%  Ideally, we would do the above by completing the paragraph, display or
%  other structure, and then modifying its last line (\hbox).
%  Unfortunately this cannot be done, because TeX does not allow you to
%  remove a box (using \lastbox) which has already been contributed to the 
%  main vertical list (current page).

%  It would, I suppose, be possible to do it by collecting the entire proof
%  in its own \vbox, modifying that and \unvbox'ing it to the current page.
%  This approach may lead to the following problems:
%  (i) overflow of TeX's main memory, especially if the \end{Proof} is missing;
%  (ii) footnotes, floats, marginpars, labels and index items may be lost.
%  (iii) I don't know how page-breaking would be handled.

%========================================================================

% Switch to enable/suppress \qedsymbol; since suppression may be done
% within a nested scope it must be global, so we do our own save/restore
% in a symbol which is local to the Proof environment.
% On first use, we check to see whether an obsolete version of Paul.sty
% is around, and complain loudly if it is, because they interfere seriously.

\newif\if@qed\@qedfalse
\def\save@set@qed{\check@pt@sty@v\let\saved@ifqed\if@qed\global\@qedtrue}%
\def\restore@qed{\global\let\if@qed\saved@ifqed}

% prefer proofs with statements if possible - hence \penalty700
% the only essential part of this is \save@set@qed
\def\@Proof{%
   \par\removelastskip\smallskip\penalty700
   \save@set@qed
   \noindent\ProofFont{\TheWordProof\enskip}%
}%

\def\@endProof{%
   \qed\restore@qed
   \penalty-700 \smallskip
}

\def\@qed#1{\check@pt@fm@thm
\if@qed					% have we already done \qed?
     \global\@qedfalse\pushright{#1}%  - no  - do it now, but not again
\else\ifhmode\ifinner\else\par\fi\fi%  - yes - just end paragraph, if any
\fi}

% \pushright{text} always prints its argument, on the far right, in text mode
\def\@pushright#1{%
  {\ifvmode				% vertical mode (see comments below)
       \null\hfill{#1}\par		% --- #1 alone in a paragraph!
  \else\ifmmode\maths@pushright{\hbox{#1}}% maths (force a text argument)
       \else\ifinner\hbox@pushright{#1}%  inside an \hbox
            \else\parag@pushright{#1}%    in a paragraph
  \fi  \fi  \fi
}}%

% In the maths case we are really at the mercy of the environment.
% Assuming that we have $\qed$ simply embedded in a paragraph,
% the horizontal processing \hbox@pushright will at least make
% sure that the square isn't at the beginning of the line, but
% we can't stop it being on the next page. However it is more
% likely that the reason why we're in maths mode is that we're
% inside an eqnarray or similar, in which case \hfill will do
% its best to put the square on the right of the available space.
\def\maths@pushright#1{{%
  \ifinner
     \hbox@pushright{#1}%
  \else
     \eqno#1%	use TeX's right equation number feature (\eqno) within $$.
     \def\]{$$\ignorespaces}%	 suppress LaTeX's error checking (HACK!)
  \fi
}}%

% An idea that didn't work: see TeXbook p188. (16 Feb 1993)
% Use \predisplaysize to find out the width of the previous box,
% in the case where it has already been completed.
% But this can only be used inside displays ($$), and a display in
% vertical mode begins a paragraph first, so we end up with
% \predisplaysize=\parindent+2em whatever happened in the previous
% paragraph.

% \ifinner is true inside a \vbox or \vadjust; in both of these
% circumstances we are allowed to remove boxes with \lastbox etc.
% Maybe it's worth testing this case and doing it.

%===========================================================================
% We have to add to the theorem environments the commands to enable \qed.
% This is done by re-defining \@thm#1#2.
% Note that Mittelbach's theorem.sty re-defines \@thm and \@endtheorem, so
% that must be loaded before us, otherwise our changes will get over-written
% and \qed will never be enabled.
% Paul.sty didn't use this method of gaining control inside a theorem,
% whereas it did define \qed and \pushright, so here is the one place
% where we can check for it.

\let\nonqed@thm\@thm % save original
\let\nonqed@endthm\@endtheorem

\def\@thm{\save@set@qed\nonqed@thm}%
\def\@endtheorem{\ifAutoQED\qed\fi\restore@qed\nonqed@endthm}%

%===========================================================================
% Check for obsolete versions and clashes of this and other macro files.
% Why do I do this? Because of those people who complain to me that
% something doesn't work but refuse to read the manual, and those
% who refuse to update their software, and then publish papers with
% commutative diagrams full of the bugs which I fixed several years ago.

\def\obsolete@pt@version#1{\errhelp={%
%     *****************************************************************
%     *            The master version is available by                 *
        Anonymous FTP theory.doc.ic.ac.uk /tex/contrib/Taylor/tex/#1}%*
%     *****************************************************************
\errmessage{You have an obsolete version of #1 - please get a new one}}%

% First check QED.sty
\ifnum\year>1993 \obsolete@pt@version{QED.sty}\fi

% If theorem.sty hasn't been loaded, arrange to check it again in \@qed.
\ifx\theorem@style\undefined
   \def\check@pt@fm@thm{\relax
      \ifx\theorem@style\undefined
          \global\let\check@pt@fm@thm\relax
      \else\errhelp={The macros \@thm and \@endtheorem need to be re-defined.}%
          \errmessage{QED.sty must be loaded AFTER theorem.sty but before
           using \string\newtheorem}%
      \fi
      \global\let\check@pt@fm@thm\relax
      }%
\else
   \let\check@pt@fm@thm\relax%
\fi

% Check for Paul.sty, which SERIOUSLY conflicts with this file.
% and also for version 2 or version 3 of commutative diagrams.
\def\check@pt@sty@v{%
   \relax
   \ifx\@@begintheorem\undefined
   \else\message{*** QED.sty and the old Paul.sty seriously conflict! ***}%
        \obsolete@pt@version{Paul.sty}%
   \fi
   \ifx\execHorizontalMap\undefined
        \gdef\obsolete@pt@version##1{}%
   \else\global\let\di@gr@m\diagram
        \gdef\diagram{\obsolete@pt@version{diagrams.tex}\di@gr@m}%
   \fi
   \global\let\check@pt@sty@v\relax
}%

% And check whether you've actually defined \square.
\ifx\square\undefined
   \def\square{\bigcirc
      \errhelp={Anonymous ftp e-math.ams.com /ams/amsfonts}%
      \errmessage{\string\square\space is an AMS symbol}%
      \global\let\square\bigcirc}%
\fi

%===========================================================================
% If Mittelbach's theorem.sty is loaded, skip the rest of the file.
\ifx\theorem@style\undefined\else\qedrestoreat\expandafter\endinput\fi
%===========================================================================

% Allow the user to suppress italicisation and numbering.

% The following two macros are called by LaTeX to begin a theorem
% respectively with and without an optional (``attribution'') argument.
% It is clear from latex.tex that they are intended to be customised.
% The code common to the two has been taken out as \begin@theorem and
% \TheoremHeader. Of these, the former is re-defined by \TheoremsAsCommands.

\def\@opargbegintheorem#1#2#3{\begin@theorem{\TheoremHeader{#1}{#2}{ (#3)} }}

\def\@begintheorem#1#2{\begin@theorem{\TheoremHeader{#1}{#2}{} }}

\def\begin@theorem#1{%
  \trivlist\item[\hskip\labelsep {#1}]%		% LaTeX likes this method.
  \TheoremFont					% Italicise the enunciation
  \ifx\ProofFont\empty\def\ProofFont{\rm}\fi	% but not any nested proof.
  }%


%%% Here's what LaTeX (92/1/14) does:
%%%% RmS 91/08/14 Moved \it after \item to make it work with NFSS
%%\def\@begintheorem#1#2{\trivlist \item[\hskip \labelsep{\bf #1\ #2}]\it}
%%\def\@opargbegintheorem#1#2#3{\trivlist
%%      \item[\hskip \labelsep{\bf #1\ #2\ (#3)}]\it}
%%\def\@endtheorem{\endtrivlist}


% The document style option "theorems-as-commands" will allow you to
% write \Theorem instead of \begin{Theorem} ... \end{Theorem}.
\def\TheoremsAsCommands{%
  \def\TheoremFont{}% suppress italicised theorems.
  \def\begin@theorem##1{%
     \par\removelastskip\smallskip
     \save@set@qed % enable \qed
     \noindent{##1}% the name
     \TheoremFont % italicise at your own risk!
   }%
  \def\@endtheorem{\ifAutoQED\qed\fi\restore@qed}%
}%

\expandafter\let\csname ds@theorems-as-commands\endcsname\TheoremsAsCommands

%===========================================================================
% If we're running under LaTeX, skip the rest of the file.
\ifx\newtheorem\undefined\else\qedrestoreat\expandafter\endinput\fi
%===========================================================================

% This implements a rudimentary version of \newtheorem for plain TeX.
% Only the unattributed theorems and the
% declarations \newtheorem{Theorem}[Result]{Theorem} and
% \newtheorem{Convention}[Unnumbered]{Convention} are supported.
% The numbering is based on the counts \chapternumber, \sectionnumber
% and \subsectionnumber, which it is up to you to define with \newcount.
% It's also up to you to reset \c@Result at the beginning of each section.

% Form the number: (chapter).(section).(subsection).(#1)
% where any of the first three parts which is zero
% negative or undefined is omitted.
\def\ther@sult#1{%
\ifx\chapternumber\undefined\else
   \ifnum\chapternumber>0 \number\chapternumber.\fi\fi
\ifx\sectionnumber\undefined\else
   \ifnum\sectionnumber>0 \number\sectionnumber.\fi\fi
\ifx\subsectionnumber\undefined\else
   \ifnum\subsectionnumber>0 \number\subsectionnumber.\fi\fi
\expandafter\number\csname c@#1\endcsname}%

% plain version of LaTeX's \@thm
\def\plain@thm#1#2{% #1=counter, #2=text
  \save@set@qed
  \expandafter\advance\csname c@#1\endcsname1
  \@begintheorem{#2}{\csname the#1\endcsname}%
}%

% Rudimentary \newtheorem
\def\newtheorem#1[#2]#3{%   #1=command, #2=counter, #3=text
  %
  % \def\#1{\plain@thm{#2}{#3}}
  \expandafter\def\csname #1\endcsname{\plain@thm{#2}{#3}}%
  %
  % \def\end#1{\@endtheorem}
  \expandafter\def\csname end#1\endcsname{\@endtheorem}%
  %
  % if \c@#2 is not defined
  %    \newcount\c@#2
  %    \def\the#2{\ther@sult{#2}}
  \expandafter\ifx\csname c@#2\endcsname\relax
     \expandafter\expandafter\csname newcount\endcsname
        \csname c@#2\endcsname
     \expandafter\def\csname the#2\endcsname{\ther@sult{#2}}%
  \fi
}%

\TheoremsAsCommands

\qedrestoreat\endinput

%===========================================================================

% The following commands are not executed (because of \endinput above),
% but you may find it convenient to include some of them.

% First set up uniform numbering in LaTeX;
% don't use this in plain TeX because this usage of \newcommand is not
% supported and we have already done by hand what it would do in LaTeX,
% viz to define the macro \theResult and the count \c@Result.
\ifx\@@thm\undefined
	\newtheorem{Result}{Result}[subsection]%
	\@addtoreset{Result}{section}%
	\ifx\chapter\undefined\else\@addtoreset{Result}{chapter}\fi
\fi

\let\theUnnumbered\relax\countdef\c@Unnumbered255 \def\p@Unnumbered{}

\newtheorem{Answer}[Result]{Answer}
\newtheorem{Answers}[Result]{Answers}
\newtheorem{Conjecture}[Result]{Conjecture}
\newtheorem{Construction}[Result]{Construction}
\newtheorem{Convention}[Unnumbered]{Convention}
\newtheorem{Corollary}[Result]{Corollary}
\newtheorem{Counterexample}[Result]{Counterexample}
\newtheorem{Definition}[Result]{Definition}
\newtheorem{Example}[Result]{Example}
\newtheorem{Examples}[Result]{Examples}
\newtheorem{Exercise}[Result]{Exercise}
\newtheorem{Exercises}[Result]{Exercises}
\newtheorem{Fact}[Result]{Fact}
\newtheorem{Facts}[Result]{Facts}
\newtheorem{Lemma}[Result]{Lemma}
\newtheorem{NB}[Unnumbered]{NB}
\newtheorem{Notation}[Result]{Notation}
\newtheorem{Note}[Unnumbered]{Note}
\newtheorem{Observation}[Result]{Observation}
\newtheorem{Observations}[Result]{Observations}
\newtheorem{Problem}[Result]{Problem}
\newtheorem{Proposition}[Result]{Proposition}
\newtheorem{PS}[Unnumbered]{PS}
\newtheorem{Remark}[Result]{Remark}
\newtheorem{Question}[Result]{Question}
\newtheorem{Questions}[Result]{Questions}
\newtheorem{Theorem}[Result]{Theorem}
\newtheorem{Thesis}[Result]{Thesis}
\newtheorem{Warning}[Result]{Warning}

