4

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}
  • do you like a solution with etoc and without minitoc? – touhami Mar 15 '17 at 21:56
3

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.

  • Why is this solution only useful if "only for a few section[s]"? – Scott Wilton Mar 25 '17 at 18:43
  • @ScottWilton: Because it needs an output file handle for each section ToC and these are hard limited if you are using pdflatex. If you are using lualatex the file handles are limited by the OS only. So with lualatex you can use it for a lot of section. For pdflatex you can try the alpha package scrwfile to avoid the file handle problem. – Schweinebacke Mar 29 '17 at 6:40
2

I have looked at this briefly for usage of etoc. The main obstacle is that one of KOMA, tocbasic, or minitoc defines an xpart sectioning level which is unknown to etoc. The simplest for your usage is to let etoc ignore it (it seems to always follow a part line in the toc file hence etoc will still know where local TOC should end even if ignoring xpart) and also to prevent etoc from redefining \tableofcontents:

\let\withoutetoctableofcontents\tableofcontents
\usepackage{etoc}
\etocsetlevel{xpart}{6}% makes xpart invisible to etoc
\let\tableofcontents\withoutetoctableofcontents

\begin{document}

Then you can do your etoc local toc this way for example:

\section{Section 1.1}

\etocsetnexttocdepth{3}
\etocsettocstyle{}{}
\localtableofcontents


\lipsum

Your example thus modified (nota bene there is a comment

\setcounter{tocdepth}{-1} % only includes parts and chapters (not sections) in main TOC

which looks wrong to me, it should be 0 not -1 or at least it would be in book class, perhaps KOMA is different)

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

\let\withoutetoctableofcontents\tableofcontents
\usepackage{etoc}
\etocsetlevel{xpart}{6}
\let\tableofcontents\withoutetoctableofcontents

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

\etocsetnexttocdepth{3}
\etocsettocstyle{}{}
\localtableofcontents


\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}
  • It's minitoc that defines xpart, xchapter, and xsect levels. BTW: KOMA-Script defines \parttocdepth, \chaptertocdepth etc. so you do not need to know the numbers. – Schweinebacke Mar 22 '17 at 15:35
  • @Schweinebacke thanks for info, similar \etocsetlevel{xchapter}{6} etc would then be needed if they ever appear in the main .toc file (which is the only file etoc uses) – user4686 Mar 22 '17 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.