使用 Koma-script 定义每章的目录

使用 Koma-script 定义每章的目录

我尝试使用@esddd 在问题中建议的解决方案“通过 KOMA-script 替换 minitoc 包样式的可能性”,但不幸的是我遇到了以下问题:

在此处输入图片描述

如上所示,在为章节定义的目录中,还添加了以下部分的条目。如何在 MNWE 代码中抑制上述部分的条目?请参阅 MNWE:

微电子工程协会

 \documentclass[fontsize=10pt,open=any,twocolumn]{scrbook}[2019/10/12]% needs version 3.27 or newer
\usepackage{blindtext}
\usepackage{scrwfile}% avoid trouble with the limited number of write handles
\usepackage{xpatch}


% https://tex.stackexchange.com/a/359758
% https://tex.stackexchange.com/a/502077
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a chapter toc
\useparttoctrue% switch on part-toc-entries
\edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
\DeclareNewTOC[
    listname=My Content,
    %unset=onecolumn% if the part toc should use twocolumn
]{\ext@parttoc}% declare a new toc file
\begingroup
    \value{tocdepth}=\chaptertocdepth% we want entries down to chapter
    \listoftoc{\ext@parttoc}% show the toc with header
\endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifuseparttoc% if part toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@parttoc}{#1}{#2}{#3}% do it
\fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% automatically switch of part toc entries at start of every \part

\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}[2][\thechapter]{% new command to generate and show a chapter toc
\usechaptertoctrue% switch on chapter-toc-entries
\edef\ext@chaptoc{tcc#1}% extension of the chapter-toc-file, e.g., tcc1
\DeclareNewTOC{\ext@chaptoc}% declare a new toc file
%\addsec*{Contents}% header of the chaptertoc
\setchapterpreamble{%
    \begin{minipage}{\linewidth}
    \hrulefill\par
    \value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
    \listoftoc*{\ext@chaptoc}% show the toc without header
    \end{minipage}%
    \par\bigskip\nobreak\noindent\hrulefill\par
    \bigskip\noindent\ignorespaces% add some vertical space after the toc and do not indent the following text
}%
}
% \xpretocmd{⟨command⟩}{⟨prepend⟩}{⟨success⟩}{⟨failure⟩}
% \xapptocmd{⟨command⟩}{⟨append⟩}{⟨success⟩}{⟨failure⟩}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifusechaptertoc% if chapter toc entries should be generated
    \ifstr{#1}{chapter}{}
    {\expandafter\tocbasic@addxcontentsline\expandafter{\ext@chaptoc}{#1}{#2}{#3}}% do it
\fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{}% automatically switch of chapter toc entries at start of every \chapter
\makeatother

\newcommand\setchaptertoc[1][]{%
\ifstr{#1}{}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc}}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc[#1]}}%
}

\setcounter{tocdepth}{\partnumdepth}% depth of TOC
\begin{document}
\tableofcontents

\part{My Part I}
\parttoc
\setchaptertoc
    \chapter{My chapter I}
        \section{My first section}
            \blindtext
        \subsection{My first subsection}
            \blindtext
        \section{My second section}
            \blindtext

\part{My Part II}
\parttoc
\setchaptertoc
    \chapter{My chapter II}
        \section{My first section}
            \blindtext
            \subsection{My first subsection}
                \blindtext
        \section{My second section}
            \blindtext
\end{document}

代码有详细的文档,但我仍然不明白它为什么会这样工作。

答案1

\xpretocmd\part{\usechaptertocfalse}{}{}其他答案中缺少了这一点。

\documentclass[fontsize=10pt,open=any,twocolumn]{scrbook}[2019/10/12]% needs version 3.27 or newer
\providecommand*{\Ifstr}{\ifstr}% added because of changes in upcoming KOMA-Script version 3.28 and \ifstr replaced by \Ifstr in the code (see https://komascript.de/faq_deprecatedif) 
\usepackage{blindtext}
\usepackage{scrwfile}% avoid trouble with the limited number of write handles
\usepackage{xpatch}

% https://tex.stackexchange.com/a/359758
% https://tex.stackexchange.com/a/502077
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a chapter toc
\useparttoctrue% switch on part-toc-entries
\edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
\DeclareNewTOC[
    listname=My Content,
    %unset=onecolumn% if the part toc should use twocolumn
]{\ext@parttoc}% declare a new toc file
\begingroup
    \value{tocdepth}=\chaptertocdepth% we want entries down to chapter
    \listoftoc{\ext@parttoc}% show the toc with header
\endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifuseparttoc% if part toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@parttoc}{#1}{#2}{#3}% do it
\fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% automatically switch of part toc entries at start of every \part

\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}[2][\thechapter]{% new command to generate and show a chapter toc
\usechaptertoctrue% switch on chapter-toc-entries
\edef\ext@chaptoc{tcc#1}% extension of the chapter-toc-file, e.g., tcc1
\DeclareNewTOC{\ext@chaptoc}% declare a new toc file
%\addsec*{Contents}% header of the chaptertoc
\setchapterpreamble{%
    \begin{minipage}{\linewidth}
    \hrulefill\par
    \value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
    \listoftoc*{\ext@chaptoc}% show the toc without header
    \end{minipage}%
    \par\bigskip\nobreak\noindent\hrulefill\par
    \bigskip\noindent\ignorespaces% add some vertical space after the toc and do not indent the following text
}%
}
% \xpretocmd{command)}{(prepend⟩}{⟨success⟩}{⟨failure⟩}
% \xapptocmd{⟨command⟩}{⟨append⟩}{⟨success⟩}{⟨failure⟩}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifusechaptertoc% if chapter toc entries should be generated
    \Ifstr{#1}{chapter}{}
    {\expandafter\tocbasic@addxcontentsline\expandafter{\ext@chaptoc}{#1}{#2}{#3}}% do it
\fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \chapter
\xpretocmd\part{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \part
\makeatother

\newcommand\setchaptertoc[1][]{%
  \Ifstr{#1}{}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc}}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc[#1]}}%
}

\setcounter{tocdepth}{\partnumdepth}% depth of TOC
\begin{document}
\tableofcontents

\part{My Part I}
\parttoc
\setchaptertoc
\chapter{My chapter I}
\section{My first section}
\blindtext
\subsection{My first subsection}
\blindtext
\section{My second section}
\blindtext

\part{My Part II}
\parttoc
\setchaptertoc
\chapter{My chapter II}
\section{My first section}
\blindtext
\subsection{My first subsection}
\blindtext
\section{My second section}
\blindtext
\end{document}

在此处输入图片描述

相关内容