% \iffalse
%<*internal>
\iffalse
%</internal>
%
%<*license>
Copyright (C) 2009 by T.M. Trzeciak <t34www@googlemail.com>

This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version.  The latest version of this license is in the file:

http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
T.M. Trzeciak.

This work consists of the file  docmute.dtx
and the derived files           docmute.ins,
                                docmute.pdf
                                docmute.sty.
%</license>
%
%<*readme>
The docmute package enables to input or include standalone
LaTeX documents as if they consisted of only the content
between \begin{document} and \end{document}.
%</readme>
%
%<*internal>
\fi
\begingroup
%</internal>
%
%<*batchfile>
\input docstrip.tex
\keepsilent\askforoverwritefalse
\nopreamble\nopostamble
\generate{\file{\jobname.sty}{\from{\jobname.dtx}{package}}}
%</batchfile>
%<batchfile>\endbatchfile
%
%<*internal>
\generate{\file{\jobname.ins}{\from{\jobname.dtx}{batchfile}}}
\generate{\file{README.TXT}{\from{\jobname.dtx}{readme}}}
\endgroup
%</internal>
% \fi
%
% \begin{comment}
% 
%<*documentation>
\documentclass{ltxdoc}
\usepackage{verbatim}
\usepackage{docmute}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%\OnlyDescription
%
\begin{document}

\GetFileInfo{docmute.sty}
\title{The \textsf{docmute} package%
\thanks{This file describes version \fileversion, last revised \filedate.}}
\author{T.\,M. Trzeciak\thanks{E-mail: \texttt{t34www@googlemail.com}}}
\maketitle

\section{Intoduction}

The structure of \LaTeX\ documents doesn't allow one document to be included as a part of another (at least not without some hacking). This package tries to remove this limitation and enable the standard |\input| and |\include| commands to be used on standalone documents just as if they contained no preamble.

\section{Usage}

The package is loaded with the usual |\usepackage{docmute}|. It redefines the |\documentclass| command in such a way that everything between this command and the |\begin{document}| is skipped, as well as everything after the |\end{document}|. No attempts are made to analyse the actual content of the preamble. It is up to you to ensure that the main document loads all packages required by subsidiary documents.

\DescribeMacro{\docmute}
In rare cases some commands might be already present already before the |\documentclass| command. These commands would be then normally executed by the main document. You can prevent that by putting |\csname docmute\endcsname| at the top of the inputted document. The |\docmute| command has the same effect as the redefined |\documentclass|. Executing it through the control sequence forming primitive rather than directly ensures that standalone compilation is still possible.  In that case |\csname docmute\endcsname| will have the same effect as |\relax|.

By default, nesting of inputted documents is not allowed, i.e. inputted documents cannot themselves input other documents. This behaviour can be changed with the package option |nested|. With this option inputted documents are surrounded by a group and nesting becomes possible. Note, that due to grouping only global macro (re)definitions will be visible in the parent document.

\section{Known limitations}

The |\documentclass| and/or |\csname docmute\endcsname| command  has to be in the same file as |\begin{document}|. This is due to employed preamble gobbling method, which relies on scanning for delimited macro arguments and which doesn't work across the file boundaries.

\DocInput{\jobname.dtx}

\end{document}
%</documentation>
%
% \end{comment}
%
% \StopEventually{}
% 
% \section{Implementation}
% 
% \iffalse
%<*package>
% \fi
% 
% Initial boilerplate code.
% 
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{docmute}[2009/10/08 v1.1 Input standalone documents]
%    \end{macrocode}
%    
% Macro used for comparison to see if we already reached 
% |\begin{document}|.
% 
%    \begin{macrocode}
\def\docmute@docenv{document}
%    \end{macrocode}
%    
% The main macro of this package. Grab everything before the 
% |\begin| command and check what argument was passed to it.
% 
%    \begin{macrocode}
\long\def\docmute@gobblepreamble#1\begin#2{% 
  \def\docmute@thisenv{#2}% 
%    \end{macrocode}
%    
% If |\begin{document}| was reached we can stop gobbling, 
% otherwise we continue with tail recursive call.
% 
%    \begin{macrocode}
  \ifx\docmute@thisenv\docmute@docenv
    \docmute@afterpreamble
  \else
    \expandafter\docmute@gobblepreamble
  \fi
}
%    \end{macrocode}
%    
% After gobbling stops, |\enddocument| is redefined to stop 
% input when it's executed. Excess spaces, if any, are taken 
% care of by |\ignorespaces|.
% 
%    \begin{macrocode}
\def\docmute@afterpreamble{% 
  \docmute@atbegindoc
  \def\enddocument{% 
    \docmute@atenddoc
    \aftergroup\ignorespaces
    \endinput
  }% 
  \ignorespaces
}
%    \end{macrocode}
%    
% Save original definitions of |\document| and |\enddocument|
% 
%    \begin{macrocode}
\let\docmute@document=\document
\let\docmute@enddocument=\enddocument
%    \end{macrocode}
%    
% Hooks to implement nesting.
% 
%    \begin{macrocode}
\let\docmute@atbegindoc=\relax
\def\docmute@atenddoc{%
  \let\enddocument=\docmute@enddocument
  \begingroup
}
%    \end{macrocode}
%    
% Define the |\docmute| command
% 
%    \begin{macrocode}
\let\docmute=\docmute@gobblepreamble
%    \end{macrocode}
%    
% Redefine the |\documentclass| command at the beginning of 
% the document. |\AtBeginDocument| happens to early to be used
% for that, so we need to append directly to the |\document|.
% 
%    \begin{macrocode}
\def\document{% 
  \docmute@document
  \let\documentclass=\docmute
  \ignorespaces
}
%    \end{macrocode}
%    
% Finally, handle the only option we have and that's it.
% 
%    \begin{macrocode}
\DeclareOption{nested}{% 
  \let\docmute@atbegindoc=\begingroup
  \let\docmute@atenddoc=\relax
}
\ProcessOptions
\endinput
%    \end{macrocode} 
% \iffalse
%</package>
% \fi
% 
% \Finale

