# How to write a minitoc with plain KOMA-script?

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

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
\documentclass{scrreprt}
\usepackage{blindtext}
\usepackage{scrwfile}
\usepackage{hyperref}

\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}[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
\begin{minipage}{.9\linewidth}
\value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
\listoftoc*{\ext@subtoc}% show the toc without header
\end{minipage}\par
\bigskip\noindent\ignorespaces% 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
\ifusechaptertoc% if chapter toc entries should be generated
\expandafter\tocbasic@addxcontentsline\expandafter{\ext@subtoc}{#1}{#2}{#3}% do it
\fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{}% automatically switch of chapter toc entries at start of every \chapter
\makeatother

\begin{document}
\tableofcontents

\addchap{Introduction}
\chaptertoc[Intro]
\blindtext
\addsec{Unnumbered I}
\Blindtext
\addsec{Unnumbered II}
\blindtext

\chapter{one}
\chaptertoc
\blindtext
\section{two}\Blindtext
\section{three}\blindtext
\subsection{four}\Blindtext
\subsection{five}\blindtext
\section{six}\blindtext

\chapter{seven}
\chaptertoc
\section{eight}\Blindtext
\section{nine}\Blindtext

\end{document}


Result:

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

\documentclass{scrreprt}

\usepackage{etoolbox}
\usepackage{catchfile}

% Works with or without hyperref.
%\usepackage{hyperref}

\makeatletter
% Macros for resetting and appending to the variables that store the chapter tocs.
\def\@chaptoc@toc@reset{%
\expandafter\gdef\csname @chaptoc@toc@\@chaptoc@chapter@num\endcsname{}%
}
\def\@chaptoc@toc@append{%
\expandafter\gappto\csname @chaptoc@toc@\@chaptoc@chapter@num\endcsname
}
% The parsing macro, that goes through the toc file and creates the chapter toc macros.
\def\@chaptoc@parse@tocfile{%
% We only need to do this once.
\let\@chapter@parse@tocfile\relax
\begingroup
\def\@chaptoc@chapter@num{0}%
\@chaptoc@toc@reset
\makeatletter
\IfFileExists{\jobname.toc}{%
\CatchFileDef\@tmpa{\jobname.toc}{}%
\expandafter\@chaptoc@parse@head
\@tmpa
\contentsline\@chaptoc@parse@stop%
}{}%
\endgroup
}
\def\@chaptoc@chapter@string{chapter}
\def\@chaptoc@parse@stop{\@chaptoc@parse@stop}%
\long\def\@chaptoc@parse@head#1\contentsline#2{%
\@chaptoc@toc@append{#1}%
\ifx\@chaptoc@parse@stop#2%
\expandafter\@gobble
\else
\begingroup
\def\@tmpa{#2}%
\ifx\@tmpa\@chaptoc@chapter@string
\endgroup
\expandafter\expandafter
\expandafter\@chaptoc@parse@cline@chapter
\else
\endgroup
\expandafter\expandafter
\expandafter\@chaptoc@parse@cline@other
\fi
\fi
{#2}%
}
\def\@chaptoc@parse@cline@other{%
\@chaptoc@toc@append{\contentsline}%
\@chaptoc@parse@head
}
\def\@chaptoc@parse@cline@chapter#1#2#3{%
\begingroup
\def\numberline##1##2\@nil{%
\endgroup
\def\@chaptoc@chapter@num{##1}%
}%
#2\@nil
\@chaptoc@toc@reset
\expandafter\@chaptoc@parse@head\@chaptoc@parse@gobble@hyperrefargs
}
% A macro for printing the current chapter toc.
\def\@chaptoc@toc@print{%
\begingroup
\setcounter{tocdepth}{\chaptocdepth}%
\parindent 0pt%
\section*{\contentsname}%
\csname @chaptoc@toc@\arabic{chapter}\endcsname
\@chaptoc@toc@afterhook
\endgroup
}
\newcommand*\chaptocdepth{\value{tocdepth}}
\def\@chaptoc@toc@afterhook{}
\newcommand\chaptocafter[1]{%
\def\@chaptoc@toc@afterhook{#1}%
}
% Set everything up.
\AtBeginDocument{%
% When hyperref is loaded, \contentsline has one more argument.
\@ifpackageloaded{hyperref}{%
\protected@write\@auxout{}{\let\protect\@chaptoc@parse@gobble@hyperrefargs\protect\@gobble}%
}{%
\protected@write\@auxout{}{\let\protect\@chaptoc@parse@gobble@hyperrefargs\protect\@empty}%
}%
% When the aux file is read in the first time, the toc file will be parsed.
\protected@write\@auxout{}{%
\protect\@chaptoc@parse@tocfile
}%
% Automatically print the chapter toc after each chapter title.
\apptocmd\scr@@startchapter{\@chaptoc@toc@print}{}{}%
}
\makeatother

\begin{document}

\tableofcontents

\chapter{one}
\section{two}
\section{three}
\subsection{four}
\subsection{five}
\section{six}

\setcounter{chapter}{5}
\chapter{seven}
\section{eight}
\section{nine}

\chapter{ten}
\section{eleven}
\subsection{twelve}
\addtocontents{toc}{%
\vskip\protect\baselineskip\hrule\vskip\protect\baselineskip
This line was manually added!%
\vskip\protect\baselineskip\hrule\vskip\protect\baselineskip
}
\subsection{thirteen}
\section{fourteen}

\end{document}


• 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.