[Tex/LaTex] KOMA-script: Add word “Part” and “Chapter” in ToC

koma-scripttable of contents

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} 

Best Answer

\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