External packages for minitoc's are incompatible with KOMA-script. There are workarounds (1), (2) but a workaround sometimes calls for trouble at the next update.

How can I generate a main table of contents plus a table of contents per chapter without additional packages in plain KOMA?


2 Answers 2


Here is a suggestion that adapts the answer of @Schweinebacke to Create section-level toc in a KOMA-script document that already uses minitoc for chapter tocs.

It uses package scrwfile to avoid trouble with the limited number of write handles. Additionally I have added an optional argument for the postfix of the file extension. So it can be used for \addchap too.

% see https://tex.stackexchange.com/a/359758

\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}[1][\thechapter]{% new command to generate and show a chapter toc
  \usechaptertoctrue% switch on chapter-toc-entries
  \edef\ext@subtoc{toc#1}% extension of the section-toc-file, e.g., toc1
  \DeclareNewTOC{\ext@subtoc}% declare a new toc file
  \addsec*{Contents}% header of the chaptertoc
    \value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
    \listoftoc*{\ext@subtoc}% show the toc without header
  \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
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@subtoc}{#1}{#2}{#3}% do it
\xpretocmd\chapter{\usechaptertocfalse}{}{}% automatically switch of chapter toc entries at start of every \chapter


\addsec{Unnumbered I}
\addsec{Unnumbered II}





enter image description here


You can parse the .toc file generated by KOMA-Script and divide it up by chapters.



% Works with or without hyperref.

  % Macros for resetting and appending to the variables that store the chapter tocs.
    \expandafter\gdef\csname @chaptoc@toc@\@chaptoc@chapter@num\endcsname{}%
    \expandafter\gappto\csname @chaptoc@toc@\@chaptoc@chapter@num\endcsname
  % The parsing macro, that goes through the toc file and creates the chapter toc macros.
    % We only need to do this once.
  % A macro for printing the current chapter toc.
      \parindent 0pt%
      \csname @chaptoc@toc@\arabic{chapter}\endcsname
  % Set everything up.
    % When hyperref is loaded, \contentsline has one more argument.
    % When the aux file is read in the first time, the toc file will be parsed.
    % Automatically print the chapter toc after each chapter title.





  This line was manually added!%


MWE output

  • I use catchfile to save the content of the .toc file in a macro before parsing it. This way I can easily insert parsing macros right before and after the .toc content.
  • Discontinuous chapter numbering and manually inserted code are supported. (Though you can certainly break it if you try.)
  • hyperref is supported
  • By default, the chapter tocs have the same depth as the main toc, but you can change that by redefining \chaptocdepth.
  • By default, nothing is added after the chapter toc, but you can change that using \chaptocafter. Reasonable options could be, for example, \chaptocafter{\section*{}}, \chaptertocafter{\clearpage} or some \vskip, depending on your document.

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.