# How to add \part only to TOC?

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
\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
\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?

• I have a vague recollection that this sort of behaviour also occurs with Appendices and \include. – Peter Wilson Sep 1 '15 at 18:07
• i haven't tried this, but i know that 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 \included file; leave a comment in the main file saying you've done that. – barbara beeton Sep 1 '15 at 18:28
• You could also try to lie to \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. – Stephen Sep 1 '15 at 18:35
• There should have been line-breaks after the "%" in the code in my preceding comment, but in comments those are just removed. – Stephen Sep 1 '15 at 18:36
• @barbarabeeton: OK. Thanks, thats what I thought … I’d prefer to not have the Parts inside the chapter includes since they belong to a higher levee, but if I think it’s the easiest way to go … – Tobi Sep 1 '15 at 21:22

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 \included 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.

If you look at the aux file you will see that \@input{chap2.aux} occurs before \@writefile{toc}{\contentsline {part}{\numberline {II}TOC Part}{7}{part.2}} and there is nothing one can do about it. I even tried modifying \@docinclude to not use \immediate\write\@mainaux{\string\@input{#1.aux}}.

Anyway, the main key is to use \input instead of \include. The practical difference is that \include forces a \clearpage before and after, which is redundant here. See also Usage of \include, \input and \import commands.

I also replaced \phantomsection to make the aux files entries for \part and \TOCpart' closer in form.

\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}{%
\cleardoublepage
\refstepcounter{part}%
\hypertarget{part.\arabic{part}}{\relax}%
\IfBooleanF{#1}{\protect\numberline{\thepart}}%
#2%
}%
}

\begin{document}
\tableofcontents
\part{Normal Part}
\input{chap1}

\TOCpart{TOC Part}
\input{chap2}
\end{document}

• Thanks … but for a big project \include is much better since then I can use \includeonly – Tobi Sep 1 '15 at 21:19

If you're always including a \TOCpart immediately before a \chapter (with no content discussion), you can use "page + 1" as the page for the \TOCpart. Additionally, as hyperlink jump, you can use whatever will be set by the subsequent \chapter. It has the form chapter.#.

The above is implemented below:

\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
\stepcounter{part}%
\protect\contentsline{part}% type
{\protect\numberline{\thepart} #2}% number + title
{\number\numexpr\value{page}+1}% page number
\cleardoublepage
\ignorespaces
}

\begin{document}
\tableofcontents
\part{Normal Part}
\include{chap1}

\TOCpart{TOC Part}
\include{chap2}

\part{Normal Part}
\chapter{Chapter} This is a chapter.
\chapter{Another chapter} This is a chapter.

\TOCpart{TOC Part}
\chapter{Chapter} This is a chapter.
\end{document}

• It may be better to perform a \cleardoublepage before inserting the part ToC entry. Regardless, if the insertion is consistent, the above principle should work. – Werner Sep 1 '15 at 22:02

"that’s kind of cheating and works in this case but will fail if chapters can start on any page (which is not the case for my current project). You could make your comment into an answer for the record, since it will be much more legible" (Tobi Sep 1 at 21:24)

There should have been line-breaks in the code in my comment, but in comments those are just removed. - Thus, just for the record and readability, another (not recommended ) way to do it:

You can try to lie (!) to \addcontentsline about the page it is used on:

\makeatletter%
\NewDocumentCommand{\TOCpart}{s m}{%
\clearpage%
\refstepcounter{part}%
\phantomsection%
\ifodd\c@page%
\IfBooleanF{#1}{\protect\numberline{\thepart}}%
#2%
}%
\else%
`