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?
\include
.\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; leave a comment in the main file saying you've done that.\addcontentsline
about the page it is used on:\makeatletter \NewDocumentCommand{\TOCpart}{s m}{% \clearpage \refstepcounter{part}% \phantomsection \ifodd\c@page% \addcontentsline{toc}{part}{% \IfBooleanF{#1}{\protect\numberline{\thepart}}% #2% }% \else% \addtocounter{page}{+1}% \addcontentsline{toc}{part}{% \IfBooleanF{#1}{\protect\numberline{\thepart}}% #2% }% \addtocounter{page}{-1}% \fi% \cleardoublepage \ignorespaces } \makeatother
. If Barbara Beeton's solution works, I would recommend to use that one.