[Tex/LaTex] Restate theorem in separate file

cross-referencingtheoremsthmtoolsxr

I state a theorem in the main body of a document and prove it in the appendix. I use thmtools's restatable environment to restate the theorem there. However, I now need to have my appendix in a separate pdf file (hence a separate tex document). I'm using xr and xcite to get the cross-references and bibliographical references to play nicely.

But the commands created by restatable are not carried. Is there any way to to the restating of the theorem in the appendix in a separate document?

Here's a MWE:

main document: main.tex

\documentclass{article}
\usepackage{thmtools}
\newtheorem{theorem}{Theorem}

\begin{document}

\begin{restatable}{theorem}{thmcmd}\label{thmlabel}
Here is the statement of the theorem.
\end{restatable}

The above is Theorem~\ref{thmlabel}.

In this way I can restate it:
\thmcmd*

\end{document}

appendix: appendix.tex

\documentclass{article}
\usepackage{xr}
\externaldocument{main}
\usepackage{thmtools}
\newtheorem{theorem}{Theorem}

\begin{document}

I can refer to Theorem~\ref{thmlabel}.

But I cannot restate it like this:
% \thmcmd*

\end{document}

Best Answer

tcolorbox package provides some macros for saving the content of boxes and use it later.

The simplest example could be:

\documentclass{article}
\usepackage[most]{tcolorbox}

\begin{document}
\begin{tcolorbox}[saveto=myfile.tex, lowerbox=ignored]
This is a nice theorem
\tcblower
This is the nice prove
\end{tcolorbox}

\begin{tcolorbox}[title=A nice theorem with its prove]
\input{myfile.tex}
\end{tcolorbox}

\end{document}

In previous code saveto option, saves the whole tcolorbox content (upper and lower parts) in file myfile.tex. It saves only the contents, not the format of the box.

Option lowerbox=ignored makes that the lower part (the prove in this case) doesn't appears, but it is saved.

Later on into the same document, or into another document, myfile.tex can be restated as a tcolorbox content.

enter image description here

More information about these commands can be found in tcolorbox documentation, sections 4.3 Upper part, 4.4 Lower part and for more complex examples in chapter 8. Recording. Some examples here: https://tex.stackexchange.com/a/257455/1952 and https://tex.stackexchange.com/a/224429/1952

Update

I'll try to show that example shown in https://tex.stackexchange.com/a/224429/1952 can be also applied with an external document. The first document will contain theorems and proofs, although only theorems will be printed, while the second one will show theorems and proofs. This solution applies an invisible box (blankest in tcolorbox nomenclature) as OP wanted.

The document with theorems and proof is called TestTheorems.tex and its content is:

\documentclass{article}

\usepackage{lipsum}
\usepackage[most]{tcolorbox}

\NewTColorBox[auto counter,number within=section]{theorem}{+O{}}{ %
    enhanced, blankest,
    coltitle=black,
    title={Theorem~\thetcbcounter:},
    label={theorem@\thetcbcounter},
    attach title to upper=\quad,
    theorem style=standard,
    lowerbox=ignored,
    saveto=theorems/theorem-\thetcbcounter.tex,
    record={\string\proof{\thetcbcounter}{theorems/theorem-\thetcbcounter.tex}},
    #1
}

\begin{document}
\section{Some theorems without proof}

\tcbstartrecording

Some theorems are shown in this document. All proofs are also written here, but not printed. They will be printed in another document.

\begin{theorem}
This is the first theorem
\tcblower
This is the proof of the first theorem\end{theorem}

\begin{theorem}
This is the second theorem
\tcblower
This is the proof of the second theorem\end{theorem}

\begin{theorem}
This is the third theorem
\tcblower
This is the proof of the third theorem\end{theorem}

\begin{theorem}
This is the fourth theorem
\tcblower
This is the proof of the fourth theorem\end{theorem}

\tcbstoprecording

\end{document}

It declares a theorem tcolorbox whose contents will be saved (saveto option) in different files, one for each theorem. The name of this files is alse recorded (record option) into an auxiliary file called TestTheorems.record. This auxiliary file is created with comman \tcbstartrecording and is closed with command \tcbstoprecording.

After processing this file the result is:

enter image description here

but also a folder theorems with four files in it, one for each theorem, and the file TestTheorems.records into main folder.

The second file TestProofs.tex contains the definition of tcolorbox which will read all recorded theorems and proofs. And with xr help, they will keep their original names and numbers. Command \tcbinputrecords declares which file will be read. Being an external file, it's necessary to indicate the name as an option.

\documentclass{article}

\usepackage{xr}
\externaldocument{TestTheorems}
\usepackage[most]{tcolorbox}

\NewTotalTColorBox{\proof}{mm}{ %
    enhanced, blankest,
    coltitle=black,
    theorem style=plain,
    title={Theorem~\ref{theorem@#1}:},
    phantomlabel={proof@#1},
    attach title to upper=\par,
    before lower={Proof:\par}
}{\input{#2}}

\begin{document}
\section{Phnatom section}

\section{Theorems and proofs}

This document shows already printed theorems but it also includes all proofs.

\tcbinputrecords[TestTheorems.records]

\end{document}

The result is:

enter image description here