Update: I decided to create a real library out of this project. I'm now uploading it on CTAN, but meanwhile you can find the whole project here https://github.com/leo-colisson/proofAtTheEnd/
The syntax is now an environment, which is clearer I think. To use it in your project, first download it:
cd <your project>
repopratend="https://raw.githubusercontent.com/leo-colisson/proofAtTheEnd"
wget ${repopratend}/master/proofAtTheEnd.sty
wget ${repopratend}/master/xparse.sty
load it in your project:
\usepackage{proofAtTheEnd}
and use it with:
\begin{theoremEnd}{thm}[Yes I can have a title]
\label{thm:ilikelabels}
Creating a new theorem is easy
\end{theoremEnd}
\begin{proofEnd}
You want a proof? Here is it!
\end{proofEnd}
And put where you want to write the proofs the code
\printProofs
You can do much more with it, see the documentation here : https://github.com/leo-colisson/proofAtTheEnd/ !
First version:
After spending quite a long time to find a nice solution, I came up to do my own "mini-library". The code is not really a one line solution, but it's really customizable, easy to change, it does not involve complex latex code, but it works greatly! I manage to solve all the tasks above, even more (I added the possibility to add text in the appendix appart from theorem, restate theorem before the main theorem...), and it's super easy to use! The only "problem" is that when you click on a proof in appendix, you are redirected to an input file that is created by the script instead of staying in the main file... But you can with one word write the proof both in appendix and in the main paper (where synctex works), which is practical when you write the proof ;-) If you would like to have something that is not present here, let me know!
But pictures are better than words:

And the code to produce a theorem looks like (change thm
into the name of the environment you want to use, like lemma
...):
\theoremProofEnd{thm}[Yes I can have a title]{\label{thm:ilikelabels}
If $1+1=2$, then $2 = 1+1$.
}{
Come on, you can do it!
}
and to print the proofs, you just write wherever you want
\printProofs
Now, you can also add styles to the theorem to have more power. For example, if you want to restate the theorem in the appendix, and put it in another section of the appendix (you will create another "category" or theorems), you just need to write
\theoremProofEnd[restate, category=myimportanttheorems]{thm}[Quantum Yao's XOR Lemma]{
The Yao's XOR Lemma for one-rounds protocols with quantum adversary holds.
}{
If you have the proof, let me know!
}
And to load the "important theorems" in another section:
\section{Proofs that are important}
\printProofs[myimportanttheorems]
You can easily change the defaults, or even define you own style using something like:
\pgfkeys{/prAtEnd/custom defaults/.style={
category=greattheorem
}
}
You can also add comments only in appendix (or use option both
if you want the comment in both the appendix and the main text) with
\textInAppendix{See, I am a simple text in appendix only.}
Finally, you can also restate a theorem before, by using:
\section{Section with restate before theorem}
\theoremProofEndRestateBefore{thm}[Title]{laterrestatable}{
This theorem has been introduced before the real definition.
}
\section{Section with late theorems}
\theoremProofEnd[restated before]{thm}{laterrestatable}{
To state a theorem before the initial definition, use theoremProofEndRestateBefore where you first want to state the theorem, with a unique name in the second mandatory argument, and when you want to insert the theorem for the second time, use the usual theoremProofEnd command with the same unique name as before in place of the theorem definition.
}
The list of options for theorems are (you can add no
in front of most options to negate them):
category=mystupidtheorems
to change the group of the theorem (each group can be included in a different place)
proof here
: if you want the proof to be written in the main text
proof end
: if you want the proof in the appendix
restate
: if you want to restate the theorem in the appendix
link to proof
: to add a link after the theorem to the proof
both
: (only with \textInAppendix
) to put a text also in the main body
default
: options by defaults
custom defaults
: to overwrite the defaults
normal
: theorem without proof in the appendix
debug
: alias for proof here
, useful to have a copy of the proof in the main paper to use synctex to navigate between paper and proof easily.
stared
: Just displays 'Proof' in the appendix, with a link to the theorem
no link to theorem
: Just displays 'Proof' in the appendix, with no link to the theorem
text link
: Text used in the link to the proof, defaults to See \hyperref[proof:prAtEnd\thecounterAllProofEnd]{proof} on page~\pageref{proof:prAtEnd\thecounterAllProofEnd}
text proof
: Text used in the title of the proof, defaults to {Proof of \string\autoref{thm:prAtEnd\thecounterAllProofEnd}}
Enjoy!
A full example:
\usepackage{verbatim}
%% TODO idea: allow sketch proofs with full proof in Appendix
%% TODO: restate before
\usepackage{mathtools}
\usepackage{amssymb, amsthm, amsmath, thm-restate}
\usepackage{thmtools} %%
\usepackage{hyperref}
\usepackage{etoolbox}
\newtheorem{thm}{Theorem}[section]
\newtheorem*{thm*}{Theorem}
\providecommand*\thmautorefname{Theorem}
\newtheorem{corollary}[thm]{Corollary}
\newtheorem*{corollary*}{Corollary}
\providecommand*\corollaryautorefname{Corollary}
\newtheorem{lemma}[thm]{Lemma}
\newtheorem*{lemma*}{Lemma}
\providecommand*\lemmaautorefname{Lemma}
\usepackage{catchfile}
\newwrite\appendwrite
\newcommand*\appendtofile[2]{%
\begingroup
\IfFileExists{#1}%
{\CatchFileDef{\filecontent}{#1}{\catcode`\\=12 \endlinechar=`^^J\catcode\endlinechar=12\relax}}% keep existing end-of-lines
{\let\filecontent\empty}%
\immediate\openout\appendwrite=#1\relax
\immediate\write\appendwrite{\detokenize\expandafter{\filecontent}#2}%
\immediate\closeout\appendwrite
\endgroup
}
%% This functions takes one input: the category (without .tex),
%% If it's the first time we write in this
%% category file, it "blanks" it.
\def\prefixPrAtEndFiles{pratend}
\newcommand*\eraseIfNeeded[1]{
% A macro 'pratendmacrocat{category}' is created to check
% if it's the first time we write in this category file.
\edef\macroname{pratendmacrocat#1}
\ifcsdef{\macroname}{% The macro exists, nothing to do
}{ % The macro does not exists, create it, and empty the file
\expandafter\def\csname \macroname\endcsname{true}
\immediate\openout\appendwrite=\prefixPrAtEndFiles#1.tex
\immediate\write\appendwrite{}
\immediate\closeout\appendwrite
}
}
\usepackage{pgfkeys}
\newif\ifproofhere
\newif\ifproofend
\newif\ifrestatethm
\newif\iflinktoproof
\newif\ifboth
\newif\ifallattheend
\newif\ifrestatedbefore
\pgfkeys{
/prAtEnd/.cd, %% Proof at end will be the main path
%% Category of the proof (if you want to put proofs
%% at several places), can be anything
category/.initial=defaultcategory,
category/.store in=\category,
category/.get=\category,
%% Display the proof in the main part
proof here/.is if=proofhere,
no proof here/.style={proof here=false}, % alias
%% Display the proof when using \printProofs
proof end/.is if=proofend,
no proof end/.style={proof end=false},
%% Restate the theorem when using \printProofs
restate/.is if=restatethm,
no restate/.style={restate=false},
%% Put a link to the proof after the theorem
link to proof/.is if=linktoproof,
no link to proof/.style={link to proof=false},
%% Put the theorem and proof only in appendix
all end/.is if=allattheend,
no all end/.style={all end=false},
%% Text of link
text link/.code={\def\pratendtextlink{#1}},
%% Text of proof. Make sure also to "\renewcommand*{\proofname}{Name of the proof}"
%% to make sure the proof for normal theorems are changed
text proof/.code={\def\pratendtextproof{#1}},
%% Custom restate command
restate command/.code={\edef\pratendcustomrestate{#1}},
%% (Re)stated before
%% If the theorems has been stated before, then we just need to put the restate command in
%% place of the argument, and we set this value to true:
restated before/.is if=restatedbefore,
no restated before/.style={restated before=false},
%% In star version, we don't want
%% Put the text (defined only for \textInAppendix) in both the
%% current location and in appendix
both/.is if=both,
no both/.style={both=false},
%%%% Defaults, alias, and styles
defaults/.style={
no proof here,
no all end,
proof end,
link to proof,
no restate,
category=defaultcategory,
no both,
text link={See \hyperref[proof:prAtEnd\pratendcountercurrent]{proof} on page~\pageref{proof:prAtEnd\pratendcountercurrent}},
text proof={Proof of \string\autoref{thm:prAtEnd\pratendcountercurrent}},
restate command=pratenddummymacro,
},
custom defaults/.style={
%% you can put in this style any overwrite of the defaults
},
normal/.style={
proof here,
no all end,
no proof end,
no link to proof,
no restate,
no both,
},
debug/.style={
proof here
},
no link to theorem/.style={ % Remove the link to the theorem
text proof={\proofname},
},
stared/.style={ % Remove
text proof={\string\mbox{\string\hyperref[thm:prAtEnd\pratendcountercurrent]{\proofname}}},
},
}
\usepackage{xparse}
\newcounter{counterAllProofEnd}
\stepcounter{counterAllProofEnd}
\NewDocumentCommand\theoremProofEndRestateBefore{mO{}m+m}{
%% This function is only for the specific case when you want to restate the theorem
%% before the actual definition.
%% - The first (mandatory) argument is for the type of theorem
%% - The second (facultative) argument is for the title of the theorem
%% - The third (mandatory) argument is a UNIQUE name that will be used to store the counter
%% that you need to give back to theoremProofEnd in place of the theorem
%% - The fourth (mandatory) argument is the theorem
\edef\currcounterval{\roman{counterAllProofEnd}}
\edef\temprest{\noexpand\begin{restatable*}[#2]{#1}{prAtEndRestate\currcounterval}}%
\expandafter\edef\csname #3\endcsname{\currcounterval}%
\temprest%
\label{thm:prAtEnd\currcounterval}%
\show\currcounterval%
#4%
\end{restatable*}%
\stepcounter{counterAllProofEnd}%
}
\NewDocumentCommand\theoremProofEnd{O{}mO{}+m+g}{
% The first facultative argument will be the options: type of proof you want, the file to which you want to write...
% The first mandatory option is the type of the theorem (thm,lemma,...)
% The second facultative argument will be the title
% the second mandatory option is the theorem (will \label inside eventually)
% the last mandatory option is the proof
\pgfkeys{%
/prAtEnd/.cd,
defaults,
custom defaults,
#1
}%
\edef\temprest{\noexpand\begin{restatable}[#3]{#2}{prAtEndRestate\roman{counterAllProofEnd}}}%
\unless\ifallattheend%
\ifrestatedbefore% Restate the theorem if it was stated before
\edef\pratendcountercurrent{\csname #4\endcsname} % Store the current (alpha value of the) counter
\csname prAtEndRestate\pratendcountercurrent\endcsname % Restate the theorem
\show\laterrestatable
\fi%
\unless\ifrestatedbefore% Otherwise just state the theorem in a restatable data
\temprest%
\label{thm:prAtEnd\roman{counterAllProofEnd}}%
#4%
\end{restatable}%
\edef\pratendcountercurrent{\roman{counterAllProofEnd}} % Store the current (alpha value of the) counter
\fi%
\show\pratendcountercurrent%
\expandafter\edef\csname \pratendcustomrestate\endcsname{\noexpand\csname prAtEndRestate\pratendcountercurrent\endcsname}%
\IfNoValueTF{#5}{}{%
\iflinktoproof%
\pratendtextlink{}%
\fi%
\ifproofhere%
\begin{proof}%
#5%
\end{proof}%
\fi%
}%
\fi%
\eraseIfNeeded{\category}%
\unless\ifallattheend%
\ifrestatethm%
\appendtofile{\prefixPrAtEndFiles\category.tex}{\string\prAtEndRestate\pratendcountercurrent*}
\fi%
\fi%
\ifallattheend%
\appendtofile{\prefixPrAtEndFiles\category.tex}{\string\begin{restatable}[#3]{#2}{prAtEndRestate\pratendcountercurrent}\string\label{thm:prAtEnd\pratendcountercurrent}\detokenize{#4}\string\end{restatable}}%
\fi%
\IfNoValueTF{#5}{}{%
\ifproofend%
\appendtofile{\prefixPrAtEndFiles\category.tex}{\string\begin{proof}[\pratendtextproof]\string\phantomsection\string\label{proof:prAtEnd\pratendcountercurrent}\detokenize{#5}\string\end{proof}}%
\fi%
}%
\stepcounter{counterAllProofEnd}%
}
\NewDocumentCommand\textInAppendix{O{}+m}{
% Use it to put normal text in Appendix.
\pgfkeys{
/prAtEnd/.cd,
defaults,
#1
}
\ifboth%
#2
\fi%
\eraseIfNeeded{\category}
\appendtofile{\prefixPrAtEndFiles\category.tex}{\detokenize{#2}}%
}
\NewDocumentCommand\printProofs{O{defaultcategory}}{
\input{\prefixPrAtEndFiles#1.tex}
}
\newwrite\tempfile
%%% You can easily modify the defaults:
% \pgfkeys{/prAtEnd/custom defaults/.style={
% category=greattheorem
% }
% }
%%% Or create new styles to apply:
% \pgfkeys{/prAtEnd/great category/.style={
% category=greattheorem
% }
% }
\begin{document}
\section{First section}
\theoremProofEnd[]{thm}[Yes I can have a title]{\label{thm:ilikelabels}
Simplicity is luxury, I am a default theorem.
}{
Let's be simple
}
And I can refer to my theorems using classic labels, like in \autoref{thm:ilikelabels}.
\theoremProofEnd[category=greattheorem]{thm}[Different categories]{
You can also create several categories, and put the proofs in different sections.
}{ %%
See, I am in another section! And I refer to \autoref{thm:ilikelabels} even in the proof.
}
\theoremProofEnd[restate]{thm}[I am restatable]{ %% Theorem
I am a restatable theorem, go in Appendix you will see ;-)
}{ %% Proof
I am a proof of a restatable theorem.
}
\theoremProofEnd[normal]{thm}{
You can easily turn it back into a normal theorem!
}{ %% Proof
And keep the proof with you!
}
You can also put comments that appear only in the appendix.
\textInAppendix{See, I am a simple comments with math $\delta = b^2-ac$ and references \autoref{thm:mytheoremattheend}.}
\textInAppendix[both]{Or that appears in both and with references \autoref{thm:mytheoremattheend}!}
\theoremProofEnd[proof here]{thm}{
And you can duplicate the proof, here AND in appendix ;)
}{ %% Proof
I am a proof that is everywhere, practical if you want to use synctex while you write the proof ;)
}
\theoremProofEnd{lemma}{
You can mix it with lemmas... Or any other theorem-like environment easily!
}{ %%
See, I'm the proof of a lemma!
}
And also you can put some proofs only at the end, like for \autoref{thm:mytheoremattheend}!
\theoremProofEnd[all end]{thm}{\label{thm:mytheoremattheend}
$\delta = b^2-4ac$
You can also put theorems only at the end.
}{ %%
See, I'm the proof of a lemma that is only at the end!.
}
You can also remove the link to the theorem:
\theoremProofEnd[no link to theorem, restate]{thm}{
I don't like links in proofs.
}{ %%
Yes, I like being lost, but not too lost, so I prefer to restate as well!
}
Or keep the link, but remove the reference (practical for stared versions):
\theoremProofEnd[stared]{thm*}{
I don't like numbers.
}{ %%
Yes, I hate numbers, but I like links.
}
\pgfkeys{/prAtEnd/french/.style={
text link={Voir \hyperref[proof:prAtEnd\pratendcountercurrent]{preuve} à la page~\pageref{proof:prAtEnd\pratendcountercurrent}},
text proof={Preuve de \string\autoref{thm:prAtEnd\pratendcountercurrent}}
}
}
\theoremProofEnd[french]{thm}{ %% See how "french" is defined just above
Change the text/languages of the link: Il est même possible de changer la langue du texte du lien!
}{
Si c'est pas beau ;)
}
\theoremProofEnd[]{thm}[Yes I can have no proof]{
Proof is useless. You can do do it.
}
\theoremProofEnd[restate command=mymanualrestate]{thm}[Manual restate]{
A theorem can be manually restated
}{
Use restate command for that! (see \autoref{sec:manualrestate} for an example)
}
\section{Section with restate before theorem}\label{sec:restatebefore}
\theoremProofEndRestateBefore{thm}[Title]{laterrestatable}{\label{thm:laterrestatable}
This theorem has been introduced in \autoref{sec:restatebefore} before the real definition, but the real definition is in \autoref{sec:restateafter}, more precisely here: \autoref{thm:laterrestatable}.
}
\theoremProofEnd{thm}{
And this is a normal theorem
}{
With a normal proof
}
\section{Section with late theorems}\label{sec:restateafter}
\theoremProofEnd[restated before]{thm}{laterrestatable}{
To state a theorem before the initial definition, use theoremProofEndRestateBefore where you first want to state the theorem, with a unique name in the second mandatory argument, and when you want to insert the theorem for the second time, use the usual theoremProofEnd command with the same unique name as before in place of the theorem definition.
}
\section{Section with standard proofs}
% \verbatiminput{defaultcategory}
\printProofs
\section{Section with important proofs only}
\printProofs[greattheorem]
\section{Section with manual restate}\label{sec:manualrestate}
I like to manually restate theorems:
\mymanualrestate*
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: