[Tex/LaTex] Put the proof in appendix, with links between theorem and proofs

positioningtheoremsthmtools

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".
  2. Add a link from this text to the proof
  3. Add another link from the proof title to the theorem
  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
Please find the \hyperlink{th1.1}{proof of this theorem} in appendix.
[... 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 !

Best Answer

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[colorlinks]{hyperref}
\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}}%
      \addtocounter{counttheorems}{1}
      \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}}%
      \addtocounter{counttheorems}{1}
      \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.
  % [\textbf{thm_fix:\pat@uniqlabel}]\hyperlink{thm_fix:\pat@uniqlabel}{Ahah}\\
  \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}%
    \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}
$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}
Related Question