# Why does the \includeonly command not \stepcounter the chaptercounter for omitted chapters?

I'm a big fan of the \includeonly command, to keep pagecounts intact while only printing specific chapters. But I'm wondering why chaptercounters are not stepped in a scenario such as this one:

\documentclass{report}
\usepackage{filecontents}

\begin{filecontents}{chapterone.tex}
\chapter{First Title}
Text
\end{filecontents}

\begin{filecontents}{chaptertwo.tex}
\chapter{Second Title}
Text
\end{filecontents}

\begin{filecontents}{chapterthree.tex}
\chapter{Third Title}
Text
\end{filecontents}

\includeonly{chapterone,chapterthree}

\begin{document}
\include{chapterone}
\include{chaptertwo}
%\stepcounter{chapter} % I have to manually step it here to get the right chapter number for three
\include{chapterthree}
\end{document}


If LaTeX goes through the content of chaptertwo to determine how many pages it is skipping over in order to start at the correct number for subsequent chapters, is there a reason why it does not check/step chaptercounters? Or am I using it incorrectly or did not understand something?

• I'm getting the expected chapter numbers if I run the example first with \includeonly commented out and only then add the \includeonly. The same holds for page numbers and everything else. LaTeX needs the data from the .aux files, so \includeonly will only give the 'correct' numbers (w.r.t. to the entire document) if at some point the entire document has been compiled and all chapters could write their .aux files. See for example the third paragraph in Davids answer to tex.stackexchange.com/q/87010/35864. – moewe Feb 19 at 7:41

## 1 Answer

LaTeX does not open the non-included files at all it just opens their .aux file. the values of all latex counters are saved in the aux file of each included file.

So if you do \includeonly{chaptertwo} then \include{chapterone} sets every declared latex counter to the values they had at the end of processing chapterone.tex last time that was included.

So periodically you should process the whole document without \includeonly so that the saved vales at each \include point are closer to the correct values.

• Oh, I see. Yes, that makes sense. – jan Feb 19 at 7:55
• Technically you can also process the chapters in order so that chapterone.aux is fresh when processing chaptertwo.tex and so on. – Stig Hemmer Feb 19 at 11:06
• @StigHemmer you can for some documents, not all, for example if your front matter uses the same page numbering as the main document then adding figures or tables or additional chanters can make the table of contents of list of figures take an additional page so the page numbers will be wrong in earlier chapters until you run a document that processes all the chapters and gets the frontmatter lists closer to final form. – David Carlisle Feb 19 at 12:48