TransWikia.com

Troubles customizing the ToC with tocloft

TeX - LaTeX Asked on August 17, 2020

I’m having difficulties in setting up the ToC of a large book with many chapters, sections and subsections. The horizontal spacings aren’t right, and the dots to the page number isn’t right neither. Here’s a MWE showing the troubles:

documentclass[11pt,twoside]{book}
usepackage[T1]{fontenc}
usepackage{lmodern}
usepackage{microtype}
usepackage[total={6.25in,10in},left=1.25in,top=0.5in,includehead,includefoot]{geometry}
usepackage{lipsum}

usepackage[titles]{tocloft}
    renewcommand{cftdotsep}{2}  %  Points spacement.
    makeatletter  % To fix a small offset with page number.  TO BE FIXED!
    renewcommand{@pnumwidth}{2.3em} % {1.75em}  TO BE FIXED!

    % 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}{~} % Appears to be too short.  TO BE FIXED!
    setlength{cftsecindent}{11pt} % Random number?  TO BE FIXED!
    setlength{cftsecnumwidth}{32pt} % Random number?  TO BE FIXED!
    addtolength{cftsecnumwidth}{8pt} % ??

    % Sub-sections spacement:
    renewcommand{cftsubsecpresnum}{hfill} % ??
    renewcommand{cftsubsecaftersnum}{~} % Appears to be too short.  TO BE FIXED!
    setlength{cftsubsecindent}{56pt} % Random number?  TO BE FIXED!
    setlength{cftsubsecnumwidth}{48pt} % Random number?  TO BE FIXED!
    addtolength{cftsubsecnumwidth}{12pt} %  ??

begin{document}

frontmatter 

tableofcontents

mainmatter

chapter{Chapter Title}
setcounter{page}{12}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]

chapter{Chapter Title}
setcounter{page}{850}
lipsum[2-4]
setcounter{section}{12}
section{Section Title}
lipsum[2-4]
setcounter{subsection}{15}
subsection{Subsection}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]

setcounter{chapter}{12}
chapter{Chapter Title}
setcounter{page}{1311}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
setcounter{subsection}{14}
subsection{Subsection}
lipsum[2-4]
setcounter{section}{15}
section{Section Title}
lipsum[2-4]
setcounter{subsection}{12}
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]

backmatter
pagestyle{plain}

end{document}

Preview, with troubles shown in red:

enter image description here

  1. As the picture shows, there are some extra dots on the right side. How to fix this? I need a constant spacing between the last dot and its page number, or all the dots should be aligned (whatever the page number).

  2. The spacing looks a bit too tight between the section/subsection title and its number. What should I use instead of {~} ?

  3. The spacings to the left margin appear to be random and I don’t know what numbers I should use there, for a book that uses 11 points fonts. Currently, I used funny looking numbers by eyes only (11pt, 32pt, 8pt, 56pt, 48pt, 12pt ??). I need help there.


EDIT: About the dots problem, the microtype package appears to be the cause of the extra dots. A simple solution is given there: How can I make the dots in a ToC end at the same place?

One Answer

Here is a revised version of your MWE.

% tocprob.tex  SE 557386

documentclass[11pt,twoside]{book}
usepackage[T1]{fontenc}
usepackage{lmodern}
usepackage{microtype}
usepackage[total={6.25in,10in},left=1.25in,top=0.5in,includehead,includefoot]{geometry}
usepackage{lipsum}

usepackage[titles]{tocloft}
    renewcommand{cftdotsep}{2.10}  %  Points spacement. % PW changed
    makeatletter  % To fix a small offset with page number.  TO BE FIXED!
    renewcommand{@pnumwidth}{2.3em} % {1.75em}  TO BE FIXED!

    % 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}{~} % Appears to be too short.  TO BE FIXED!
%    setlength{cftsecindent}{11pt} % Random number?  TO BE FIXED!
%    setlength{cftsecnumwidth}{32pt} % Random number?  TO BE FIXED!
    addtolength{cftsecnumwidth}{8pt} % ??

    % Sub-sections spacement:
%    renewcommand{cftsubsecpresnum}{hfill} % ??
%    renewcommand{cftsubsecaftersnum}{~} % Appears to be too short.  TO BE FIXED!
%    setlength{cftsubsecindent}{56pt} % Random number?  TO BE FIXED!
%    setlength{cftsubsecnumwidth}{48pt} % Random number?  TO BE FIXED!
    addtolength{cftsubsecnumwidth}{12pt} %  ??

begin{document}

frontmatter 

tableofcontents

mainmatter

chapter{Chapter Title}
setcounter{page}{12}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]

chapter{Chapter Title}
setcounter{page}{850}
lipsum[2-4]
setcounter{section}{12}
section{Section Title}
lipsum[2-4]
setcounter{subsection}{15}
subsection{Subsection}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]

setcounter{chapter}{12}
chapter{Chapter Title}
setcounter{page}{1311}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
section{Section Title}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
setcounter{subsection}{14}
subsection{Subsection}
lipsum[2-4]
setcounter{section}{15}
section{Section Title}
lipsum[2-4]
setcounter{subsection}{12}
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]
subsection{Subsection}
lipsum[2-4]

backmatter
pagestyle{plain}

end{document}

Basically what I have done is to comment out many of your tocloft changes to revert back to the general ToC layout. In some places you needed extra space between numbers and titles and I just added the extra space to the default spacing. I don't know what caused the problem with the dot spacing but changing your cftdotsep from 2 to 2.10 seems to have fixed that.

Answered by Peter Wilson on August 17, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP