You could split every report into two files: a main file holding only the preamble and an \input
as document body, and a second file holding all the actual content.
Then you are able to \input
or \include
all the content files in another main file for the book.
% Main file of one report, e.g. `report01.tex`
\documentclass{report}
\title{...}
\begin{document}
\maketitle
\input{content01}
\end{document}
The content file then look like:
% content01.tex
\section{...}
Some text
Book main file:
\documentclass{book}
% ....
\begin{document}
\maketitle
\input{content01}
\input{content02}
\input{content03}
\input{content04}
\end{document}
This should work fine as long you don't want to keep the report title pages.
There are also packages like docmute
or standalone
which allow you to \include
or \input
another file which has its own preamble. The preambles of this sub-files are then ignored.
This is basically the same like above but you don't need to split each report into two files.
Here are two answers. (You've already said you don't like the second one; I'm leaving it because someone might find it useful some day.)
First:
With this TeX
source
\documentclass{book}
\usepackage{etoolbox}
\newtoggle{volumeone}
%\toggletrue{volumeone}
\togglefalse{volumeone}
\iftoggle{volumeone}{
\includeonly{ch1,ch2}
}{
\includeonly{ch2,ch3}
}
\begin{document}
\tableofcontents
\include{ch1} % \chapter{one} contents
\include{ch2} % \chapter{two} contents
\include{ch3} % \chapter{three} contents
\end{document}
run pdflatex
several times (to stabilize all references). Here is book.toc
:
\contentsline {section}{\numberline {1.1}one}{4}
\contentsline {section}{\numberline {1.2}two}{4}
\contentsline {chapter}{\numberline {2}two}{5}
\contentsline {section}{\numberline {2.1}one}{5}
\contentsline {section}{\numberline {2.2}two}{5}
\contentsline {chapter}{\numberline {3}three}{7}
\contentsline {section}{\numberline {3.1}one}{7}
\contentsline {section}{\numberline {3.2}two}{7}
Edit that file, to remove the first two lines (all the references to Chapter one). Then rerun pdflatex
once.
This works for me on Windows 7,
$ pdflatex -version
MiKTeX-pdfTeX 2.9.4535 (1.40.13) (MiKTeX 2.9)
since pdflatex
hasn't noticed that the toc
file is out of date.
You could automate this workflow with a shell script or batch file, using perl
or python
or awk
to edit the toc
.
Second: pagination is right but lots of other things need fixing - headers, chapter counters, references ... . I don't know how.
\documentclass{book}
\usepackage{etoolbox}
\newtoggle{volumeone}
\toggletrue{volumeone}
%\togglefalse{volumeone}
\iftoggle{volumeone}{
\includeonly{ch1,ch2,ch3}
}{
\includeonly{ch3,ch4}
}
\newcommand{\tocline}[1]{%
\addcontentsline{toc}{chapter}{#1}
}
\newcommand{\mychapter}[1]{%
%do everything the actual \chapter
%command does except enter a line in
%the table of contents
}
\newcommand{\mysection}{%
%check volumeone toggle, omit from toc as approprate
}
\begin{document}
\tableofcontents
\mychapter{one}
\iftoggle{volumeone}{\tocline{one}}{}
\include{ch1} % contents of chapter one in ch1.tex
\mychapter{two}
\iftoggle{volumeone}{\tocline{two}}{}
\include{ch2}
\mychapter{three}
\tocline{three} % in both volumes
\include{ch3}
\mychapter{four}
\iftoggle{volumeone}{}{\tocline{four}}
\include{ch4}
\end{document}
toggle the toggle
Best Answer
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 thecleveref
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 afterhyperref
changed them.Here are two small subfiles, both defining a macro, and both using a label
fig:1
.subfile1.tex
issubfile2.tex
isNow the main 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 likepdfjoin
. Note however, that withpdfpages
you lose all hyperlinks from the sub-pdfs.