Text FormattingCustomizing the ToC with toclof

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Customizing the ToC with toclof

Post by Cham »

I'm having lots of troubles customizing my very large ToC, using the toclof package. Here's a MWE example that shows what I'm trying to achieve. Currently, the spacings are done by eyes only, and I don't understand well the commands used in that code, even after trial and mistakes:

Code: Select all

\RequirePackage[l2tabu, orthodox]{nag}
\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}

\usepackage{tocloft}
	\renewcommand{\cftdotsep}{2}  %  Points spacement ...
	\makeatletter  % To fix a small offset with page number.
	\renewcommand{\@pnumwidth}{2.3em} % {1.75em}
	%\renewcommand{\@tocrmarg}{2.75em} % Useless command?
	\makeatother
	\setcounter{tocdepth}{3} % Add sub-sub-sections to ToC.
	
	% Chapter spacement:
	\renewcommand{\cftchappresnum}{\chaptername\ }
	\renewcommand{\cftchapaftersnumb}{\newline}
	\setlength{\cftchapnumwidth}{0pt} % Spacement to left of chapter number.
	
	% Sections spacement:
	%\renewcommand{\cftsecpresnum}{\hfill} %
	%\renewcommand{\cftsecaftersnum}{\hspace*{10pt}} %
	%\setlength{\cftsecindent}{18pt} %
	%\setlength{\cftsecnumwidth}{36pt} %
	\addtolength{\cftsecnumwidth}{10pt} %
	
	% Sub-sections spacement:
	%\renewcommand{\cftsubsecpresnum}{\hfill} %
	%\renewcommand{\cftsubsecaftersnum}{\hspace*{10pt}} %
	%\setlength{\cftsubsecnumwidth}{44pt} %
	\setlength{\cftsubsecindent}{54pt} %
	\addtolength{\cftsubsecnumwidth}{12pt} %
	
	% Sub-sub-sections spacement:
	\renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize} % Subsubsection style.
	\setlength{\cftsubsubsecindent}{98pt} %
	%\setlength{\cftsubsubsecnumwidth}{0pt} %
	
\begin{document}

\tableofcontents

\mainmatter
\setcounter{chapter}{16}

\chapter{Chapter title}
\setcounter{section}{22}
\section{Section title}
\setcounter{subsection}{32}
\subsection{Subsection title}
\subsubsection{Title of a subsubsection}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\setcounter{chapter}{9}
\setcounter{section}{8}
\section{Section title}
\subsection{Subsection title}
\subsubsection{Title of a subsubsection}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}

\end{document}
Here's what it gives:
ToC.jpg
ToC.jpg (88.27 KiB) Viewed 4657 times
I would like to get the subsubsections titles to be exactly aligned with their subsection title. Currently, the spacement shown above is done by eye and isn't exact.

Also, the section number should have a constant space with its title. Currently, I'm unable to get this.

And lastly, I would like to have the subsection number to be left aligned with its section title, and have a constant spacing on its right and its title.

How can I achieve that?

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

Customizing the ToC with toclof

Post by Cham »

I think I got it, but I need a confirmation that's a proper way of doing this:

Code: Select all

\RequirePackage[l2tabu, orthodox]{nag}
\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}

\usepackage{tocloft}
	\renewcommand{\cftdotsep}{2}  %  Points spacement ...
	\makeatletter  % To fix a small offset with page number.
	\renewcommand{\@pnumwidth}{2.3em} % {1.75em}
	%\renewcommand{\@tocrmarg}{2.75em} % Useless command?
	\makeatother
	\setcounter{tocdepth}{3} % Add sub-sub-sections to ToC.
	
	% Chapter spacement:
	\renewcommand{\cftchappresnum}{\chaptername\ }
	\renewcommand{\cftchapaftersnumb}{\newline}
	\setlength{\cftchapindent}{0pt} % Spacement to left of chapter number.
	\setlength{\cftchapnumwidth}{0pt} %
	
	% Sections spacement:
	%\renewcommand{\cftsecpresnum}{\hfill} %
	%\renewcommand{\cftsecaftersnum}{\hfill} %
	\setlength{\cftsecindent}{24pt} % Spacement to left of section number.
	\setlength{\cftsecnumwidth}{32pt} %
	%\addtolength{\cftsecnumwidth}{12pt} %
	
	% Sub-sections spacement:
	%\renewcommand{\cftsubsecpresnum}{\hfill} %
	%\renewcommand{\cftsubsecaftersnum}{\hfill} %
	\setlength{\cftsubsecindent}{56pt} % Spacement to left of subsection number.
	\setlength{\cftsubsecnumwidth}{48pt} %
	%\addtolength{\cftsubsecnumwidth}{12pt} %
	
	% Sub-sub-sections spacement:
	\renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize} % Subsubsection style.
	\setlength{\cftsubsubsecindent}{104pt} % Spacement to left of subsubsection number.
	
\begin{document}

\tableofcontents

\mainmatter
\setcounter{chapter}{16}

\chapter{Chapter title}
\setcounter{section}{22}
\section{Section title}
\setcounter{subsection}{32}
\subsection{Subsection title}
\subsubsection{Title of a subsubsection}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\setcounter{chapter}{9}
\setcounter{section}{8}
\section{Section title}
\subsection{Subsection title}
\subsubsection{Title of a subsubsection}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}
\subsection{Subsection title}

\end{document}
Here's what it gives:
ToC2.jpg
ToC2.jpg (85.7 KiB) Viewed 4652 times
Would it be better to move all the numbers at a constant space from its title instead? Currently, I don't knbow how to do that! :oops:
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Customizing the ToC with toclof

Post by rais »

Well, if I would try s.th. like that, I'd use the parent's values to create the indent for the current entry, e.g., \cftsecindent+\cftsecnumwidth for \cftsubsecindent. Should you later decide on, say, a different indent for section entries, you'll just need to change it in one place (and not several places, as in your original).
I modified your sample code for fixed distance number/entry; if that's better or worse I leave up to you. :D

Code: Select all

\RequirePackage[l2tabu, orthodox]{nag}
\documentclass[11pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{xcolor}
\usepackage{tocloft}
\renewcommand{\cftdotsep}{2}  %  Points spacement ...
\makeatletter  % To fix a small offset with page number.
  \renewcommand{\@pnumwidth}{2.3em} % {1.75em}
  %\renewcommand{\@tocrmarg}{2.75em} % Useless command? <-- see \cftsetrmarg in tocloft's doc

%\renewcommand{\numberline}[1]{% tocloft's definition
%  \hb@xt@\@tempdima{\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}
  \renewcommand{\numberline}[1]{%<-- modified to allow for \raggedleft in \cft*presnum
    \ifdim\@tempdima>0pt
      \parbox{\@tempdima}{\@cftbsnum #1\@cftasnum}%
    \else
      \makebox[0pt][l]{\@cftbsnum #1\@cftasnum}% hide the overflow of the number's box from LaTeX
% please note that this may lead to overprinting (title over number), if \cft*aftersnumb doesn't contain
% enough space---or a line break
    \fi
    \@cftasnumb
  }
\makeatother
\newlength\ntsepwidth
\setlength\ntsepwidth{10pt}
\newcommand*\ntsep{\rule{\ntsepwidth}{0pt}}%<-- number/title separator

\setcounter{tocdepth}{3} % Add sub-sub-sections to ToC.

% Chapter spacement:
\renewcommand{\cftchappresnum}{\chaptername\ }
\renewcommand{\cftchapaftersnumb}{\newline}
\setlength{\cftchapindent}{0pt} % Spacement to left of chapter number.
\setlength{\cftchapnumwidth}{0pt}%

% Sections spacement:
\renewcommand{\cftsecpresnum}{\raggedleft}%
\renewcommand{\cftsecaftersnum}{\ntsep} %
\setlength{\cftsecindent}{24pt} % Spacement to left of section number.
%\setlength{\cftsecnumwidth}{32pt} %
\settowidth\cftsecnumwidth{12.34\ntsep}

%\addtolength{\cftsecnumwidth}{12pt} %

% Sub-sections spacement:
\renewcommand{\cftsubsecpresnum}{\raggedleft} %
\renewcommand{\cftsubsecaftersnum}{\ntsep} %
\setlength{\cftsubsecindent}{\dimexpr\cftsecindent+\cftsecnumwidth} % Spacement to left of subsection number.
%\setlength{\cftsubsecnumwidth}{48pt} %
\settowidth\cftsubsecnumwidth{12.34.56\ntsep}
%\addtolength{\cftsubsecnumwidth}{12pt} %

% Sub-sub-sections spacement:
\renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize} % Subsubsection style.
\setlength{\cftsubsubsecindent}{\dimexpr\cftsubsecindent+\cftsubsecnumwidth} % Spacement to left of subsubsection number.

\begin{document}
 
\tableofcontents
 
\mainmatter

\addtocontents{toc}{\makebox[0pt][l]{\raisebox{0pt}[0pt][0pt]{% my `calipers' for the TOC
% omit this block for final output
  \rule{\cftsecindent}{0.5pt}%
  {\color{blue!30}\rule[-200pt]{0.4pt}{200pt}}%
  \rule{\dimexpr\cftsecnumwidth-0.4pt-\ntsepwidth\relax}{0.5pt}%
% \ntsep is set inside the box for the number; the sarting point for this line is shifted
% horizontally already by the thickness of the (prev.) vertical line---the 0.4pt  
  {\color{blue!30}\rule[-200pt]{0.4pt}{200pt}}%
  \rule{\dimexpr\ntsepwidth-0.4pt\relax}{0.5pt}%
  {\color{blue!30}\rule[-200pt]{0.4pt}{200pt}}%
  \rule{\dimexpr\cftsubsecnumwidth-0.4pt-\ntsepwidth\relax}{0.5pt}%
  {\color{red!30}\rule[-200pt]{0.4pt}{200pt}}%
  \rule{\dimexpr\ntsepwidth-0.4pt\relax}{0.5pt}%
  {\color{red!30}\rule[-200pt]{0.4pt}{200pt}}%
  }}}

\section{Section title}
\subsection{Subsection title}
\subsubsection{Title of a subsubsection}
\subsection{A somewhat longer subsection title, just to see what happens---or what does not happen}

\setcounter{chapter}{16}
 
\chapter{Chapter title}
\setcounter{section}{22}
\section{Section title}
\setcounter{subsection}{32}
\subsection{Subsection title}
\subsubsection{Title of a subsubsection}
\subsection{Subsection title}
\end{document}
KR
Rainer
Post Reply