I want to create a book type file, where each chapter is separatly compiled. That is I get the whole thing as well as each individual chapters in pdf or dvi or ps format. How do I do that?
[Tex/LaTex] How to create a book with separately produced chapters
book-design
Related Solutions
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}
\usepackage[colorlinks]{hyperref}
\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}
\usepackage[colorlinks]{hyperref}
\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[colorlinks]{hyperref}
\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.
Here is an attempt at recreating the chapters. There are several things which might break:
- two-digit chapter numbers will be too wide
- long chapter titles will mess up the vertical alignment
From here it isn't that difficult to change the section numbers. I'll leave this task to the OP or anyone else who'd like to comment …
Here is the code so far:
\documentclass[oneside]{book}
\usepackage{bookman}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{microtype}
\usepackage{titlesec}
\usepackage{blindtext}
\usepackage{xcolor}
\usepackage{soul}
\definecolor{gray50}{gray}{0.5}
\usepackage{tikz}
\newcommand\dottedline[1][]{%
\tikz[baseline]\draw[line width=3pt,dotted,line cap=round,dash pattern=on 0pt off 3\pgflinewidth, gray50](0,-1.2)--(0,1.8);%
}
% \titleformat{〈command〉}[〈shape〉]{〈format〉}{〈label〉}{〈sep〉}{〈before-code〉}[〈after-code〉]
\titleformat%
{\chapter}% command
[hang]% shape
{\Huge\bfseries}% format
{\raisebox{0pt}[0pt][4\baselineskip]{%
\begin{minipage}{25mm}
\raisebox{12mm}{\parbox{25mm}{\centering\fontsize{90}{90}\normalfont\bfseries\textcolor{gray50}{\thechapter}\\[4mm]\normalsize\bfseries\textcolor{gray50}{\so{CHAPTER}}}}%
\end{minipage}\hspace{10mm}\dottedline
}
}% label
{10mm}% sep
{}% before-code
[]% after-code
\begin{document}
\chapter{Introduction}
\blindtext
\chapter{Second Chapter}
\end{document}
This is the output:
Best Answer
Without using packages, you could use simply the \input commands:
Write each chapter in a separate file (don't compile these files):
In chapter1.tex:
In chapter2.tex:
Then write your book as:
in book.tex:
Compile this file to get your full book.
To get the separate chapter as articles, you can compile the following file:
In chapter1Article.tex:
If you want them as a book with only one chapter, use the following file. To get the right chapter number, use \setcounter.
In chapter1Book.tex:
etc...