Page LayoutProblems with spacings in ToC

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Problems with spacings in ToC

Post by Cham »

I'm using the tocloft package to customize my ToC, and I'm having troubles in setting the spaces between the chapter/section/subsection number and the title after it.

Here's a complete MWE showing the issue :

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[nottoc,notlof]{tocbibind}
\usepackage{tocloft}
	\setcounter{tocdepth}{3}  % To add the subsubsections to the ToC.
	\renewcommand{\cftchappresnum}{\chaptername\ }
	\renewcommand{\cftchapaftersnum}{~-~}  % To add "-" after the chapter's No.
	\setlength{\cftchapnumwidth}{120pt}  % Width of chapter No.
	\cftpagenumbersoff{chapter}  % Removes the chapter's page No. from ToC.
	\setlength{\cftsecindent}{18pt}  % Indent of section No.
	\setlength{\cftsecnumwidth}{36pt}  % Width of section No.
	\setlength{\cftsubsecindent}{54pt}  % Indent of subsection No.
	\setlength{\cftsubsecnumwidth}{46pt}  % Width of subsection No.
	\setlength{\cftsubsubsecindent}{118pt}  % Indent of subsubsection No.
	\setlength{\cftsubsubsecnumwidth}{0pt}  % Width of subsubsection No.
	\renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize}  % Style of subsubsection.

\usepackage{lipsum}

\begin{document}
\frontmatter
\addcontentsline{toc}{section}{\textsl{A Few Words}}
\addcontentsline{toc}{section}{\textsl{Notations and Conventions}}
\addcontentsline{toc}{section}{\textsl{The Table of Contents}}
\tableofcontents
\mainmatter

\chapter{Title of some chapter}
\section{The title of some section}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
\section{The title of some section}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]

\setcounter{chapter}{12}
\chapter{Title of some chapter}
\setcounter{section}{10}
\section{The title of some section}
	\lipsum[1-10]
\setcounter{subsection}{14}
\subsection{Title of a subsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
\section{The title of some section}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]
	\subsubsection{Title of some subsubsection}
	\lipsum[1-10]
\subsection{Title of a subsection}
	\lipsum[1-10]

\end{document}
And here's a preview, with the red marks showing the spacing issues :
ToC.jpg
ToC.jpg (146.37 KiB) Viewed 6892 times
So my questions are these :

1. How can I define a constant spacing between the chapter's number and its title after it, with the "-" in between ? I don't think that doing it "by hand" is the correct way.

2. How can I define a nice spacing for all the section/subsection numbers and the title to its right, while maintaining some nice vertical alignment ? Currently, it is done by hand/eye, and it's not looking very nice.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Problems with spacings in ToC

Post by Cham »

Any suggestion about the problems above ?

Is there any other package to customize the ToC ?
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Problems with spacings in ToC

Post by Cham »

I may have found a way to solve my spacing problems above, except for the three first entries (A Few Words, Notation, and The Table of Contents. See picture below). Here's the working code :

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage[nottoc,notlof]{tocbibind}
\usepackage{tocloft}
        \makeatletter
	\renewcommand{\@pnumwidth}{2.3em}
	\makeatother
	\setcounter{tocdepth}{3}
	\renewcommand{\cftchappresnum}{\chaptername\ }
	\renewcommand{\cftchapaftersnumb}{\newline}
	
	\renewcommand{\cftsecpresnum}{\hfill}  %
	\renewcommand{\cftsecaftersnum}{\hspace*{8pt}}
	\addtolength{\cftsecnumwidth}{8pt}
	
	\renewcommand{\cftsubsecpresnum}{\hfill}  %
	\renewcommand{\cftsubsecaftersnum}{\hspace*{8pt}}
	\addtolength{\cftsubsecnumwidth}{8pt}
	
	\setlength{\cftchapnumwidth}{0pt}
	\setlength{\cftsecindent}{12pt}
	%\setlength{\cftsecnumwidth}{36pt}
	\setlength{\cftsubsecindent}{54pt}
	%\setlength{\cftsubsecnumwidth}{44pt}
	\setlength{\cftsubsubsecindent}{114pt}
	%\setlength{\cftsubsubsecnumwidth}{0pt}
	\renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize}
	\renewcommand{\cftdotsep}{2}

\usepackage{lipsum}
\usepackage[colorlinks,linkcolor=blue,urlcolor=red]{hyperref}

\begin{document}
\frontmatter
\addcontentsline{toc}{section}{\textsl{A Few Words}}
\addcontentsline{toc}{section}{\textsl{Notations and Conventions}}
\addcontentsline{toc}{section}{\textsl{The Table of Contents}}
\tableofcontents
\mainmatter

\chapter{Title of some chapter}
\section{The title of some section}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
\section{The title of some section}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]

\setcounter{page}{980}
\setcounter{chapter}{12}
\chapter{Title of some chapter}
\setcounter{section}{10}
\section{The title of some section}
        \lipsum[1-10]
\setcounter{subsection}{14}
\subsection{Title of a subsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
\section{The title of some section}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
        \subsubsection{Title of some subsubsection}
        \lipsum[1-10]
\subsection{Title of a subsection}
        \lipsum[1-10]
\end{document}
And here's a preview of what the code do :
ToC2.jpg
ToC2.jpg (173.36 KiB) Viewed 6859 times
Please, what should I do to properly adjust the placement of the three first entries, so they are well balanced with the rest ? How can I push them to the right, without adding extra spaces in their title ?

Also, I don't understand why the subsections 13.11.15, 13.11.16 and 13.11.17 are so tight with their title (see the picture), since the numbers are supposed to be aligned to the right instead of the left. According to the code, there should be a space of 8 points between the number and the title. What am I doing wrong here ?
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Problems with spacings in ToC

Post by Cham »

No suggestions about the last query ?

Currently, my code is all messy with many hacks, since I don't know the proper way of doing these things. The documentation is so kryptic ! :?

What would you do to solve the issue described above ?
Post Reply