Page LayoutDifferent spacing between section number and its caption TOC

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
will_flp
Posts: 6
Joined: Mon Dec 17, 2012 7:06 pm

Different spacing between section number and its caption TOC

Post by will_flp »

Hi,

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:
http://tex.stackexchange.com/questions/ ... -alignment
http://tex.stackexchange.com/questions/ ... or-the-toc

But I think they are not suitable.

Well the minimal example is:

Code: Select all

\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}

Image


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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
will_flp
Posts: 6
Joined: Mon Dec 17, 2012 7:06 pm

Different spacing between section number and its caption TOC

Post by will_flp »

For the record, here is the link to the crosspost in Tex Stackexchange.

Moreover, below you find the answer suggested by Werner.
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 of 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 on the entire ToC. The solution is to add

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

just after you initiate the Appendix. Since you're adding stuff to the ToC, results will only be visible upon the second compile (see the example below).
Image


Code: Select all

\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}
Last edited by will_flp on Thu Jul 08, 2021 2:52 pm, edited 2 times in total.
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Different spacing between section number and its caption TOC

Post by Ijon Tichy »

You should not and need not use tocloft with a KOMA-Script class.

Code: Select all

\documentclass[a4paper,12pt,twoside]{scrreprt}% BTW: a4paper is the default.

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

% Additional appendix stuff.
\newcommand*{\appendixmore}{%
  \addtocontents{toc}{\protect\DeclareTOCStyleEntry[numwidth=4em]{section}{section}}%
}

\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
\appendix
\addchap{Appendix}% Wouldn'nt \addpart be better?

\chapter{Appendix Test Chapter}

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

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

\renewcommand{\appendixname}{Annex} 
\addchap{Annex}

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

\chapter{Annex Test Chapter}

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

\end{document}
See the KOMA-Script manual for more information about \DeclareTOCStyleEntry.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply