[Tex/LaTex] Different spacing between section number and its caption TOC

numberingsectioningspacingtable of contents

I want to use two different spacing between section number and its caption in the Table of Contents (TOC)

I already have tried the solutions:

TOC Text – numbers alignment

Adjusting spaces for the ToC

But I think they are not suitable.

Well the minimal example is:

\documentclass[a4paper,12pt,twoside]{scrreprt}

\usepackage{hyperref}
\hypersetup{colorlinks,breaklinks,plainpages=false,linktocpage,hypertexnames=true,naturalnames=false}
\hypersetup{linkcolor=red,citecolor=blue,filecolor=blue,urlcolor=blue} %Colorful

\usepackage{tocloft}

%%% Configure de distance
\setlength{\cftsecnumwidth}{4em}


\begin{document}
\tableofcontents

\chapter{Hardware Data} \label{chap.hardware}

\setcounter{section}{19}

\section{Test section one}
\subsection{test section one one}
\subsection{test section one two}

\section{Test section two}
\subsection{test section two one}
\subsection{test section two two}

%%%% Appendix
\cleardoublepage
\phantomsection
\appendix
\chapter*{Appendix}
\addcontentsline{toc}{part}{Appendix}

\chapter{Appendix Test Chapter}

\setcounter{section}{199}
\section{Test Section}

%%%%%%%%%%%%%%%% Annex:
\cleardoublepage
\phantomsection
\setcounter{chapter}{0}
\renewcommand{\theHchapter}{an.\Alph{chapter}}
\renewcommand{\thechapter}{$\,\!$\Alph{chapter}}

\renewcommand{\appendixname}{Annex} 
\addcontentsline{toc}{part}{Annex}
\chapter*{Annex}

\renewcommand{\thesection}{\thechapter$_{x}$.\arabic{section}}

\chapter{Annex Test Chapter}

\setcounter{section}{1199}
\section{Test Section}

\end{document}

Wich results in the figure below, depending on

\setlength{\cftsecnumwidth}{4em}

enter image description here

Well, in the Annex I want more space, but I also want to keep the smaller spacing for the sections in the regular chapters.

Does anyone have a clue and/or can help ?

Thanks a lot !

Will

PS: For the record, I have also asked this question in

http://latex-community.org/forum/viewtopic.php?f=47&t=23471

Best Answer

The process might be somewhat counter-intuitive, but you need to insert the spacing changes mid-document for it to have a partial effect in the ToC. The reason for this is because the entire .toc file is read during a call to \tableofcontents. Therefore, changing anything prior to that (in the preamble, for example), will have an effect for the entire ToC. The solution is to add

\addtocontents{toc}{\setlength{\cftsecnumwidth}{4em}}

just after you initiate the Appendix:

enter image description here

\documentclass[a4paper,12pt,twoside]{scrreprt}% http://ctan.org/pkg/koma-script

\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\hypersetup{colorlinks,breaklinks,plainpages=false,linktocpage,hypertexnames=true,naturalnames=false}
\hypersetup{linkcolor=red,citecolor=blue,filecolor=blue,urlcolor=blue} %Colorful

\usepackage{tocloft}


\begin{document}
\tableofcontents

\chapter{Hardware Data} \label{chap.hardware}

\setcounter{section}{19}

\section{Test section one}
\subsection{test section one one}
\subsection{test section one two}

\section{Test section two}
\subsection{test section two one}
\subsection{test section two two}

%%%% Appendix
\cleardoublepage
\phantomsection
\appendix
\chapter*{Appendix}
\addcontentsline{toc}{part}{Appendix}

%%% Configure de distance
\addtocontents{toc}{\setlength{\cftsecnumwidth}{4em}}

\chapter{Appendix Test Chapter}

\setcounter{section}{199}
\section{Test Section}

%%%%%%%%%%%%%%%% Annex:
\cleardoublepage
\phantomsection
\setcounter{chapter}{0}
\renewcommand{\theHchapter}{an.\Alph{chapter}}
\renewcommand{\thechapter}{$\,\!$\Alph{chapter}}

\renewcommand{\appendixname}{Annex} 
\addcontentsline{toc}{part}{Annex}
\chapter*{Annex}

\renewcommand{\thesection}{\thechapter$_{x}$.\arabic{section}}

\chapter{Annex Test Chapter}

\setcounter{section}{1199}
\section{Test Section}

\end{document}

Since you're adding stuff to the ToC, results will only be visible upon the second compile.