KOMA-script:在目录中添加单词“部分”和“章节”

KOMA-script:在目录中添加单词“部分”和“章节”

如何在目录中将单词“部分”添加到部分条目中,将单词“章节”添加到章节条目中?

\documentclass[chapterprefix]{scrbook}

\usepackage[english]{babel}

\begin{document}

\tableofcontents

\part{This is a part}
\chapter{This is a chapter}
\section{This is a section}

\end{document} 

答案1

\documentclass[chapterprefix]{scrbook}

\usepackage[english]{babel}

\renewcommand*{\addparttocentry}[2]{%
  \addtocentrydefault{part}{\partname\nobreakspace #1}{#2}%
}%

\makeatletter

\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{\chaptername\nobreakspace #1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{\scr@float@addtolists@warning}%
  \fi
}

\makeatother

\usepackage[tocindentauto]{tocstyle}
\usetocstyle{KOMAlike}

\begin{document}

\tableofcontents

\part{This is a part}
\chapter{This is a chapter}
\section{This is a section}

\end{document}

在此处输入图片描述

tocindentauto当您使用包中的选项时,请不要忘记至少运行 LaTeX 三次tocstyle(请参阅手册第 3 页):

使用选项时,tocindentauto目录的所有宽度均通过 计算 tocstyle。宽度的计算需要至少一次包含所有目录条目的 LaTeX 运行。因此,您至少需要三次 LaTeX 运行

答案2

这里还有另一个建议,如果存在未编号的章节或未编号的部分或附录,则该建议仍然有效。

\documentclass[
  chapterprefix,
  %numbers=noenddot,
  %toc=indentunnumbered,
  %listof=totoc,
  %listof=chapterentry
]{scrbook}

\usepackage[english]{babel}

\RedeclareSectionCommand[
    tocnumwidth=4em
]{part}
\RedeclareSectionCommand[
    tocnumwidth=6em
]{chapter}
\RedeclareSectionCommand[
    tocindent=6em
]{section}
\RedeclareSectionCommand[
    tocindent=8em
]{subsection}

\let\oldaddchaptertocentry\addchaptertocentry
\renewcommand{\addchaptertocentry}[2]{%
  \ifstr{#1}{}{%
    \oldaddchaptertocentry{#1}{#2}}{%
    \oldaddchaptertocentry{\chapapp{} #1}{#2}%
}}
\let\oldaddparttocentry\addparttocentry
\renewcommand{\addparttocentry}[2]{%
  \ifstr{#1}{}{%
    \oldaddparttocentry{#1}{#2}}{%
    \oldaddparttocentry{\partname{} #1}{#2}%
}}

\usepackage{blindtext}
\begin{document}
\frontmatter
\tableofcontents
\listoftables
\chapter{This is a chapter in frontmatter}
\mainmatter
\part{This is a part}
\blinddocument
\begin{table}%
  \begin{tabular}{l}
  \Huge X
  \end{tabular}
\caption{A table}
\end{table}
\addchap{This is an unnumbered chapter}
\addsec{This is an unnumbered section}
\appendix
\blinddocument
\end{document}

结果:

在此处输入图片描述


更新使用 KOMA-Script 版本 3.20(或更新版本)

\documentclass[chapterprefix]{scrbook}[2016/05/10]

\newcommand\partentrynumberformat[1]{\partname\ #1}
\RedeclareSectionCommand[
  tocentrynumberformat=\partentrynumberformat,
  tocnumwidth=4em
]{part}

\newcommand\chapterentrynumberformat[1]{\chapapp\ #1}
\RedeclareSectionCommand[
    tocentrynumberformat=\chapterentrynumberformat,
    tocnumwidth=5.5em
]{chapter}

\RedeclareSectionCommand[tocindent=5.5em]{section}
\RedeclareSectionCommand[tocindent=7.8em]{subsection}

\usepackage{blindtext}
\begin{document}
\frontmatter
\tableofcontents
\listoftables
\chapter{This is a chapter in frontmatter}
\mainmatter
\part{This is a part}
\blinddocument
\begin{table}%
  \begin{tabular}{l}
  \Huge X
  \end{tabular}
\caption{A table}
\end{table}
\addchap{This is an unnumbered chapter}
\addsec{This is an unnumbered section}
\appendix
\blinddocument
\end{document}

在此处输入图片描述

答案3

这是一个解决方案

\documentclass[chapterprefix]{scrbook}

\usepackage[english]{babel}
\makeatletter
\let\oldaddchaptertocentry\addchaptertocentry
\renewcommand{\addchaptertocentry}[2]{%
\oldaddchaptertocentry{}{\@chapapp{} #1. #2}}
\let\oldaddparttocentry\addparttocentry
\renewcommand{\addparttocentry}[2]{%
\oldaddchaptertocentry{}{\partname{} #1. #2}}
\makeatother
\begin{document}

\tableofcontents

\part{This is a part}
\chapter{This is a chapter}
\section{This is a section}

\part{This is a part}
\chapter{This is a chapter}
\section{This is a section}
\appendix

\part{This is a part}
\chapter{This is a chapter}
\section{This is a section}
\end{document}

在此处输入图片描述

答案4

由于 tocstyle 包已弃用,请使用 tocbasic :

\usepackage{tocbasic}
\DeclareTOCStyleEntry[
  entrynumberformat=\entrynumberwithprefix{\chaptername},
  dynnumwidth
]{tocline}{chapter}

\DeclareTOCStyleEntry[
  entrynumberformat=\entrynumberwithprefix{\partname},
  dynnumwidth
]{tocline}{part}
\newcommand\entrynumberwithprefix[2]{#1\enspace#2\hfill}

相关内容