Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
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

1 Answer 1

up vote 2 down vote accepted

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

\newcommand{\foo}{first subfile}

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

subfile2.tex is

\newcommand{\baz}{second subfile}

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

Now the main document:


\chapter{First chapter including first subfile}

\chapter{Second chapter including second subfile}

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.

share|improve this answer
Good tip about redefining the \label command. Seems to work fine for me... –  Rabarberski Apr 28 at 12:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.