% This is file `theomac.sty',
%
% The usage is explained after coding-stuff, right after
% "\endinput". This way you don't need all these percent-chars
% all the time...
%
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{amsmath}
\ProvidesPackage{theomac}
  [2007/06/04 v1.00beta2 Define macros from theorems.]
%
% Find out how to catch the arguments - there are the following
% possibilities:
%   - \newtheorem{env_name}[numbered_like]{caption}
%   - \newtheorem{env_name}{caption}[within]
%   - \newtheorem{env_name}{caption}
%
\newcommand*\newtheoremWithMacro[1]{%
  \@ifnextchar[%
              {\newtheoremwithmacrooptbrace{#1}}%
              {\newtheoremwithmacrobraceopt{#1}}%
}%
\newcommand*\newtheoremwithmacrobraceopt[2]{%
  \@ifnextchar[%
              {\newtheoremwithmacrolastopt{#1}{#2}}%
              {\newtheoremwithmacronolastopt{#1}{#2}}%
}%
%
% Depending on the order of the arguments, one of the following
% three macros is called for defining the theorem <foo> and
% afterwards calling "\@newtheoremWithMacro{<foo>}" for trans-
% forming that environment into one that catches up an additional
% optional argument for defining a macro which can henceforth be
% used for restating that theorem:
%
\@ifdefinable\newtheoremwithmacrooptbrace{%
  \gdef\newtheoremwithmacrooptbrace#1[#2]#3{%
    \newtheorem{#1}[#2]{#3}\@newtheoremWithMacro{#1}{#2}%
  }%
}%
\@ifdefinable\newtheoremwithmacrolastopt{%
  \gdef\newtheoremwithmacrolastopt#1#2[#3]{%
    \newtheorem{#1}{#2}[#3]\@newtheoremWithMacro{#1}{#1}%
  }%
}%
\@ifdefinable\newtheoremwithmacronolastopt{%
  \gdef\newtheoremwithmacronolastopt#1#2{%
    \newtheorem{#1}{#2}\@newtheoremWithMacro{#1}{#1}%
  }%
}%
%
%"\@newtheoremWithMacro{<foo>}" "transforms" the theorem-environ-
% ment "<foo>" into one that catches up an additional optional
% argument for defining a macro which can henceforth be used for
% restating that theorem:
%
\newcommand\@newtheoremWithMacro[2]{%
  % First "copy" the theorem-environment "<foo>" to the environ-
  % ment "<foo>theorem":
  \expandafter\@ifdefinable\csname #1theorem\endcsname{%
    \expandafter\global
    \expandafter\let
    \csname #1theorem\expandafter\endcsname
    \csname #1\endcsname
  }%
  \@ifundefined{end#1theorem}{%
    \expandafter\global
    \expandafter\let
    \csname end#1theorem\expandafter\endcsname
    \csname end#1\endcsname
  }%
  {%
    \edef\reserved@a{%
      \expandafter\expandafter
      \expandafter\@gobble
      \expandafter\string
      \csname end#1theorem\endcsname
    }%
    \@notdefinable
  }%
  %
  % Then define the macro "\@<foo>" to catch the first optional
  % argument into the macro \@tempa and to start collecting the
  % "body" of the environment into the token-register "\toks@"
  % whereby a call to the former "<foo>"-theorem-environment
  % which was saved in/copied to "<foo>theorem" must precede
  % everything in order to make sure that a theorem is formed
  % when later the token-register is "flushed":
  %
  \expandafter\@ifdefinable\csname @#1\endcsname{%
    \expandafter\gdef\csname @#1\endcsname[##1]{%
      \def\@tempa{##1}%
      \collect@body\toks@{#1theorem}%
    }%
  }%
  %
  % Afterwards redefine the internal starter-macro of the
  % "<foo>"-theorem-environment (and thus that environment it-
  % self) to call the "\@<foo>"-macro and hereby supply an empty
  % optional argument if no optional argument is provided:
  %
  \expandafter\gdef\csname #1\endcsname{%
    \@ifnextchar[%
    {\@tempswatrue\csname @#1\endcsname}%
    {\@tempswafalse\csname @#1\endcsname[]}%
  }%
  % Redefine the internal-ending-macro of the "<foo>"-theorem-
  % environment so that to the content of the token-register
  % both the leading token "\begin" [Within the \collect@body-
  % part there was inserted "{#1theorem}" -- Together this yields
  % "\begin{#1theorem}" ] and a trailing phrase "\end{#1theorem}"
  % get added. Afterwards flush the register once in order to
  % print the theorem...
  %
  \expandafter\gdef\csname end#1\endcsname{%
    \toks@\expandafter{%
           \expandafter\begin
           \the\toks@\end{#1theorem}}%
    \the\toks@
    % In case there was no optional argument, we have
    % "\@tempswafalse".
    % In case "\@tempa" is empty, the optional argument for
    % defining the macro is empty. In both cases we don't need to
    % define a "theorem-printing"-macro. Otherwise we have to.
    % Then within "\@tempa" the name of that macro is stored so
    % that "\@tempa" needs to be expanded once before defining
    % can take place. The environment itself is within the
    % token-register.
    % All we have to do is making sure that
    % - we always get the same value for the counter
    % - the counter does not get stepped
    % - label has no effect (otherwise: multiply defined
    %   references-errors!!!!!)
    % whenever the theorem is restated:
    \ifx\@tempa\@empty\@tempswafalse\fi
    \if@tempswa
      {%
        \restatedtrue          
        \expandafter\edef
        \csname the#2\endcsname{%
          \csname the#2\endcsname
        }%
        \expandafter\@ifdefinable\@tempa{%
          \expandafter\expandafter
          \expandafter            \gdef
          \expandafter\@tempa
          \expandafter{%
          \expandafter\bgroup
          \expandafter\restatedtrue
          \expandafter\def
          \csname the#2%
          \expandafter\expandafter
          \expandafter            \endcsname
          \expandafter\expandafter
          \expandafter            {%
          \csname the#2%
          \expandafter\endcsname
          \expandafter}%
          \expandafter\let
          \expandafter\label
          \expandafter\@gobble
          \expandafter\let
          \expandafter\refstepcounter
          \expandafter\@gobble
          \the\toks@
          \egroup
          }%
        }%
      }%
    \fi
  }%
}%
%
% The following switch is set true when a theorem is restated
% by calling the associated macro:
%
\@ifdefinable\ifrestated{%
  \@ifdefinable\restatedfalse{%
    \@ifdefinable\restatedtrue{%
      \newif\ifrestated
      \global\restatedfalse
    }%
  }%
}%

\endinput
%
Usage etc:
==========

change history
--------------

2006/07/29 v1.00beta1 - initial release
2007/06/04 v1.00beta2 - bugfix related to counter-management
                        with [numbered_like]-option.
                        if-switch "\ifrestated"  which is "true"
                        when theorem is restated and otherwise
                        "false".
                        I think you can use this switch
                        - within the theorem-environment's 
                          arguments
                        - within the theorem-environmnent's body
                        - when (re)defining or patching the
                          \the<counter> macro which is associated
                          to the theorem's counter.
                      

legal stuff
-----------

theomac.sty — Copyright (C) 2006, 2007 by Ulrich Diez
              (ulrich.diez@alumni.uni-tuebingen.de)

theomac may be distributed and/or modified under the conditions
of the LaTeX Project Public Licence (LPPL), either version 1.3
of this license or (at your option) any later version. (The
latest version of this license is in:
http://www.latex-project.org/lppl.txt
and version 1.3 or later is part of all distributions of LaTeX
version 1999/12/01 or later.)
The author and Current Maintainer of this work is Ulrich Diez.
This work has the LPPL maintenance status ‘maintained’.
Usage of theomac.sty is at your own risk.
There is no warranty — neither for the documentation nor for any
other part of theomac.sty. If something breaks, you usually may
keep the pieces.


What is it? / Acknowledgment / Intro:
-------------------------------------

The file theomac.sty is a package for LaTeX2e. It came into being
as a result of internet-discussion that took place at
comp.text.tex (usenet).
Subject: Restating Theorems
Initial posting in July 8 2006 by Louis Theran
(ID: 1152428341.793307.221130@p79g2000cwp.googlegroups.com)

Special thanks to Ulrich M. Schwarz, Louis Theran, Michael 
and Emanuale <emanuele.viola@gmail.com>  for
their comments and for reporting problems and thus providing
important contributions to the making of this package.

The introducing-phrase in Louis Theran's initial posting:

...
> In longer papers, I like to state results in an early section
> and then restate them before the proof later on.
...

Usually you define theorem-like-environments by means of the
"\newtheorem"-macro.

theomac.sty provides a macro "\newtheoremWithMacro" which is
similar to "\newtheorem" but which creates environments which
also allow specifying an additional optional-argument which is
to hold the name of a newly to be defined control-sequence.
Internally these environments fork:

If optional argument is not supplied or supplied empty:
  Simply create the theorem-text and hereby internally use an
  environment defined in terms of "\newtheorem".

If optional argument is supplied a macro-name:
  Additionally define a macro to spit out the theorem-text. (For
  getting the environment-text into a defining-control-sequence's
  argument, amsmath.sty's "\collect@body"-macro is used. I tried
  to take care so that referencing-labels won't get defined more
  than once and counters will be balanced correctly.)

  If you wish to repeat/restate the theorem, you can do so by
  simply calling that macro given in the optional argument when
  stating it the first time.

"\newtheoremWithMacro" has the same syntax as "\newtheorem".

Difference is:

  \newtheorem{foo}...

creates theorem-environment with syntax

  \begin{foo} ... \end{foo}

________________________

  \newtheoremWithMacro{foo}...

creates theorem(-like)-environment with syntax

  \begin{foo}[\macro] ... \end{foo}

If you supply the optional-argument "\macro", you can later
repeat/restate your theorem-statement by calling "\macro".
"\macro" will be defined in terms of "\global".
If "\macro" is already defined, an error is raised.

Usage
-----

Possible syntax-patterns for "\newtheorem" are:

  \newtheorem{env_name}[numbered_like]{caption}
  \newtheorem{env_name}{caption}[within]
  \newtheorem{env_name}{caption}

Possible syntax-patterns for "\newtheoremWithMacro"are the same:

  \newtheoremWithMacro{env_name}[numbered_like]{caption}
  \newtheoremWithMacro{env_name}{caption}[within]
  \newtheoremWithMacro{env_name}{caption}

Syntax-patterns for a theorem "foo" defined in terms of
"\newtheorem" are:

\begin{foo}
This is my theorem
\end{foo}

\begin{foo}[Galois]
This is my theorem
\end{foo}

Syntax-patterns for a theorem "bar" defined in terms of
"\newtheoremWithMacro" are:

\begin{bar}
This is my theorem
\end{bar]

\begin{bar}[]
This is my theorem
\end{bar}
(But this is obsolete and yields the same as above!)

\begin{bar}[][Galois]
This is my theorem
\end{bar}

\begin{bar}[\macro][Galois]
This is my theorem
\end{bar}

\begin{bar}[\macro]
This is my theorem
\end{bar}

The last two variants also define "\macro" to restate the
theorem.

Restrictions
------------

A noteworthy restriction is:
"Verbatim" is broken within the body of environments that are
defined in terms of "\newtheoremWithMacro".

This is because amslatex' "\collect@body" will read ahead and
tokenize the whole stuff before catcode-changes of verbatim-
environment get into effect.

E.g., the following code does work as expected...

  \newtheorem{foo}...
  ...
  \begin{foo}
  \begin{verbatim}
  \TeX is funny.
  \end{verbatim}
  \end{foo}

...while this one does not:

  \newtheoremWithMacro{bar}...
  ...
  \begin{bar}
  \begin{verbatim}
  \TeX is funny.
  \end{verbatim}
  \end{bar}


Usage-Example:
--------------

\documentclass{article}
\usepackage{theomac}
\usepackage{hyperref}

\newtheorem{theorem}{Theorems Caption}[section]
\newtheoremWithMacro{Mytheorem}[theorem]{My Theorem}
%\newtheoremWithMacro{Mytheorem}{My Theorem}[section]
\makeatletter
\g@addto@macro\thetheorem{\ifrestated, restated\fi}%
%\g@addto@macro\theMytheorem{\ifrestated, restated\fi}%
\makeatother

\begin{document}

\section{test}

\begin{Mytheorem}[\firsttheorem][Not Galois]%
This is my first theorem.
\label{firsttheoremsfirstocurrence}%
\end{Mytheorem}

Let's restate the first theorem: \firsttheorem

\begin{Mytheorem}[\secondtheorem][Not Galois]
This is my second theorem.
\end{Mytheorem}

\begin{Mytheorem}
This is my third theorem.
\end{Mytheorem}

Let's restate the first theorem: \firsttheorem

\newpage

\section{test}

\begin{Mytheorem}
This is my fourth theorem.
\end{Mytheorem}

Let's restate the first theorem: \firsttheorem

\begin{Mytheorem}
This is my fifth theorem.
\end{Mytheorem}

This is page-reference to the page where my first theorem
occurred the first time: \pageref{firsttheoremsfirstocurrence}%

This is reference to the number of my first theorem---the
hyperlink goes to the place where the theorem was stated
the first time: \ref{firsttheoremsfirstocurrence}%

\end{document}

%
%% End of file `theomac.sty'.
