# Latex compiling out \begin{document} etc

I am writing a thesis with latex, each chapter is in its own TeX file and get included with a \input{Chapters/Chapter1} command. One of my Chapters is generated with Sweave (R code) and includes \documentclass{article}, \begin{document}, \end{document} etc at the top - this interferes with compiling the full document - so I remove them manually each time I compile. I am looking for a way to remove these lines at compile time of the full document. thanks.

• have a look at the docmute package Apr 9 '14 at 15:10
• Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question.
– Werner
Apr 9 '14 at 18:38

Normally you cannot do this because macros like \usepackage can only be set in the preamble. If however, the preamble of the auto generated chapter is simple enough, and it seems to be since you said you only edit out \documentclass, \begin{document}, and \end{document}, then you can automated the processb y redefining \documentclass and the document environment in the full thesis file to do nothing but gobble their arguments. The document environment basically creates a group and expands \document at the beginning and \enddocument at the end.The tricky thing is that you still need to expand the final \enddocument that finishes the full document. You could just define \document to be \def\document{\def\enddocument{}} since \enddocument will be restored once the group is closed. The problem with this is the chapter will be in a different group and therefore any changes you make will not be global. So it is probably better to close the group in \document and let \endocument redefine itself. The following MWE provides one solution

\RequirePackage{filecontents}

\begin{filecontents*}{\jobname-A.tex}
\documentclass{article}
\begin{document}
This is file A
\end{document}
\end{filecontents*}

\begin{filecontents*}{\jobname-B.tex}
\documentclass{article}
\begin{document}
This is file B
\end{document}
\end{filecontents*}

\documentclass{article}
\begin{document}
\renewcommand{\documentclass}[2][]{}
\let\enddocumentOrig\enddocument
\def\document{\endgroup\def\enddocument{\def\enddocument{\enddocumentOrig}\begingroup}}

This is the main file

\input{\jobname-A}

\input{\jobname-B}

\end{document}


It is as simple as adding the docmute package to your preamble:

\RequirePackage{filecontents}

\begin{filecontents*}{\jobname-A.tex}
\documentclass{article}
\usepackage{mwe}
\begin{document}
This is file A
\end{document}
\end{filecontents*}

\begin{filecontents*}{\jobname-B.tex}
\documentclass{article}
\usepackage{lipsum}
\begin{document}
This is file B
\end{document}
\end{filecontents*}

\documentclass{article}
\usepackage{docmute}
\begin{document}

This is the main file

\input{\jobname-A}

\input{\jobname-B}

\end{document}


docmute redefines \input and \include to exclude any contents outside \begin{document}...\end{document}.

includex (distributed with MiKTeX) provides a similar interface via \includedoc (there is a starred version as well).