\ifx\labelsloaded\relax\endinput\else\let\labelsloaded\relax\fi
\catcode`\@=11 % Letter
%
\newwrite\@auxout
\newif\if@fwdref \@fwdreffalse
 
%      **************************
%      *       COUNTERS         *
%      **************************
%
\newcount\Thm@nro \Thm@nro=0
\newcount\Sec@nro \Sec@nro=0
\newcount\SubSec@nro \SubSec@nro=0
\newcount\Item@nro \Item@nro=0
%
\def\theThm{\the\Thm@nro}
\def\theSec{\the\Sec@nro}
\def\theSubSec{\the\SubSec@nro}
\def\theItem{\the\Item@nro}
%
%
\def\stepbyone#1{\global\expandafter
  \advance\csname#1@nro\endcsname by1\relax}
\def\stepcounter#1#2{\global\expandafter
  \advance\csname#1@nro\endcsname by #2\relax}
\def\setcounter#1#2{\global\expandafter\csname#1@nro\endcsname =#2\relax}
 
\def\Thm@#1{\xdef\@currentlabel{#1\LeftTagForm%
    \ifnum\Thm@nro=0
    \else\ifnum\Sec@nro=0 \theThm
	 \else
    		\ifnum\SubSec@nro=0 \theSec.\theThm
                \else\theSec.\theSubSec.\theThm
        	\fi
	 \fi
    \fi%
    \RightTagForm}%
  \xdef\@heading{%
    \LeftTagForm%
    \ifnum\Thm@nro=0
    \else\ifnum\Sec@nro=0 \theThm
	 \else
    		\ifnum\SubSec@nro=0 \theThm %%\theSec.\theThm
                \else\theSec.\theSubSec.\theThm
        	\fi
	 \fi
    \fi%
    \RightTagForm
}\@heading} %GRG
\def\Thm#1{\stepbyone{Thm}\Thm@{#1}}

\def\Sec@#1{\setcounter{SubSec}0\setcounter{Thm}0%
   \xdef\@currentlabel{Section \ifnum\Sec@nro=0\else\theSec\fi}
   \xdef\@currentcontentsentry{#1}
  \xdef\@heading{%
    \ifnum\Sec@nro=0\else\theSec\fi}\@heading}
\def\Sec#1{\stepbyone{Sec}\Sec@{#1}}

\def\SubSec@{\setcounter{Thm}0%
\xdef\@currentlabel{Section\ifnum\SubSec@nro=0\else \theSec.\theSubSec\fi}%
\xdef\@heading{\ifnum\SubSec@nro=0\else \theSec.\theSubSec\fi}\@heading}

\def\SubSec{\stepbyone{SubSec}\SubSec@}

\def\Item@{\xdef\@currentlabel{\LeftTagForm
    \ifnum\Item@nro=0 \else\romannumeral\theItem\fi\RightTagForm}%
  \@currentlabel}
\def\Item{\stepbyone{Item}\Item@}

%\def\proclaim#1#2#3{\setcounter{Item}0\smallskip\goodbreak
%   \thmskip\flushpar\Thm{#3}\label{#1}{\it #2}\nobreak\smallskip\nobreak\flushpar}
\def\cor#1#2{\proclaim{#1}{#2}{Corollary}}
\def\defn#1#2{\proclaim{#1}{#2}{Definition}}
\def\lemma#1#2{\proclaim{#1}{#2}{Lemma}}
\def\note#1#2{\proclaim{#1}{#2}{Note}}
\def\prop#1#2{\proclaim{#1}{#2}{Proposition}}
\def\thm#1#2{\proclaim{#1}{#2}{Theorem}}
\def\proof{\smallskip\flushpar{\smc Proof.}\quad}
\def\sketchproof{\smallskip\flushpar{\smc Sketch Proof.}\quad}
\def\temp#1{\smallskip\flushpar{\hbox{{\smc Temporary details:}} #1}{\hfil$\spadesuit$\linebreak}\flushpar}
\def\thmskip{\vskip10pt}

\def\item#1#2{\flushpar{\hbox{\hbox to1cm{\rm\Item}\label{#1}\hfil} #2\hfil}}
\def\Proofitem#1#2{\flushpar{\hbox{\refer{#1}} #2}\flushpar}

\def\LeftTagForm{}
\def\RightTagForm{}
\def\Tag@{\xdef\@currentlabel{\LeftTagForm%
    \ifnum\Thm@nro=0
    \else\ifnum\Sec@nro=0 \theThm
	 \else
    		\ifnum\SubSec@nro=0 \theSec.\theThm
                \else\theSec.\theSubSec.\theThm
        	\fi
	 \fi
    \fi%
    \RightTagForm}%
  \@currentlabel}
\def\Tag{\stepbyone{Thm}\Tag@}
 
%\catcode`\@=\active
%
%   END EDITING
%================================================================
 
%    ********************************
%    *   CROSS REFERENCING MACROS   *
%    ********************************
%
%  The user writes  \label{foo}  to define the following cross-references:
%     \ref{foo}     : value of most recently incremented referencable counter.
%       (Chapter, section, theorem, tag, figure, table and roster's item
%        counters are referencable, footnote counters are not.)
%     \pageref{foo} : page number at which \label{foo} command appeared.
%  where  foo  can be any string of characters not containing '\', '{' or '}'.
%
 
\def\thepage{\number\pageno}
 
\def\refer#1{\@ifundefined{r@#1}{{ ??}\global\@fwdreftrue\@warning
   {Reference `#1' on page \thepage \space
    undefined}}{\edef\@tempa{\@nameuse{r@#1}}\expandafter
    \@car\@tempa \@nil\null}}
 
\def\referpage#1{\@ifundefined{r@#1}{{ ??}\global\@fwdreftrue\@warning
   {Reference `#1' on page \thepage \space
    undefined}}{\edef\@tempa{\@nameuse{r@#1}}\expandafter
    \@cdr\@tempa\@nil\null}}
 
\newif\if@filesw \@fileswtrue
 
\def\label#1{\@bsphack\if@filesw {\let\thepage\relax
   \xdef\@gtempa{\write\@auxout{\string
      \newlabel{#1}{{\@currentlabel}{\thepage}}}}}\@gtempa
   \fi\@esphack}
 
\def\contentsentry#1{\@bsphack\if@filesw {\let\thepage\relax
   \xdef\@gtempa{\write\@auxout{\string
      \newcontentsentry{#1}{{\@currentlabel}{\thepage}}}}}\@gtempa
   \fi\@esphack}
 
 
% \rosterlabel is AmSTeX-specific
\def\rosterlabel#1{\@bsphack\xdef\@currentlabel{%
   \therosteritem{\number\rostercount@}}%
   \if@filesw {\let\thepage\relax
   \xdef\@gtempa{\write\@auxout{\string
      \newlabel{#1}{{\@currentlabel}{\thepage}}}}}\@gtempa
   \fi\@esphack}
 
\def\newlabel#1#2{\@ifundefined{r@#1}{}{\@warning{Label `#1' multiply
   defined}}\global\@namedef{r@#1}{#2}}
 
\def\@currentlabel{} % For \label commands that come before any environment
 
%     ****************
%     *   INDEXing   *
%     ****************
% This is quite primitive
%
\newif\if@indexsw
\newwrite\@idxout
 
\def\makeindex{\@indexswtrue\immediate\openout\@idxout=\jobname.idx
   \immediate\write\@idxout{\string\relax}}
 
\def\index#1{\@bsphack\if@indexsw {\let\thepage\relax
   \xdef\@gtempa{\write\@idxout{\string
      \indexentry{#1}{\thepage}}}}\@gtempa
   \fi\@esphack}
 
\def\indexentry#1#2{\noindent {#1}{\unskip\nobreak\hfil\penalty50
   \hskip2em\hbox{}\nobreak\hfil#2%
   \parfillskip=0pt \finalhyphendemerits=0 \par}}
 
%      **************************
%      *       CITATIONS        *
%      **************************
%      These are AmS-TeX specific
%
%  CONVENTION
%  \b@FOO : The name or number of the reference created by \citeto{FOO}
%     E.g., if \citeto{FOO} -> [17] , then \b@FOO -> 17.
%
\def\make@ref#1{\@ifundefined{b@#1}{{??}\global\@fwdreftrue\@warning
   {Citation `#1' on page \thepage \space
    undefined}}{\@nameuse{b@#1}}}
%
% FROM AMSTEX.TEX
%\newif\ifin@
%\def\in@#1#2{\def\in@@##1#1##2##3\in@@{\ifx\in@##2\in@false\else\in@true\fi}%
% \in@@#2#1\in@\in@@}
%
\def\citeto#1{\relaxnext@
 \def\nextiii@##1,##2\end@{\immediate\write\@auxout{\string\citation{##1}}%
   {\make@ref{##1}},##2}%
 \in@,{#1}\ifin@\def\next{\nextiii@#1\end@}\else
   \def\next{\immediate\write\@auxout{\string\citation{#1}}%
     {\make@ref{#1}}}\fi
     \next}
\def\noOf#1{\global\advance\c@num by1
   \if@filesw \immediate\write\@auxout
   {\string\bibcite{#1}{\the\c@num}}\fi\no\make@ref{#1}}
\def\keyOf#1#2{\if@filesw
   {\def\protect##1{\string ##1\space}\immediate
   \write\@auxout{\string\bibcite{#1}{#2}}}\fi
   \key{$\lbrack${\bf #2}$\rbrack$}}
 
\def\TAG{\tag"\Tag"}
% AmSTeXin pitaa joissakin komennoissaan voida nahda \tag suoraan
% joilloin komentoa \TAG ei voi kayttaa, vaan tulee kirjoittaa \tag"\Tag"
% Tassa pitaa kayttaa AmSTeXin \tagin literaalista optiota \tag"...",
% silla numerossa \Tag ovat sulut (...) mukana.
% Jos sulkujen muotoa halutaan muuttaa, ovat sulut maariteltyna nimilla
% \LeftTagForm (vasen kaarisulku) ja \RightTagForm (oikea kaarisulku).
% Siis: \redefine\LeftTagForm{...} (tarvittaessa \predefine ...).
% ==========================================================================
\let\@@end=\end
\let\@@supereject=\supereject
\def\supereject{\@@supereject}
\def\end{\if@fwdref
  \typeout{Undefined forward references/citations. Rerun.}\fi
  \@@end}
%
% PLAINin \bye
%\def\bye{\par\vfill\supereject\end}
%
% AMSTEXin \enddocument
%\outer\def\enddocument{\nobreak\sfcode`\.=3000 \vskip12\p@ minus6\p@
% \thekeywords@\thesubjclass@\nobreak\vskip12\p@ minus6\p@\addressnum@\z@
% \loop\ifnum\addressnum@<\addresscount@\advance\addressnum@\@ne
% \csname address\number\addressnum@\endcsname\repeat
% \vfill\supereject\end}
 
%      **************************
%      *     USEFUL HACKS       *
%      **************************
%
%  \@namedef{NAME}   : Expands to \def\NAME , except name can contain any
%         characters.
%  \@nameuse{NAME}   : Expands to \NAME .
%  \@ifnextchar X{YES}{NO}
%       : Expands to YES if next character is an 'X',
%         and to NO otherwise. (Uses temps a-c.)
%         NOTE: GOBBLES ANY SPACE FOLLOWING IT.
%  \@ifundefined{NAME}{YES}{NO}
%       : If \NAME is undefined then it executes YES,
%         otherwise it executes NO.  More precisely,
%         true if \NAME either undefined or = \relax.
%  \@car T1 T2 ... Tn\@nil == T1  (unexpanded)
%  \@cdr T1 T2 ... Tn\@nil == T2 ... Tn     (unexpanded)
%  \typeout{message} : produces a warning message on the terminal
%  \@warning{message}: prints 'Warning: message.'
%
\newwrite\@unused
\def\typeout#1{{\let\protect\string\immediate\write\@unused{#1}}}
\def\@warning#1{\typeout{Warning: #1.}}
 
\def\@namedef#1{\expandafter\def\csname #1\endcsname}
\def\@nameuse#1{\csname #1\endcsname}
 
\def\@car#1#2\@nil{#1}
\def\@cdr#1#2\@nil{#2}
 
\long\def\@ifundefined#1#2#3{\expandafter\ifx\csname
  #1\endcsname\relax#2\else#3\fi}
 
\def\@ifnextchar#1#2#3{\let\@tempe #1\def\@tempa{#2}\def\@tempb{#3}\futurelet
    \@tempc\@ifnch}
\def\@ifnch{\ifx \@tempc \@sptoken \let\@tempd\@xifnch
      \else \ifx \@tempc \@tempe\let\@tempd\@tempa\else\let\@tempd\@tempb\fi
      \fi \@tempd}
 
\def\?{\let\@sptoken= } \?  % this makes \@sptoken a space token
 
\def\?{\@xifnch} \expandafter\def\? {\futurelet\@tempc\@ifnch}
 
% SPACE HACKS
% \@bsphack ... \@esphack
%     used by macros such as \index and \label
%     that want to be invisible -- i.e.,
%     not leave any extra space when used in the middle of text.  Such
%     a macro should begin with \@bsphack and end with \@esphack
%     The macro in question should not create any text, nor change the
%     mode.
%
\newdimen\@savsk
\newcount\@savsf
 
\def\@bsphack{\@savsk\lastskip
    \ifhmode\@savsf\spacefactor\fi}
 
\def\@esphack{\relax\ifhmode\spacefactor\@savsf
     {}\ifdim \@savsk >\z@ \ignorespaces\fi\fi}
 
%     **************************
%     *    FILE HANDLING       *
%     **************************
%
%    \Input{foo} : If file foo exists, then \input's it,
%                  otherwise types a warning message.
\def\Input#1{\openin1 #1 \ifeof1 \typeout{No file #1.}\closein1
    \else\closein1 \relax\input #1 \fi}
 
%      **************************
%      *     BIBLIOGRAPHY       *
%      **************************
%
\newcount\c@num \c@num=0
 
\def\bibcite#1#2{\@ifundefined{r@#1}{}{\@warning{Referencelabel `#1' multiply
   defined}}\global\@namedef{b@#1}{#2}}
 
\def\@gobble#1{}
\let\citation\@gobble
 
%      **************************
%      *      INSERTIONS        *
%      **************************
%
% AMSTEX.TEXin \ins@ (\midspace...) MUUTETTU
%
% Jos \ins@:n \caption ulottuu kahdelle riville
% tulisi \captionissa mahd. esiintyva \Fig sovellettua
% kahdesti ja \Fig-numero kasvaisi kahdella
%
\def\CorrectCounter{}
%\newif\ifmid@
%\def\topspace{\mid@false\ins@}
%\def\midspace{\mid@true\ins@}
\def\ins@#1{\relaxnext@
 \smallcaptionwidth@\captionwidth@\gdef\thespace@{#1}%
 \def\next@{\ifx\next\space@\def\next. {\futurelet\next\nextii@}\else
  \def\next.{\futurelet\next\nextii@}\fi\next.}%
 \def\nextii@{\ifx\next\caption\def\next\caption{\futurelet\next\nextiii@}%
  \else\let\next\nextiv@\fi\next}%
 \def\nextiv@{\vnonvmode@
  {\ifmid@\let\next\midinsert\else\let\next\topinsert\fi
  \next\vbox to\thespace@{}\endinsert}
  {\ifmid@\nonvmodeerr@\midspace\else\nonvmodeerr@\topspace\fi}}%
 \def\nextiii@{\ifx\next\captionwidth\let\next\nextv@
  \else\let\next\nextvi@\fi\next}%
 \def\nextv@\captionwidth##1##2{\smallcaptionwidth@##1\relax\nextvi@{##2}}%
 \def\nextvi@##1{\def\thecaption@{##1}%
  \def\next@{\ifx\next\space@\def\next. {\futurelet\next\nextvii@}\else
   \def\next.{\futurelet\next\nextvii@}\fi\next.}%
  \futurelet\next\next@}%
 \def\nextvii@{\vnonvmode@
  {\ifmid@\let\next\midinsert\else
  \let\next\topinsert\fi\next\vbox to\thespace@{}\nobreak\smallskip
  \setbox\z@\hbox{\noindent\ignorespaces\thecaption@\unskip}%
  \ifdim\wd\z@>\smallcaptionwidth@\centerline{\vbox{\hsize\smallcaptionwidth@
  \noindent\ignorespaces\CorrectCounter\thecaption@
  \gdef\CorrectCounter{}\unskip}}%
  \else\centerline{\box\z@}\fi\endinsert}
  {\ifmid@\nonvmodeerr@\midspace
  \else\nonvmodeerr@\topspace\fi}}%
 \futurelet\next\next@}
% END OF \ins@ MODIFICATION
%
%
%      **************************
%      *     INITIALIZATION     *
%      **************************
\Input{\jobname.aux}
\immediate\openout\@auxout=\jobname.aux
\immediate\write\@auxout{\string\relax}
\catcode`\@=\active
