# counterwithin problems with table of content

I would like to use the option counterwithin to get my numbering as I, I.1., I.2, .., II, II.1, II.2,...

I use the following minimal script

\documentclass[11pt, a4paper, twoside, openany]{scrbook}
\usepackage{chngcntr}
\counterwithin{chapter}{part}
\begin{document}
\tableofcontents
\part{This is part 1}
\chapter{This is I.1}
\part{This is part 2}
\chapter{This is II.1}
\end{document}


This does what I want, but in the table of content, for some reason, there is not enough space between the numbers and the chapter names. As the roman numbers get longer, the numbers and text even overlap.

-

You can use tocloft and change the numwidth:

\setlength{\cftchapnumwidth}{2em}


Change 2em to whatever you desire.

\documentclass[11pt, a4paper, twoside, openany]{scrbook}
\usepackage{chngcntr}
\usepackage{tocloft}
\counterwithin{chapter}{part}
\setlength{\cftchapnumwidth}{2em}
\begin{document}
\tableofcontents
\part{This is part 1}
\chapter{This is I.1}
\part{This is part 2}
\chapter{This is II.1}
\end{document}


You can do the same for part, sec subsec, subsubsec para and subpara in a similar fashion. Do

\setlength{\cftXnumwidth}{<dimension>}


Where X has to be replaced by part sec etc.

-
But I would use \counterwithin*{chapter}{part} so that chapter numbers are not prepended by part number. –  Harish Kumar May 30 at 11:15

You can use the package tocstyle which is part of the KOMA-Script-bundle.

\documentclass[openany]{scrbook}
\usepackage{tocstyle}
\usetocstyle{KOMAlike}
\usepackage{chngcntr}
\usepackage{blindtext}
\counterwithin{chapter}{part}
\begin{document}
\tableofcontents
\part{This is part 1}
\blinddocument\blinddocument
\part{This is part 2}
\blinddocument\blinddocument
\end{document}


You have to run the code several times to get:

Or you can use the option toc=flat:

\documentclass[openany,toc=flat]{scrbook}
\usepackage{chngcntr}
\usepackage{blindtext}
\counterwithin{chapter}{part}
\begin{document}
\tableofcontents
\part{This is part 1}
\blinddocument\blinddocument
\part{This is part 2}
\blinddocument\blinddocument
\end{document}


Run twice to get

-