重新定义 \chapter 以添加 \printMiniToc

重新定义 \chapter 以添加 \printMiniToc

我希望每一章\printMiniToc后面都有。但是我又不想在\printMiniToc每个后面都添加\chapter

所以我尝试使用etoolbox并尝试了这个:

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

然而这导致了此消息:

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

出现了约50次。

注意:我见过重新定义 \chapter{...} 命令时出现问题似乎是关于改变风格,因此答案似乎是这样的:

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

我认为它不适用于这种情况。

NB2:我也在使用 KOMA 脚本

NB3:这是\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

答案1

您可以更新\scr@@startchapter以附加\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}

答案2

避免修补内部和未记录的 KOMA-Script 命令。因此,这里有一个建议修补\chapterlinesformat

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

在此处输入图片描述

如果chapterprefix设置了选项,\chapterlineswithprefix则必须以相同的方式进行修补:

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

在此处输入图片描述

有和没有选项的示例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}

相关内容