# Ultimate proof at the end/in appendix: different sections, with references to proof/theorem, restate, and synctex

I saw lot's of different posts/answers to put the proof at the end, but none of them fully convince me. Indeed, I'd like to have the following:

1. a way to put the proofs in different sections: some proofs may stay on place, some proofs may go to appendix in section A, some may to to appendix in section B... Ideally, creating a new "kind" of proof to put it somewhere else should be easy.
2. with a way to link automatically the proof and the theorem: at the end of the theorem, I'd like a sentence like See \autoref{proof:myproof} page XX, and at the end of the proof I'd like a sentece like See \autoref{thm:mytheorem} page XX (with links enabled of course), and moving a proof from one "kind" to another should be easy as well.
3. I want to be able to treat lemma and theorems together, I don't want to treat them as two different objects and push them at two different places
4. I want to be able to give them a name, and use \autoref{lem:mylemma} to refer to them (with link of course) across the document
5. they should behave "nicely" with counters from reals theorems/proofs. Indeed, some people in the team may still use the commands given by \newtheorem defined with amsthm/thmtools, and I don't want to disturb them, and therefore a "custom" theorem-proof-at-the-end after a "real" theorem should have a number next to the one of the theorem. Usually, I like my lemma and theorem to share the same counter that derives from the section one.
6. I use a lot the synctex functions that let me go from a point to the PDF to the corresponding line in my .tex, and reversely, that goes from a line in my .tex to the corresponding line in my .pdf, so I would love to be able to use synctex on the proofs as well. If it's not possible when the proof is at the end, at least I'd like to be able to use synctex when I'm writting the proof, i.e. when it is not yet pushed in the appendix. But a synctex that works all the time would be awesome!
7. Eventually, an option to be able to restate the theorem in the appendix before the proof would be interesting.

Let me know if you have a nice solution! I'm trying to adapt a method based on tcbox, but it's a bit hard to make synctex work with it. Also, if you propose a solution, could you please indicate if some items are not respected?

Thanks a lot!

MWE: I started with this code. It works pretty well, but as it it's not trivial to create new "proof kind" easily. I started from How to place all proofs automatically in appendix?

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

\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.
\newcounter{counttheorems}

\newcommand\fixstatement[2][\proofname\space of]{%
\ifcsname thmt@original@#2\endcsname
% the theorem has been declared with \declaretheorem
\AtEndEnvironment{#2}{%
\xdef\pat@uniqlabel{\thecounttheorems}%
\xdef\pat@label{\expandafter\expandafter\expandafter
\@fourthoffour\csname thmt@original@#2\endcsname\space\@currentlabel}%
\xdef\pat@proofof{\@nameuse{pat@proofof@#2}}%
\expandafter\label{thm_uniq:\pat@uniqlabel}
}%
\else
% the theorem has been declared with \newtheorem
\AtEndEnvironment{#2}{%
\xdef\pat@uniqlabel{\thecounttheorems}%
\xdef\pat@label{\expandafter\expandafter\expandafter
\@fourthoffour\csname #1\endcsname\space\@currentlabel}%
\xdef\pat@proofof{\@nameuse{pat@proofof@#2}}%
\expandafter\label{thm_uniq:\pat@uniqlabel}
}%
\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}{%
You can find \hyperlink{proofatend:\pat@uniqlabel}{the proof} at the end of the paper.
\edef\next{%
% \noexpand\begin{proof}[\pat@proofof\space\pat@label]%
\noexpand\begin{proof}[\pat@proofof\space\noexpand\autoref{thm_uniq:\pat@uniqlabel}]%
\noexpand\hypertarget{proofatend:\pat@uniqlabel}
\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}%
\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}
\section{My section}
\begin{lem}\label{lem:myfirstlemma}
This should be Lemma 1.1 $1+1=2$
\end{lem}
\begin{proofatend}
I should go in the amazing proof section. \Blindtext
\end{proofatend}

\begin{thm}\label{thm:mythm}
$1+2=3$
\end{thm}
\begin{proofatend}
I should go in the amazing proof section. \Blindtext
\end{proofatend}

\begin{thm}
Proof can stay in the main text
\end{thm}
\begin{proof}
This proof can stay in the main text.
\end{proof}

\begin{lem}\label{lem:myseclemma}
This should be Lemma 1.1 $1+1=2$
\end{lem}
\begin{proofatend}
I should go in the stupid proof section. \Blindtext
\end{proofatend}

\begin{thm}
$1+2=3$
\end{thm}
\begin{proofatend}
I should go in the stupid proof section. \Blindtext
\end{proofatend}

Above you can find my lemma \autoref{lem:myfirstlemma} and my theorem \autoref{thm:mythm}

\newpage
\section*{Proofs}
\subsection{Amazing proofs}
\subsection{Stupid proofs}

\printproofs

\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


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


\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\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
%% Put the theorem and proof only in appendix
all end/.is if=allattheend,
no all end/.style={all end=false},
%% 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,
no restate,
category=defaultcategory,
no both,
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 restate,
no both,
},
debug/.style={
proof here
},
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}{}{%
\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:
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: