1

I wish to start a chapter without displaying the header at all. The side effects, however, should be preserved, namely the chapter counter should be stepped, and a TOC entry created.

Note that @Dirk's answer for Hide part and chapter headings still produces some white space in place of the heading. Plus, I would rather not use the titlesec package.

To this end, this works fine:

\documentclass{scrbook}

\begin{document}
\tableofcontents

\addtocentrydefault{chapter}{}{Ch1}\stepcounter{chapter}
\section{Sec11}
\section{Sec12}

\cleardoublepage
\addtocentrydefault{chapter}{}{Ch2}\stepcounter{chapter}
\section{Sec21}
\section{Sec22}

\end{document}

However, things change when I \include the sections:

\documentclass{scrbook}

\begin{filecontents}{Sec11.tex}
\section{Sec11}
\end{filecontents}
\begin{filecontents}{Sec12.tex}
\section{Sec12}
\end{filecontents}
\begin{filecontents}{Sec21.tex}
\section{Sec21}
\end{filecontents}
\begin{filecontents}{Sec22.tex}
\section{Sec22}
\end{filecontents}

\begin{document}
\tableofcontents

\addtocentrydefault{chapter}{}{Ch1}\stepcounter{chapter}
\include{Sec11}
\include{Sec12}

\cleardoublepage
\addtocentrydefault{chapter}{}{Ch2}\stepcounter{chapter}
\include{Sec21}
\include{Sec22}

\end{document}

Now, the TOC is messed up: enter image description here

How does this come about. And is there a nice (simple) way to fix this?

FlorianL
  • 1,659

1 Answers1

3

Original answer (Below is an update with a maybe better suggestion)

Here is a suggestion patching \@include:

\documentclass{scrbook}
\pagestyle{plain}
\usepackage{xpatch}
\makeatletter
\newcommand*\@chaptertocentry{}% initialize \@chaptertocentry
\newcommand*\chaptertocentry[1]{\def\@chaptertocentry{#1}}% command to set \@chaptertocentry

\xpatchcmd\@include{\@input@}
  {\ifstr{\@chaptertocentry}{}{}
    {\cleardoublepage\stepcounter{chapter}\addchaptertocentry{}{\@chaptertocentry}}% add the toc entry
    \@input@
  }{}{\PatchFailed}

\xapptocmd\@include{\def\@chaptertocentry{}}{}{\PatchFailed}% reset \@chaptertocentry
\makeatother

\begin{document}
\tableofcontents

\chaptertocentry{Ch1}
\include{Sec11}
\include{Sec12}

\chaptertocentry{Ch2}
\include{Sec21}
\include{Sec22}
\end{document}

enter image description here


Update

\addtocentrydefault{chapter}{}{<chapter title>} writes \@writefile{toc}{\contentsline {chapter}{\nonumberline <chapterline>}{<pagenumber>}} to the main aux file, when the current page is shipped out (to get the right pagenumber in TOC).

\include writes immediatley an \@input command for the own aux file of the included the tex file to the main aux file. So, if the \addtocentrydefault command appears on the same page as \include the \@input for the special aux goes the main aux before the \@writefile{toc}....

The resulting main aux file of your example is:

\relax 
\providecommand*\new@tpo@label[2]{}
\@writefile{toc}{\contentsline {chapter}{\nonumberline Ch1}{1}}
\@input{Sec11.aux}
\@input{Sec12.aux}
\@input{Sec21.aux}
\@writefile{toc}{\contentsline {chapter}{\nonumberline Ch2}{5}}
\@input{Sec22.aux}
\global\@namedef{scr@dte@section@lastmaxnumwidth}{18.37163pt}

As you can see "Ch1" gets the wrong page number "1" because \addcontentsline is excecuted on the (last) page of the TOC.

In your MWE there is a \cleardoublepage before \addtocentrydefault{chapter}{}{Ch2}\stepcounter{chapter} which inserts no content in the document. So there is no page break between this command and the following \include{Sec21}: \@input{Sec21.aux} is immediatly written while \@writefile{toc}{\contentsline {chapter}{\nonumberline Ch2}{5}} is written when page 5 is shipped out. So all headings in "Sec21" appear in the TOC before "Ch2".

AFAIK this behaviour can not be changed. So you have to ensure that \@writefile{toc}... command goes to the aux file of the included tex file. This can be done by the patch in my original answer above. Alternatively package scrlfile can be used:

\documentclass{scrbook}
\pagestyle{plain}

\usepackage{scrlfile}
\newcommand\hiddenchapter[1]{%
  \cleardoublepage
  \stepcounter{chapter}%
  \addchaptertocentry{}{#1}%
}

\begin{document}
\tableofcontents

\BeforeFile{Sec11.tex}{\hiddenchapter{Ch1}}
\include{Sec11}
\include{Sec12}

\BeforeFile{Sec21.tex}{\hiddenchapter{Ch2}}
\include{Sec21}
\include{Sec22}
\end{document}

The resulting main aux file is

\relax 
\providecommand*\new@tpo@label[2]{}
\@input{Sec11.aux}
\@input{Sec12.aux}
\@input{Sec21.aux}
\@input{Sec22.aux}
\global\@namedef{scr@dte@section@lastmaxnumwidth}{18.37163pt}

and the Sec21.aux:

\relax 
\@writefile{toc}{\contentsline {chapter}{\nonumberline Ch2}{5}}
\@writefile{toc}{\contentsline {section}{\numberline {2.1}Sec21}{5}}
\@setckpt{Sec21}{
\setcounter{page}{6}
\setcounter{equation}{0}
\setcounter{enumi}{0}
\setcounter{enumii}{0}
\setcounter{enumiii}{0}
\setcounter{enumiv}{0}
\setcounter{footnote}{0}
\setcounter{mpfootnote}{0}
\setcounter{part}{0}
\setcounter{chapter}{2}
\setcounter{section}{1}
\setcounter{subsection}{0}
\setcounter{subsubsection}{0}
\setcounter{paragraph}{0}
\setcounter{subparagraph}{0}
\setcounter{figure}{0}
\setcounter{table}{0}
}
esdd
  • 85,675
  • I've run into this problem again, and appreciate your solution once more. But can you explain the underlying problem? – Why can the TOC contentsline not just be written to the .toc file immediately? – FlorianL Jun 07 '18 at 14:44
  • 1
    See my updated answer with an additional new suggestion. – esdd Jun 08 '18 at 14:27
  • Great, illustrative answer! Thanks so much! I'm sorry I cannot upvote again, I hope others will do it.. – FlorianL Jun 09 '18 at 14:56