[Tex/LaTex] \renewenvironment{document}

documentclass-writingenvironmentswrite file

I would like to use \usepackage{filecontents} to write another version of my document to a file later with \write18 and compile it.

My question is: How do I create a custom \begin{document} to handle the writing of the file in my template1.tex and including it later?

I would like to have something like this:

\documentclass{beamer}
\begin{customdocument}
//frames
\end{customdocument}

Intention: My custom document environment writes the content of customdocument into a second tex file and compile it with different options.

Output: Two files (one with notes and one without) but ONE working file.

I tried creating a new environment but then got the error of missing \begin{document}


Minimal example:

\documentclass{beamer}
    \usepackage{filecontents}
    \usepackage{docmute}
\begin{filecontents*}{template1.tex}
\documentclass{beamer}
\begin{document}
\begin{frame}{A frame}
    Foo
\end{frame}
\end{document}
\end{filecontents*}
\immediate\write18{pdflatex -jobname=\jobname _notes\space template1}
\begin{document}
\input{template1}
\end{document}

Best Answer

It is not clear to me, what is the purpose of the file template1.tex. But the syntax can be fixed. Package filecontents can be loaded before \documentclass via \RequirePackage:

\RequirePackage{filecontents}
\begin{filecontents*}{template1.tex}
\documentclass{beamer}
\begin{document}
\begin{frame}{A frame}
    Foo
\end{frame}
\end{document}
\end{filecontents*}
\immediate\write18{pdflatex -jobname=\jobname _notes\space template1}
\input{template1}

A redefinition of environment document is then not needed.

Related Question