# Redefine \chapter to add \printMiniToc

I want every chapter to have \printMiniToc after it. However I don't want to have to add \printMiniToc after every \chapter.

So I tried using etoolbox and tried this:

xapptocmd{\chapter}{\printMiniToc}{\message{** patching of \string\chapter succeeded **}}{\message{** patching of \string\chapter failed **}}


However that's resulted in this message:

Argument of \select@group has an extra }.
Paragraph ended before \select@group was


NB: I've seen Problem‎ with redefining \chapter{...} command‎ seems to be about changing the style and so the answers seem to be along the line of:

\RequirePackage{etoolbox}[2010/09/12]%
\patchcmd{\chapter}{plain}{empty}{%
\wlog{* Chapter patched by \string\patchcmd.}%
}{%


Which I can't see applying in this case.

NB2: I'm also using KOMA-script

NB3: This is \printMiniToc

\makeatletter
\newcommand{\printMiniToc}{
\vfill
\hspace*{3.2cm}\parbox[t]{\textwidth-4cm}{
\hspace*{-5mm}\raisebox{0.25mm}{\fontsize{0.6cm}{1ex}\selectfont    \color{contrastColour}\faList}
\hspace*{5.5mm} {\huge{Summary}}
\vspace*{2mm}
\startcontents[chapters]
\begin{spacing}{1}
\printcontents[chapters]{p}{1}{\setcounter{tocdepth}{2}}
\end{spacing}
}
\vfill
\newpage
}
\makeatother

• What package or setup do you use to produce \printMiniToc? Can you provide a minimal example? – Werner Nov 13 '18 at 4:16
• I added the info :) – tecosaur Nov 13 '18 at 4:18
• So you're using this template... – Werner Nov 13 '18 at 4:22
• Yep! Don't tell me you got that just from a google search? – tecosaur Nov 13 '18 at 5:56

You can update \scr@@startchapter to append \printMiniToc:

\documentclass{scrreprt}

\usepackage{lipsum}
\usepackage{titletoc}

\makeatletter
\newcommand{\printMiniToc}{
\startcontents[chapters]
\printcontents[chapters]{p}{1}{\setcounter{tocdepth}{2}}
}

\let\oldscr@@startchapter\scr@@startchapter
\def\scr@@startchapter#1[#2]#3{%
\oldscr@@startchapter{#1}[#2]{#3}%
\printMiniToc
}
\makeatother

\begin{document}

\tableofcontents

\sloppy
\chapter{A chapter}\lipsum[1-10]
\section{First section}\lipsum[11-20]
\section{Second section}\lipsum[21-30]
\section{Third section}\lipsum[31-40]
\section{Last section}\lipsum[41-50]

\end{document}

• Worked like a charm :) – tecosaur Nov 13 '18 at 5:55

Avoid patching internal and undocumented KOMA-Script commands. So here is a suggestion patching \chapterlinesformat:

\usepackage{xpatch}
\xapptocmd{\chapterlinesformat}{%
\ifstr{#1}{chapter}{\ifstr{#2}{}{}{%
\normalfont\normalsize\printMiniToc
\vspace{-\baselineskip}%
}}{}}{}{\PatchFailed}


If option chapterprefix is set, \chapterlineswithprefix must be patched in the same way:

\usepackage{xpatch}
\xapptocmd{\chapterlinesformat}{%
\ifstr{#1}{chapter}{\ifstr{#2}{}{}{%
\normalfont\normalsize\printMiniToc
\vspace{-\baselineskip}%
}}{}}{}{\PatchFailed}


Example which works with and without option chapterprefix:

\documentclass[
%chapterprefix
]{scrreprt}
\usepackage{lipsum}
\usepackage{titletoc}

\newcommand{\printMiniToc}{
\startcontents[chapters]
\printcontents[chapters]{p}{1}{\setcounter{tocdepth}{2}}
}
\ifstr{#1}{chapter}{\ifstr{#2}{}{}{%
\normalfont\normalsize\printMiniToc
\vspace{-\baselineskip}%
}}{}%
}
\usepackage{xpatch}

• For some reason with my document, this doesn't seem to successfully prepend \printMiniToc — though I can see it clearly works in yours. The full 'context' of my excerpt can be found here github.com/tecosaur/mgdt/blob/master/mgdt.cls if that's of any use. – tecosaur Nov 14 '18 at 2:20
• Note the warnings regarding the usage of package titlesec together with a KOMA-Script class. This package breaks some of the KOMA possibilities (option settings etc.) – esdd Nov 14 '18 at 8:20