# Put the proof in appendix, with links between theorem and proofs

I would like to be able to add some proofs of my document in appendix. I like the solution "New Version" given here, but I would like in addition to have:

1. A text below the theorem that write something like "Please, find the proof of this theorem in appendix".
4. If possible recall the theorem just above the proof

For example it would looks like, in pseudo-generated-code:

Theorem 1.1
\label{th1.1}
1 = 1
[... lot's of stuff ...]

Proofs.
Theorem 1.1
1 = 1
\hypertarget{th1.1}
Proof of \autoref{th1.1}
Trivial.


I tried to change by myself the code of the above answer, but I'm not familiar enough with tex syntax, and I'm getting stuck on point 3 with link that point nowhere (I suspect bugs with the expandafter, but I don't know how to debug my code).

Thank you for your help !

• Rather than posting code snippets please post a full minimal working example. In particular, it should compile. This makes it much easier to help you -- and, in this instance, would hopefully make your \expandafter comment more meaningful since this command does not appear in the code you posted. – Andrew Nov 2 '16 at 12:25
• Actually I didn't write many thing, and most of it must be wrong, so someone that would like to answer could base it's answer on the linked code. If needed I can copy/paste it, but I'm not sure it's a good idea. – tobiasBora Nov 2 '16 at 12:35
• You should be able to use \label{...} and \autoref{...} to do what you want. – Andrew Nov 2 '16 at 12:40
• For the 4th point, you should use the restatable environment from thmtools. Cf tex.stackexchange.com/a/51288/34551 – Clément Nov 2 '16 at 12:58
• After playing a bit with LaTeX, I found a solution for 1,2,3. However, I didn't found a solution for point 4 because I don't see how I could automatiquely turn the \begin{theorem} into \begin{restatable}[]{theorem}{}... If you know how to do, I'm interested ! – tobiasBora Nov 2 '16 at 17:48

I found a first solution, that seems to work for points 1,2,3, but not for 4 : I don't know how to change the environment of theorem (\begin{theorem} must be transform in \begin{restatable}[]{theorem}{} EDIT : it seems that adding an option restate=foo is enough, but I don't know how I could add such an option... I tried to set \xdef\pat@restate{foo} but it doesn't work... Any idea ?).

\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}
\begin{lem}
$1+1=2$
\end{lem}
\begin{proofatend}
It's quite clear. \Blindtext
\end{proofatend}
\newpage
\begin{thm}
$1+2=3$
\end{thm}
\begin{proofatend}
Obvious from lemma. \Blindtext
\end{proofatend}
\newpage
\section*{Proofs}

\printproofs

\end{document}