[Tex/LaTex] Table of contents: Include part number for chapters entries

chaptersnumberingpartstable of contents

I have the following code

\documentclass{scrreprt}
%\usepackage[titles]{tocloft}
%\renewcommand{\cftchappresnum}{\thepart.}

\begin{document}
    \tableofcontents
    \part{First Part}
    \chapter{First Chapter}
\end{document}

that results in the following table of contents:
table of contents

How can I include the number of the part into the chapter entry, i.e. get I.1. First Chapter?

As you can see, I tried to use the tocloft package but without success.

Best Answer

You have to adjust a number of length, in addition to the counter representation. Here is a minimal example with some adjustments up to the section level:

enter image description here

\documentclass{scrreprt}
\renewcommand{\thechapter}{\thepart.\arabic{chapter}}% Prefix chapter with part number
\usepackage{etoolbox}
\makeatletter
\patchcmd{\l@chapter}{1.5em}{2em}{}{}% Increase number width for chapter
\renewcommand{\l@section}{\bprot@dottedtocline{1}{2em}{2.7em}}% Increase number width and
                                                              % indent for section
\makeatother
\begin{document}
    \tableofcontents
    \part{First Part}
    \chapter{First Chapter}
    \section{A section}
\end{document}

Prepending the part number to the chapter number is achieved using

\renewcommand{\thechapter}{\thepart.\arabic{chapter}}

This would be a global replacement, making \thepart. appear before a chapter number in the text, the ToC and references to chapters (as well as lower sectional units, since they inherit the hierarchy).

Secondly, we patch \l@chapter (using etoolbox) to allow for a wider spacing around the number. Since the ToC forms a hierarchy of the document structure, a change in one indentation level should typically be followed by an adjustment at lower levels as well. So, since the increase was made from 1.5em to 2em, we need to adjust the <indent> of \l@section (and possibly \l@subsection...) as well:

\renewcommand{\l@section}{\bprot@dottedtocline{1}{2em}{2.7em}}% Increase number width and
                                                              % indent for section

Argument structure of \bprot@dottedtocline{<level>}{<indent>}{<numwidth>} indents a sectional units at level <level> by a length <indent> and puts the number in a box of width <numwidth>. Under scrreprt, the following definitions hold for lower sectional units (if you wish to adjust them as well):

\newcommand*\l@subsection{\bprot@dottedtocline{2}{3.8em}{3.2em}}
\newcommand*\l@subsubsection{\bprot@dottedtocline{3}{7.0em}{4.1em}}
\newcommand*\l@paragraph{\bprot@dottedtocline{4}{10em}{5em}}
\newcommand*\l@subparagraph{\bprot@dottedtocline{5}{12em}{6em}}