[texhax] General Advice on Combining Documents for a Thesis

Susan Dittmar Susan.Dittmar at gmx.de
Mon May 31 11:42:25 CEST 2010


It is possible to combine several stand-alone-documents into a bigger
document without removing the \begin{document} and \end{document} lines or
the preamble. I do this quite often. What you need for that is to make sure
that all definitions of the document preamble of the embedded documents are
also in the surrounding document (perhaps using a separate file for those
definitions).

There exist several ways you can accomplish this. I use the following
approach.

Start every document with:

%%%%%%%%%%%%
% check whether this is a stand-alone or embedded document:
\makeatletter
\@ifundefined{\standalonetrue}{\newif\ifstandalone}{}
\@ifundefined{section}{\standalonetrue}{\standalonefalse}
\makeatother
% only read preamble if this is a stand-alone document:
\ifstandalone
%%%%%%%%%%%%

Then follows \documentclass{...} and the whole preamble. It will only be
read in the stand-alone document, not in case of embedding it.

This part ends with

%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%

followed possibly by a title page and the rest of the front-matter that
should not be included in the embedded version. The true end of this block
is 

%%%%%%%%%%%%
\fi	%\ifstandalone
%%%%%%%%%%%%

Everything that follows now will be used for both versions. Wherever you
want to differentiate between the stand-alone and the embedded version, you
can enclose it in

%%%%%%%%%%%%
\ifstandalone
	% this will only be in the stand-alone version
\else
	% this will only be in the embedded version
\fi	%\ifstandalone
%%%%%%%%%%%%

The document then will end with another of those \ifstandalone blocks, but
a special one.

%%%%%%%%%%%%
\ifstandalone
    %here comes all the back-matter stuff that should not be included in the
	%embedded version

	% ...

\else	%\ifstandalone
\expandafter\endinput
\fi		%\ifstandaline
\end{document}
%%%%%%%%%%%%

The construct in the \else block makes sure that the \end{document} line
(which *must* be outside of the \ifstandalone\else\fi construct!) is not
reached in case of embedding this file.

The file itself can be embedded with the \input and the \include
commands, as you desire.

In case you want to keep the different documents in different directories,
I recommend using the fink package. It helps keeping track of the current
filename and directory and thus allows relative file paths (relative to the
current file, not to the main document).

Hope that helps,

	Susan



More information about the texhax mailing list