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?
4 Answers
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}

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.
- 1,121,712
-
1Your codes work good! One problem: If there is a paragraph break, it produces an error. – Chang Oct 31 '11 at 16:50
-
3
-
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}
– J..y B..y 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 -
-
@egreg Your code generate the following error on my machine. Do you have an idea what is it about?
! Missing control sequence inserted. <inserted text> \inaccessible l.497 \printproofs– Feb 22 '13 at 12:12 -
@egreg I'm using the thmtools package and I set my theorem environment as
\declaretheorem[style=plain,name=Theorem,qed=$\square$,numberwithin=section]{thm}. The code you provided doesn't recognise the label and it just says "Proof of 1.1", for instance, instead of "Proof of Theorem 1.1". Any way to fix that? – Alfredo Hernández Oct 05 '13 at 15:16 -
@AlfredoHernández The fix is to type
\@fourthoffour\csname thmt@original@#1\endcsnameinstead of\@fourthoffour\csname#1\endcsname. Of course you have to declare all theorems with\declaretheorem. – egreg Oct 05 '13 at 15:28 -
2@AlfredoHernández I've uploaded a new version that has a few improvements and also fixes the problem with
\declaretheorem. – egreg Oct 05 '13 at 16:03 -
It works perfectly, also using proofatend as an environment makes a lot of sense, it's easier to remember. Thanks. – Alfredo Hernández Oct 05 '13 at 16:44
-
@egreg How can I change the font specification of 'Proof of Theorem 1.1'? Can I make it bold-face? – Chang Nov 06 '14 at 22:46
-
@FEQ This is based on the
proofenvironment ofamsthm; load also\usepackage{xpatch}and add\xpatchcmd{\proof}{\itshape}{\bfseries}{}{}– egreg Nov 06 '14 at 22:55 -
This looks like a good solution to the question. Is there a simple way to tweak it so that also the theorem/lemma is presented before the proof? – Alex Monras Apr 05 '16 at 13:09
-
-
@egreg - this code breaks if you don't set the option
name=...indeclaretheorem. I didn't have them set because my theorem environments were automatically showing with the correct name (i.e.theoremenvironment was being captioned Theorem) and it took me quite a while to find this issue. – Myridium Jan 15 '18 at 08:18 -
@Myridium Actually I don't really like the automatic feature of
thmtools: playing with\MakeUppercasemay be dangerous. – egreg Jan 15 '18 at 08:29 -
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'), typeI' 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 '19 at 22:26 -
@tobiasBora I can compile it with TL 2017 (the version used by Overleaf). – egreg Apr 10 '19 at 22:32
-
@egreg Oh sorry, I'm stupid some include was removed, now it works, thanks! Also, I'm wondering, do you have a solution to have several
\printproofs, like\printproofsSectionImportant,\printproofsSectionSimple... In order to display proofs in different places? (of course, each\printproofwould be associated to a different proof environment, or a different theorem environment. – tobiasBora Apr 10 '19 at 23:07 -
@tobiasBora That's more difficult. I don't think I'd take the same approach nowadays, as there are better methods available. If you want, make a new question with some more details about how you'd choose how to distinguish the various proofs. Link to this question for reference. – egreg Apr 10 '19 at 23:35
-
@egreg sure, here is it https://tex.stackexchange.com/questions/484183/ultimate-proof-at-the-end-different-sections-with-references-to-proof-theorem. What method are you refering to? – tobiasBora Apr 10 '19 at 23:52
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.
- 185
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}
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
- 136,588
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:

- 8,684

thrmappendix. – Roelof Spijker Oct 31 '11 at 15:28