How to place all proofs automatically in appendix?

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}

enter image description here

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.


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}

enter image description here

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