I have a document that I need to recompile often. Currently I am doing the standard two pass compile where the first pass generates the .toc file and the second pass then inserts it.
However, as my document compilation takes several hours, and the second pass only changes the first few pages I am looking for a way to avoid recompiling the entire document just to generate the Table of Contents.
Notes:
- I am not worried about other issues that are handled in the second pass such as labels, cross references,
longtable
,\tikzmark
, etc. - I do need the bookmarks and the TOC to have links to the corresponding pages, but do not need any links back to the TOC itself.
- The MWE below uses the solution from set minimum number of pages for TOC, to leave sufficient blank PDF pages for the Table of Contents. This should ensure that the bookmarks and links form the TOC to the body work properly.
- This document is only for internal use so leaving a few extra blank pages in the TOC to allow for growth of the document is perfectly acceptable.
Code:
\def\NumberOfChapters{10}
\def\NumberOfSections{50}
\def\NumberOfPagesForTOC{14}
\documentclass{book}
\usepackage{lipsum}
\usepackage{pgffor}
\usepackage[colorlinks=true]{hyperref}
\begin{document}
\frontmatter
\pagenumbering{roman}
\tableofcontents
\cleardoublepage
%% https://tex.stackexchange.com/questions/43140/set-minimum-number-of-pages-for-toc
\foreach\x in {\value{page},...,\NumberOfPagesForTOC}{\vbox{}\newpage}% Insert up to absolute page #14.
\pagenumbering{arabic}
\mainmatter
\foreach \x in {1,...,\NumberOfChapters}{%
\chapter{Chapter \x}
\foreach \y in {1,...,\NumberOfSections}{%
\section{Section \y}
\lipsum[1-7]
}
}
\end{document}
\end{document}
right after\tableofcontents
and do something clever with pdfjam.pdfpages
but don't know how to ensure that the links from the TOC to the appropriate pages in the document will work.