答案1
这是一个建议,将@Schweinebacke 的答案改编为在已经使用 minitoc 的 KOMA 脚本文档中创建节级目录用于章节目录。
它使用包scrwfile
来避免写句柄数量有限带来的麻烦。此外,我还为文件扩展名的后缀添加了一个可选参数。因此它也可以用于\addchap
。
% 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}
结果:
答案2
您可以解析.toc
KOMA-Script 生成的文件并按章节进行划分。
\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}
- 我用
catchfile
在解析文件内容.toc
之前将其保存在宏中。这样我就可以轻松地在.toc
内容之前和之后插入解析宏。 - 支持不连续章节编号和手动插入代码。(尽管如果你尝试的话肯定会破坏它。)
hyperref
受支持- 默认情况下,章节目录具有与主目录相同的深度,但您可以通过重新定义来更改这一点
\chaptocdepth
。 - 默认情况下,章节目录后不会添加任何内容,但您可以使用 进行更改
\chaptocafter
。合理的选项可能是,例如,\chaptocafter{\section*{}}
或\chaptertocafter{\clearpage}
某些\vskip
,具体取决于您的文档。