1

I am trying to create a table of contents for each chapter (see MNWE below). The problem occurs when the table is longer than the page length and should break. For settings, I use the tcolorbox library, which has huge customization options. I used an option breakable that should ensure this but unfortunately it doesn't. See figure below.

The content is inserted into the chapter preamble using the \setchapterpreamble. This is particularly useful - I am using a two-column format with the class option twocolumn, since the heading and the preamble are always set in a one-column layout. Probably the preamble cannot be over two pages.

If you have any other ideas on how to achieve the same effect using a different solution I would be happy for your suggestions.

\documentclass[chapterprefix=on, twocolumn, a5paper]{scrbook}
  \usepackage{xpatch}
  \usepackage{blindtext}
  \usepackage{ifthen}                    % The package's basic command is \ifthenelse

% https://tex.stackexchange.com/questions/669008/ \usepackage[most]{tcolorbox}

\makeatletter \newif\ifusechaptertoc % Switch to tell \addtocentrydefault to not only make entries % to the toc-file but also to the current section-toc-file

\newcommand{\chaptertoc}[2][\thechapter]{ % new command to generate and show a chapter toc \usechaptertoctrue % switch on chapter-toc-entries \edef\ext@chaptoc{tcc#1} % extension of the chapter-toc-file, e.g., tcc1 \DeclareNewTOC{\ext@chaptoc} % declare a new toc file %\addsec{Contents} % header of the chaptertoc \setchapterpreamble{% \begin{tcolorbox}[ breakable ] \value{tocdepth}=\subsubsectiontocdepth % we want entries down to subsection \listoftoc*{\ext@chaptoc} % show the toc without header \end{tcolorbox} % \par\nobreak\noindent\hrulefill\par % \par\bigskip\nobreak\noindent\hrulefill\par \bigskip\noindent\ignorespaces % add some vertical space after the toc and do not indent } % the following text }

\xapptocmd\addtocentrydefault{ % patch the KOMA-Script's generic toc entry generator \ifusechaptertoc % if chapter toc entries should be generated \Ifstr{#1}{chapter}{} {\expandafter\tocbasic@addxcontentsline \expandafter{\ext@chaptoc}{#1}{#2}{#3} }% do it \fi }{}{} \xpretocmd\chapter{\usechaptertocfalse}{}{} % automatically switch of chapter toc % entries at start of every \chapter \xpretocmd\part{\usechaptertocfalse}{}{} % entries in chapter toc are automatically % switched off at start of \part \makeatother

\newcommand\setchaptertoc[1][]{% \Ifstr{#1}{} {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc}} {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc[#1]}}% } % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \setcounter{tocdepth}{\partnumdepth}

\begin{document} \tableofcontents \setchaptertoc \chapter{My chapter} \blindtext \section{My section I} \subsection{My subsection I} \blindtext \section{My section II} \subsection{My subsection I} \subsubsection{My subsubsection II} \subsection{My subsection II} \subsubsection{My subsubsection II} \subsubsection{My subsubsection III} \subsection{My subsection III} \section{My section III} \section{My section IV} \subsection{My subsection I} \subsection{My subsection I} \subsection{My subsection II} \section{My section V} \subsection{My subsection I} \subsection{My subsection I} \subsection{My subsection III} \section{My section V} \subsection{My subsection I} \subsection{My subsection I} \subsection{My subsection III} \section{My section V} \section{My section V} \subsection{My subsection I} \subsection{My subsection II} \subsection{My subsection III} \section{My section V} \section{My section VI} \section{My section VII} \section{My section VIII} \section{My section IV} \section{My section V} \section{My section VI} \end{document}

enter image description here

JardaFait
  • 3,922
  • you can try the approach given https://tex.stackexchange.com/questions/235932/minitoc-for-each-section-in-documentclass-book – Damitr Aug 13 '23 at 10:21
  • @Damitr Using titletoc with a KOMA-Script class is not recommended. Moreover, the answer to the related question is AFAIK not for twocolumn documents with column spanning chapter toc. If I'm wrong, please show how to use the approach for column spanning framed chapter toc with page break (ideally using class scrbook). – cabohah Aug 15 '23 at 08:35

1 Answers1

1

KOMA-Script uses the optional argument of \@topnewpage (internal command also used by \twocolumn) to print the column spanning chapter title and preamble in twocolumn documents. The LaTeX kernel uses a box for this argument and therefore does not support page breaks inside this argument. You would need to use onecolumn mode and package multicol for the content of every chapter. But AFAIK \setchapterpreamble still would not work, because it also uses an explicit box for the preamble.

Here is an example without automatism for printing the chapter toc and switching to two column:

\documentclass[chapterprefix=on,twocolumn,a5paper]{scrbook}
\usepackage{multicol}
  \usepackage{xpatch}
  \usepackage{blindtext}
  \usepackage{ifthen}                    % The package's basic command is \ifthenelse

% https://tex.stackexchange.com/questions/669008/ \usepackage[most]{tcolorbox}

\makeatletter \newif\ifusechaptertoc % Switch to tell \addtocentrydefault to not only make entries % to the toc-file but also to the current section-toc-file

\newcommand{\chaptertoc}[2][\thechapter]{ % new command to generate and show a chapter toc \usechaptertoctrue % switch on chapter-toc-entries \edef\ext@chaptoc{tcc#1} % extension of the chapter-toc-file, e.g., tcc1 \DeclareNewTOC{\ext@chaptoc} % declare a new toc file %\addsec{Contents} % header of the chaptertoc }

\newcommand\printchaptertoc{% \ifusechaptertoc \begin{tcolorbox}[ breakable ] \value{tocdepth}=\subsubsectiontocdepth % we want entries down to subsection \listoftoc*{\ext@chaptoc} % show the toc without header \end{tcolorbox} % \par\nobreak\noindent\hrulefill\par % \par\bigskip\nobreak\noindent\hrulefill\par \bigskip\noindent\ignorespaces % add some vertical space after the toc and do not indent \fi } % the following text

\xapptocmd\addtocentrydefault{ % patch the KOMA-Script's generic toc entry generator \ifusechaptertoc % if chapter toc entries should be generated \Ifstr{#1}{chapter}{} {\expandafter\tocbasic@addxcontentsline \expandafter{\ext@chaptoc}{#1}{#2}{#3} }% do it \fi }{}{} \xpretocmd\chapter{\usechaptertocfalse}{}{} % automatically switch of chapter toc % entries at start of every \chapter \xpretocmd\part{\usechaptertocfalse}{}{} % entries in chapter toc are automatically % switched off at start of \part \makeatother

\newcommand\setchaptertoc[1][]{% \Ifstr{#1}{} {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc}} {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc[#1]}}% }

\setcounter{tocdepth}{\partnumdepth}

\begin{document} \tableofcontents \onecolumn \setchaptertoc \chapter{My chapter} \printchaptertoc \begin{multicols}{2}

\blindtext
\section{My section I}
  \subsection{My subsection I}
    \blindtext
\section{My section II}
  \subsection{My subsection I}
    \subsubsection{My subsubsection II}
  \subsection{My subsection II}
    \subsubsection{My subsubsection II}
    \subsubsection{My subsubsection III}
  \subsection{My subsection III}
\section{My section III}
\section{My section IV}
  \subsection{My subsection I}
  \subsection{My subsection I}
  \subsection{My subsection II}
\section{My section V}
  \subsection{My subsection I}
  \subsection{My subsection I}
  \subsection{My subsection III}
\section{My section V}
  \subsection{My subsection I}
  \subsection{My subsection I}
  \subsection{My subsection III}
\section{My section V}
\section{My section V}
  \subsection{My subsection I}
  \subsection{My subsection II}
  \subsection{My subsection III}
\section{My section V}
\section{My section VI}
\section{My section VII}
\section{My section VIII}
\section{My section IV}
\section{My section V}
\section{My section VI}
\end{multicols}

\end{document}

enter image description here

Maybe you can also find hooks to automatically add \printchaptertoc, \begin{multicols}{2} and \end{multicols}.

Note: multicols does handle floats like figure and table different. See the manual for more information.

cabohah
  • 11,455
  • Your code works well. Maybe that's for a separate question. Do you know how to avoid the message: Class scrbook Warning: \float@addtolists detected! Implementation of \float@addtolist became deprecated in KOMA-Script v3.01 2008/11/14 and has been replaced by several more flexible features of package tocbasic. – JardaFait Aug 15 '23 at 19:32
  • 1
  • Do you have some idea how to involve tocbasic as a solution for floated TOC? I have no idea how to deal with this. I didn't want to hide the error with scrhack. I believe there is a clean solution here. – JardaFait Aug 16 '23 at 16:01
  • @JardaFait “Floated TOC”? Sorry, I do not understand. Maybe you should ask a new question. BTW: scrhack does not simply hide the warning, it adds new features to several packages and therefore, i.e., avoids the warning. – cabohah Aug 17 '23 at 06:28