[Tex/LaTex] Create section-level toc in a KOMA-script document that already uses minitoc

koma-scriptminitocscrbooktable of contents

I have a long document which uses the KOMA-Script book class scrbook and I would like to add a section-level table of contents to a few rather long sections in my document. I am already using the minitoc package to provide part TOCs. The minitoc package also has a secttoc command, but this cannot be used along with the book class.

I have already tried using the etoc package, with the following code:

\section{My Section}
\etocsettocstyle{}{}
\etocsetnexttocdepth{3}
\localtableofcontents*

however, this seems to conflict with something in either minitoc or scrbook and didn't work.

Other resources seem to indicate that I might be able to use the built in tocbasic which is part of KOMA-Script, but I was not able to figure out how that would work from the documentation.

MWE:

\documentclass{scrbook}

%%% Language support %%%
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{lipsum} % For MWE

\setcounter{secnumdepth}{3} % makes subsubsections numbered

%%% TOC custimization %%%
\usepackage{minitoc}
\setcounter{tocdepth}{-1} % only includes parts and chapters (not sections) in main TOC
\mtcsetdepth{parttoc}{1} % only includes chapters and sections (not subsections) in part TOCs

\usepackage[tocindentauto]{tocstyle} % prettier TOC
\usetocstyle{allwithdot} %use 'KOMAlike" if you don't want dots

\mtcsettitle{parttoc}{Contents} %renames the part TOC to match main TOC

\usepackage{hyperref}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}


\begin{document}
\doparttoc

\begin{titlepage}
\centering
\ \\
\vspace{5cm}
{\Huge Title}
\end{titlepage}

\thispagestyle{empty}
\tableofcontents


\mainmatter
\part{Part 1}
\parttoc
\chapter{Chapter 1}
\section{Section 1.1}
%insert Sect TOC HERE
\lipsum
  \subsection{Sub Sec 1.1.1}
  \lipsum
    \subsubsection{Sub Sub Sec 1.1.1.1}
    \lipsum
    \subsubsection{Sub Sub Sec 1.1.1.2}
    \lipsum
    \subsubsection{Sub Sub Sec 1.1.1.3}
    \lipsum
  \subsection{Sub Sec 1.1.2}
  \lipsum
  \subsection{Sub Sec 1.1.3}
  \lipsum
  \subsection{Sub Sec 1.1.4}
  \lipsum
    \subsubsection{Sub Sub Sec 1.1.4.1}
    \lipsum
    \subsubsection{Sub Sub Sec 1.1.4.2}
    \lipsum
    \subsubsection{Sub Sub Sec 1.1.4.3}
    \lipsum
  \subsection{Sub Sec 1.1.5}
  \lipsum
\section{Section 1.2}

\chapter{Chapter 2}
\lipsum

\part{Part 2}
\parttoc
\chapter{Chapter 3}
\lipsum

\end{document}

Best Answer

If you need such a section table of contents only for a few section (or if you use lualatex that does not have TeX's file handle restrictions) you could indeed use tocbasic. Add this to the end of your preamble:

\makeatletter
\newif\ifusesectiontoc% Switch to tell \addtocentrydefault to not only make entries to the toc-file but also to the current section-toc-file
\newcommand*{\sectiontoc}{% new command to generate and show a section toc
  \usesectiontoctrue% switch on section-toc-entries
  \edef\ext@subtoc{toc\thesection}% extension of the section-toc-file, e.g., toc1.1
  \expandafter\DeclareNewTOC\expandafter{\ext@subtoc}% declare a new toc file
  \begin{minipage}{.9\linewidth}
    \value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
    \expandafter\listoftoc\expandafter*\expandafter{\ext@subtoc}% show the toc without any heading
  \end{minipage}\par
  \bigskip\noindent% add some vertical space after the toc and do not indent the following text
}
\usepackage{xpatch}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
  \ifusesectiontoc% if section toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@subtoc}{#1}{#2}{#3}% do it
  \fi
}{}{}
\xpretocmd\section{\usesectiontocfalse}{}{}% automatically switch of section toc entries at start of every \section
\makeatother

Then use \sectiontoc right after the \section that should have a section toc. This will produce, e.g.,

enter image description here

Note: You cannot use \sectiontoc for not numbered section, e.g., made by \addsec or \section*. If you need such section ToCs, you cannot use \thesection for the file extension.