I wonder how I can add a \part
entry to the TOC without any output in the the document itself. I actually got a nearly working version:
\begin{filecontents}{chap1.tex}
\chapter{Chapter} This is a chapter.
\end{filecontents}
\begin{filecontents}{chap2.tex}
\chapter{Chapter} This is a chapter.
\end{filecontents}
\documentclass{scrbook}
\usepackage{xparse,hyperref}
\NewDocumentCommand{\TOCpart}{s m}{%
\clearpage
\refstepcounter{part}%
\phantomsection
\addcontentsline{toc}{part}{%
\IfBooleanF{#1}{\protect\numberline{\thepart}}%
#2%
}%
\cleardoublepage
\ignorespaces
}
\begin{document}
\tableofcontents
\part{Normal Part}
\include{chap1}
\TOCpart{TOC Part}
\include{chap2}
\part{Normal Part}
\chapter{Chapter} This is a chapter.
\TOCpart{TOC Part}
\chapter{Chapter} This is a chapter.
\end{document}
This works except that “II. TOC Part” should be listed with page 7 instead of 6. I.e. with the same one as the following chapter. If I change the definition to
\NewDocumentCommand{\TOCpart}{s m}{%
\cleardoublepage
\refstepcounter{part}%
\phantomsection
\addcontentsline{toc}{part}{%
\IfBooleanF{#1}{\protect\numberline{\thepart}}%
#2%
}%
% \cleardoublepage
\ignorespaces
}
The entry has the right page number but if followed by an \include
it appears behind the chapter entry, which is pretty wrong … so how can I combine the two definitions to get the part before the chapter but with the chapter’s page number, while still using \include
and don’t moving \TOCpart
inside of the included file?
Best Answer
if an
\addcontentsline
is in the "main" file, just before an\include
, it is deferred.the
\addcontents
(and therefore the\TOCpart
here) needs to go inside that\include
d file, even though that seems counter-intuitive, and obscures the fact that you needed to accomplish the change that way.i recommend leaving a comment in the main file to say what you've done, so that if you want to change the location of the "part" entry (or, alternatively, later wonder where it's coming from), you will have an obvious clue.