36

Can I place all proofs to theorems in appendix automatically? Usually proofs are written in the LaTeX codes immediately after the theorems. Without physically moving proofs to appendix, how can I place them in appendix automatically? Is there a package?

  • 3
    I am not a 100% sure this can do only proofs to appendix, hence a comment and not an answer, but have a look at thrmappendix. – Roelof Spijker Oct 31 '11 at 15:28
  • If all your proofs should go to the appendix, why not provide them there? You can write them anywhere, and it gives you more freedom in the order you can present them. – user10274 Feb 3 '12 at 1:14
  • 1
    Marc: sometime you prefer the proofs to be inline, but have to move them to the appendix for a conference submission. In this case one would look for an automatized way to do so, in good computer scientist. – Jeremy Feb 10 '12 at 14:08
31

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.

  • 1
    Your codes work good! One problem: If there is a paragraph break, it produces an error. – Chang Oct 31 '11 at 16:50
  • 3
    @Chang My fault, now it's fixed: a \long was missing. – egreg Oct 31 '11 at 16:54
  • Under pdfTeX 3.1415926-1.40.10-2.2 (TeX Live 2009/Debian), your code generate on my machine the following error: ! Undefined control sequence. \fixstatement #1->\AtEndEnvironment {#1}{\xdef \pat@label {\expandafter \exp... l.51 \fixstatement{thm} – Jeremy Feb 16 '12 at 18:09
  • 1
    @Jeremy The problem is that your distribution has an old version of etoolbox, which doesn't provide \AtEndEnvironment. TeX Live 2009 is really outdated. – egreg Feb 16 '12 at 18:18
  • 2
    @AlfredoHernández I've uploaded a new version that has a few improvements and also fixes the problem with \declaretheorem. – egreg Oct 5 '13 at 16:03
2

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

0

I wrote an answer here Ultimate proof at the end/in appendix: different sections, with references to proof/theorem, restate, and synctex where I provide a mini-library super easy to use, in order to put theorems in different places, mix them with lemmas, make reference to them, restate them, duplicate the proof in appendix and in main (nice for synctex), add comments in the appendix, link between the theorem and the proofs...

I won't write the code again here to avoid duplicata of answers, so I'll put a picture: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.