37

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?

3
  • 3
    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. Oct 31, 2011 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.
    – user10274
    Feb 3, 2012 at 1:14
  • 1
    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.
    – J..y B..y
    Feb 10, 2012 at 14:08

4 Answers 4

31

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.

21
  • 1
    Your codes work good! One problem: If there is a paragraph break, it produces an error.
    – Chang
    Oct 31, 2011 at 16:50
  • 3
    @Chang My fault, now it's fixed: a \long was missing.
    – egreg
    Oct 31, 2011 at 16:54
  • 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, 2012 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, 2013 at 16:03
  • 1
    @egreg On my desktop it works fine, but on overleaf my coauthor have troubles: l.170 \globtoksblk \prooftoks{1000} The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), type I' and the correct spelling (e.g., I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.` The version of the compiler is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian).
    – tobiasBora
    Apr 10, 2019 at 22:26
2

I was recently made aware of the following package, written by Pierre Senellart, which seems to be doing exactly what you're looking for:

apxproof: https://ctan.org/pkg/apxproof

The package makes it easier to write articles where proofs and other material are deferred to the appendix.
The appendix material is written in the LaTeX code along with the main text which it naturally complements, and it is automatically deferred.
The package can automatically send proofs to the appendix, can repeat in the appendix the theorem environments stated in the main text, can section the appendix automatically based on the sectioning of the main text, and supports a separate bibliography for the appendix material.

1

tcolorbox offers some recording commands which allow to save boxes contents to be recovered later on.

savelowerto will save text after tcblower to be saved in an auxiliary file which can be read with \tcbinputrecords.

Command \tcbstartrecording and \tcbstoprecording define when savelowerto is to be applied.

More information about these commands can be found in recording section from tcolorbox documentation.

Following code shows a little example:

\documentclass{article}

\usepackage{lipsum}
\usepackage[most]{tcolorbox}

\NewTColorBox[auto counter, number within=section]{theorem}{+O{}}{ %
enhanced,
colframe=green!20!black,
colback=yellow!10!white,
coltitle=green!40!black,
fonttitle=\bfseries,
title={Theorem~\thetcbcounter:},
label={theorem@\thetcbcounter},
attach title to upper=\quad,
after upper={\par\hfill\textcolor{green!40!black} %
{\itshape Proof on page~\pageref{proof@\thetcbcounter}}},
lowerbox=ignored,
savelowerto=theorems/theorem-\thetcbcounter.tex,
record={\string\proof{\thetcbcounter}{theorems/theorem-\thetcbcounter.tex}},
#1
}

\NewTotalTColorBox{\proof}{mm}{ %
enhanced,
colframe=red!20!black,colback=yellow!10!white,
coltitle=red!40!black,
fonttitle=\bfseries,
title={Proof of Theorem~\ref{theorem@#1} on page~\pageref{theorem@#1}:},
phantomlabel={proof@#1},
attach title to upper=\par,
}{\input{#2}}

\tcbset{no proof/.style={no recording,after upper=}}

\begin{document}
\section{Some theorems}

\tcbstartrecording

\lipsum[2]
\begin{theorem}
First theorem:
1+2=3
\tcblower
Proof is obvious
\end{theorem}


\begin{theorem}[no proof]
This theorem has no proof to be printed
\end{theorem}


\begin{theorem}
Another theorem with proof
\tcblower
This is the proof of third theorem
\end{theorem}

\lipsum[1-3]
\tcbstoprecording

\section{Some theorems can be prooved}

\tcbinputrecords

\end{document}

enter image description here

Although previous example shows decorated boxes, blankest style option suppresses all colors, borders and margins around text and even tcolorbox-theorems looks like regular theorems as can be seen in Restate theorem in separate file

0

I wrote an answer here Ultimate proof at the end/in appendix: different sections, with references to proof/theorem, restate, and synctex where I provide a mini-library super easy to use, in order to put theorems in different places, mix them with lemmas, make reference to them, restate them, duplicate the proof in appendix and in main (nice for synctex), add comments in the appendix, link between the theorem and the proofs...

I won't write the code again here to avoid duplicata of answers, so I'll put a picture: enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .