Instead of the spacing environment, you can use also the tocloft
package for a more granular control. See the MWE:
\documentclass{book}
\usepackage[titles]{tocloft}
\setlength{\cftbeforechapskip}{.1ex}
\setlength{\cftbeforesecskip}{-.5ex}
\usepackage{lipsum} % placeholder text
\begin{document}
\tableofcontents
\chapter{The chapter one}
\chapter{The chapter two}
\section{Lore ipsum}
\lipsum[1]
\section{Nam dui ligula}
\lipsum[2]
\section{Nulla malesuada}
\lipsum[3]
\chapter{The chapter three}
\section{Lore ipsum}
\lipsum[1]
\chapter{The chapter four}
\section{Lore ipsum}
\lipsum[1]
\end{document}
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:
\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.
Best Answer
try setting in the preamble
\usepackage{setspace}
and after\begin{document}
write\addtocontents{toc}{\protect\setstretch{0.9}}
(needs Ctrl-L for the TeX mode.You can decrease the number to any value you need.
Another possibility is to set the additional space to zero: