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}


titletocwith 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 classscrbook). – cabohah Aug 15 '23 at 08:35