Compile separate document chapters into book w/o duplicate references?

I am trying to collect a set of independent documents, each with its own preamble and begin{document} and \end{document} lines into a book. I have looked into the subfiles package and tried \include but all of these require me to assemble a common preamble into a main.tex and then \include each of the file separately. The problem with this is that I lose the ability to independently compile each of the separate chapters because each chapters' preamble had to be removed as it was consolidated into main.tex. Thus, if I later want to compile each chapter separately, I have to replace the preamble and then compile. I would like to avoid doing this by hand because there are a lot of chapters.

Another problem is that each of the separate chapters has labels such as fig:figure1. There is no ambiguity when compiling each chapter separately, but when you combine them into one book they become ambiguous.

Is there a way around these problems?

-
have you looked at pdfpages? –  cmhughes Mar 7 '13 at 16:37
I don't think that solves my duplicate referencing problem though. –  reckoner Mar 8 '13 at 14:19

First, I think it is good style to consolidate similar preambles. If, e.g., the subfiles define macros for special symbols in their preambles, it would be a good idea to outsource all such definitions into, say, mysybols.sty, that then can be loaded by all subfiles, and easily consolidated into a main preamble.

Apart from that, you might be looking for the standalone package. This package has the possibility to execute preambles of subfiles, see example below. Note however, that conflicting definitions or package options in the sub-preambles can not be resolved. This leaves the problem of multiply defined labels. I propose a hack that makes those labels unique by prepending the chapter number. For that, the definition of \label and \ref are altered. If you use other cross-referencing macros that don't fall through to \label and \ref like \cref from the cleveref package, they have to be treated in the same way. Note that redefinition of \label and \ref comes after \begin{document} here to have those redefinitions even after hyperref changed them.

Here are two small subfiles, both defining a macro, and both using a label fig:1.

subfile1.tex is

\documentclass{article}
\newcommand{\foo}{first subfile}

\begin{document}
\section{First section of \foo}
\begin{figure}
\caption{A figure of \foo}\label{fig:1}
\end{figure}
See Figure~\ref{fig:1}.
\end{document}


subfile2.tex is

\documentclass{article}
\newcommand{\baz}{second subfile}

\begin{document}
\section{First section of \baz}
\begin{figure}
\caption{A figure of \baz}\label{fig:1}
\end{figure}
See Figure~\ref{fig:1}.
\end{document}


Now the main document:

\documentclass{report}
\usepackage[subpreambles=true]{standalone}

\begin{document}
\let\oldlabel\label
\renewcommand{\label}[1]{\oldlabel{\thechapter:#1}}
\let\oldref\ref
\renewcommand{\ref}[1]{\oldref{\thechapter:#1}}
\tableofcontents
\chapter{First chapter including first subfile}
\input{subfile1}

\chapter{Second chapter including second subfile}
\input{subfile2}
\end{document}


If the sub-preambles are conflicting, and you really don't want to tidy that up, your best option is probably joining the pdf files with the pdfpages package, or even just pasting the pdf files together with a tool like pdfjoin. Note however, that with pdfpages you lose all hyperlinks from the sub-pdfs.

-
Good tip about redefining the \label command. Seems to work fine for me... –  Rabarberski Apr 28 at 12:04