Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Can I place all proofs to theorems in appendix automatically? Usually proofs are written in the LaTeX codes immediately after the theorems. Without physically moving proofs to appendix, how can I place them in appendix automatically? Is there a package?

share|improve this question
2  
I am not a 100% sure this can do only proofs to appendix, hence a comment and not an answer, but have a look at thrmappendix. –  Roelof Spijker Oct 31 '11 at 15:28
    
If all your proofs should go to the appendix, why not provide them there? You can write them anywhere, and it gives you more freedom in the order you can present them. –  Marc van Dongen Feb 3 '12 at 1:14
    
Marc: sometime you prefer the proofs to be inline, but have to move them to the appendix for a conference submission. In this case one would look for an automatized way to do so, in good computer scientist. –  Jeremy Feb 10 '12 at 14:08
add comment

1 Answer

up vote 22 down vote accepted

This provides for at most 1000 proofs, which should be enough. After declaring a theorem, put \fixstatement after it. Using \proofatend...\endproofatend allows for setting proofs also in the document (say the main one).

\documentclass[a4paper]{article}
\usepackage{etex,etoolbox}
\usepackage{amsthm}

\makeatletter
\providecommand{\@fourthoffour}[4]{#4}
% We define an addition for the theorem-like environments; when
% \newtheorem{thm}{Theorem} is declared, the macro \thm expands
% to {...}{...}{...}{Theorem} and with \@fourthoffour we access
% to it; then we make available \@currentlabel (the theorem number)
% also outside the environment.  
\def\fixstatement#1{%
  \AtEndEnvironment{#1}{%
    \xdef\pat@label{\expandafter\expandafter\expandafter
      \@fourthoffour\csname#1\endcsname\space\@currentlabel}}}

% We allocate a block of 1000 token registers; in this way \prooftoks
% is 1000 and we can access the following registers of the block by
% \prooftoks+n (0<n<1000); we'll use a dedicated counter for it
% that is stepped at every proof
\globtoksblk\prooftoks{1000}
\newcounter{proofcount}

% We gather the contents of the proof as argument to \proofatend
% and then we store
% "\begin{proof}[Proof of <theoremname> <theoremnumber>]#1\end{proof}"
% in the next token register of the allocated block
\long\def\proofatend#1\endproofatend{%
  \edef\next{\noexpand\begin{proof}[Proof of \pat@label]}%
  \toks\numexpr\prooftoks+\value{proofcount}\relax=\expandafter{\next#1\end{proof}}
  \stepcounter{proofcount}}

% \printproofs simply loops over the used token registers of the
% block, freeing their contents
\def\printproofs{%
  \count@=\z@
  \loop
    \the\toks\numexpr\prooftoks+\count@\relax
    \ifnum\count@<\value{proofcount}%
    \advance\count@\@ne
  \repeat}
\makeatother

% Here starts the example, with two theorem declarations    
\newtheorem{thm}{Theorem}
\newtheorem{lem}[thm]{Lemma}
\fixstatement{thm}
\fixstatement{lem}


\begin{document}
\begin{lem}\label{addition}
$1+1=2$
\end{lem}
\proofatend
It's quite clear.
\endproofatend

\begin{thm}
$1+2=3$
\end{thm}
\proofatend
Obvious from lemma \ref{addition}.
\endproofatend

\section*{Proofs}

\printproofs

\end{document}

Each proof is stored in a token register and those registers are unloaded by \printproofs. Long proofs or a large number of them may cause memory problems.


New version

Stimulated by Alfredo Hernández's comment requesting a fix for the case when thmtools is used, here's a new version that has also an addition. The command \fixstatement now has an optional argument to state the “prefix” for the proof. This may be important because in some languages one has to change the construction; in Italian we should say “Dimostrazione del Teorema” but “Dimostrazione della Proposizione”.

I provide an example where both \newtheorem and \declaretheorem are used; of course one should stick with one or the other. The example uses thmtools, but it's not required (and \declaretheorem can't be used, of course, if one doesn't load it).

The second change is that environ is used, so proofatend has become an environment, with a more coherent syntax.

\documentclass[a4paper]{article}
\usepackage{etex,etoolbox}
\usepackage{amsthm,amssymb}
\usepackage{thmtools}
\usepackage{environ}

\makeatletter
\providecommand{\@fourthoffour}[4]{#4}
% We define an addition for the theorem-like environments; when
% \newtheorem{thm}{Theorem} is declared, the macro \thm expands
% to {...}{...}{...}{Theorem} and with \@fourthoffour we access
% to it; then we make available \@currentlabel (the theorem number)
% also outside the environment.  
\newcommand\fixstatement[2][\proofname\space of]{%
  \ifcsname thmt@original@#2\endcsname
    % the theorem has been declared with \declaretheorem
    \AtEndEnvironment{#2}{%
      \xdef\pat@label{\expandafter\expandafter\expandafter
        \@fourthoffour\csname thmt@original@#2\endcsname\space\@currentlabel}%
      \xdef\pat@proofof{\@nameuse{pat@proofof@#2}}%
    }%
  \else
    % the theorem has been declared with \newtheorem
    \AtEndEnvironment{#2}{%
      \xdef\pat@label{\expandafter\expandafter\expandafter
        \@fourthoffour\csname #1\endcsname\space\@currentlabel}%
      \xdef\pat@proofof{\@nameuse{pat@proofof@#2}}%
    }%
  \fi
  \@namedef{pat@proofof@#2}{#1}%
}

% We allocate a block of 1000 token registers; in this way \prooftoks
% is 1000 and we can access the following registers of the block by
% \prooftoks+n (0<n<1000); we'll use a dedicated counter for it
% that is stepped at every proof
\globtoksblk\prooftoks{1000}
\newcounter{proofcount}

% We gather the contents of the proof as argument to \proofatend
% and then we store
% "\begin{proof}[Proof of <theoremname> <theoremnumber>]#1\end{proof}"
% in the next token register of the allocated block
\NewEnviron{proofatend}{%
  \edef\next{%
    \noexpand\begin{proof}[\pat@proofof\space\pat@label]%
    \unexpanded\expandafter{\BODY}}%
  \global\toks\numexpr\prooftoks+\value{proofcount}\relax=\expandafter{\next\end{proof}}
  \stepcounter{proofcount}}

% \printproofs simply loops over the used token registers of the
% block, freeing their contents
\def\printproofs{%
  \count@=\z@
  \loop
    \the\toks\numexpr\prooftoks+\count@\relax
    \ifnum\count@<\value{proofcount}%
    \advance\count@\@ne
  \repeat}
\makeatother

% Here starts the example, with two theorem declarations
\declaretheorem[style=plain,name=Theorem,qed=$\square$,numberwithin=section]{thm}
%\declaretheorem[style=plain,name=Lemma,qed=$\square$,numberlike=thm]{lem}
%\newtheorem{thm}{Theorem}
\newtheorem{lem}[thm]{Lemma}
\fixstatement{thm}
\fixstatement[Demonstration of]{lem}


\begin{document}
\begin{lem}\label{addition}
$1+1=2$
\end{lem}
\begin{proofatend}
It's quite clear.
\end{proofatend}

\begin{thm}
$1+2=3$
\end{thm}
\begin{proofatend}
Obvious from lemma \ref{addition}.
\end{proofatend}

\section*{Proofs}

\printproofs

\end{document}

enter image description here

In the picture we have 0.1 and 0.2 because no \section command has been issued. Of course this will not happen in a normal document.

share|improve this answer
1  
Your codes work good! One problem: If there is a paragraph break, it produces an error. –  FEQ Oct 31 '11 at 16:50
2  
@Chang My fault, now it's fixed: a \long was missing. –  egreg Oct 31 '11 at 16:54
    
Under pdfTeX 3.1415926-1.40.10-2.2 (TeX Live 2009/Debian), your code generate on my machine the following error: ! Undefined control sequence. \fixstatement #1->\AtEndEnvironment {#1}{\xdef \pat@label {\expandafter \exp... l.51 \fixstatement{thm} –  Jeremy Feb 16 '12 at 18:09
1  
@Jeremy The problem is that your distribution has an old version of etoolbox, which doesn't provide \AtEndEnvironment. TeX Live 2009 is really outdated. –  egreg Feb 16 '12 at 18:18
2  
@AlfredoHernández I've uploaded a new version that has a few improvements and also fixes the problem with \declaretheorem. –  egreg Oct 5 '13 at 16:03
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.