3

How can I add the word "Part" to the part entry and "Chapter" to chapter entries in the ToC?

\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} 
3
\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}

enter image description here

Do not forget to run LaTeX at least three times when you are using the tocindentauto option from the tocstyle package (see p. 3 of the manual):

With option tocindentauto all widths at the TOCs are calculated by tocstyle. The calculation of the width needs at least one LaTeX run with all TOC entries. So you need at least three LaTeX runs

4

Here is another suggestion that still works if there is an unnumbered chapter or an unnumbered part or an appendix.

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

Result:

enter image description here


Update using KOMA-Script Version 3.20 (or newer)

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

enter image description here

2

Here is a solution

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

enter image description here

  • Thank you for your solution! Although I mentioned in the comment above that I would rather not use any additional packages I will accept Thorsten's answer which uses the tocstyle package. The reason for that comment was that I do not want to use packages which possibly interfere with the KOMA-script classes. Since the tocstyle package has been developed by the author of the KOMA-script classes there will not be any problems of compatibility but moreover this package offers me the possibility to easily change the appearance of the ToC without putting too much effort in searching the commands. – Tom Feb 27 '16 at 17:12
0

(RUS) С учетом предложенных выше решений, я для себя нашёл ответ на данный вопрос. Он учитывает, что:

  1. В русском языке слово "Приложение" намного больше слова "Глава", поэтому одинаковые отступы выглядят не красиво
  2. Главы бывают ненумерованными
  3. Мне не нравится слишком большой отступ перед section

(En) Taking into account the above solutions, I found the answer to this question for myself. He takes into account that:

  1. In Russian, the word "Application" is much larger than the word "Chapter", so the same indentation does not look beautiful 
  2. Chapters are unnumbered 3.
  3. I do not like too much indent before section

Добавьте следующий код в преамбулу (Add the following code to the preamble):

% Добавить слово Глава в оглавление
\let\originaladdchaptertocentry\addchaptertocentry
\renewcommand*{\addchaptertocentry}[2]{%
  \IfArgIsEmpty{#1}{% Глава без номера
    \originaladdchaptertocentry{#1}{#2}%
  }{% Глава с номером
    \originaladdchaptertocentry{}{\chapapp~#1. #2}%
  }%
}

Пример (Sample)

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.