1

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 

Which appears about 50 times.

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]
        \hypersetup{linkcolor=black}
        \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
1

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

enter image description here

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

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

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

enter image description here

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

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

enter image description here

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}}
}
\newcommand{\addMiniTocToChapter}[2]{%
  \ifstr{#1}{chapter}{\ifstr{#2}{}{}{%
    \par\chapterheadendvskip
    \normalfont\normalsize\printMiniToc
    \vspace{-\baselineskip}%
  }}{}%
}
\usepackage{xpatch}
\xapptocmd{\chapterlinesformat}{\addMiniTocToChapter{#1}{#2}}{}{\PatchFailed}
\xapptocmd{\chapterlineswithprefixformat}{\addMiniTocToChapter{#1}{#2}}{}{\PatchFailed}

\begin{document}
\tableofcontents
\chapter{A chapter}\lipsum[1-5]
\section{First section}\lipsum[6-10]
\section{Second section}\lipsum[11-15]
\section{Third section}\lipsum[16-20]
\section{Last section}\lipsum[21-25]
\end{document}
  • 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

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.