Page LayoutSpacing for Heading Number in ToC

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
kund2463
Posts: 6
Joined: Mon Dec 10, 2012 11:12 pm

Spacing for Heading Number in ToC

Post by kund2463 »

Hello,

I've been using this code in order to put dots between my ToC items and their respective page numbers:

Code: Select all

\makeatletter
\renewcommand\l@section[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
    \addvspace{1.0em \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\ 
      \leaders\hbox{$\m@th\mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}
     \hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
    \endgroup
  \fi}
\makeatother
Now, I need to add a couple of characters before the ToC item number, which I've done using \renewcommand. My problem comes from the fact that I need to increase the number width in the ToC. I'm not exactly sure what the code above does, but I was able to figure out that if I changed \setlength\@tempdima{1.5em}% to \setlength\@tempdima{4.0em}% I get perfect spacing for the section items. Unfortunately, the spacing on the subsections is wrong and I'm not sure how to further modify the above code to get the number width for the subsections to also change.

Any help will be much appreciated!

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

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Spacing for Heading Number in ToC

Post by localghost »

All you are after is usually done by packages like titletoc or tocloft. A quite new package for this purpose is etoc.


Best regards and welcome to the board
Thorsten
kund2463
Posts: 6
Joined: Mon Dec 10, 2012 11:12 pm

Spacing for Heading Number in ToC

Post by kund2463 »

Hi Thorsten,
Thanks for the suggestions and welcome. After playing more with the code I posted intially, I figured out that I could essentially duplicate it for subsections only modifying the first line:

Code: Select all

\renewcommand\l@subsection[2]{%
The problem is that now all of the subsections have the same left justification as the sections. How can I modify this code such that the subsection titles will be a slightly smaller font size and indented slightly?

Thanks,
Matt
kund2463
Posts: 6
Joined: Mon Dec 10, 2012 11:12 pm

Re: Spacing for Heading Number in ToC

Post by kund2463 »

Scratch my last post. I'm going to dump the initial code I posted and use tocloft. Is it possible to put the dots between ToC entries and the numbers using tocloft?

Thanks!
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Spacing for Heading Number in ToC

Post by localghost »

kund2463 wrote:[…] Is it possible to put the dots between ToC entries and the numbers using tocloft?[…]
For the standard classes the default setting is that the entries for all heading levels have the dots except for the highest level. In the »article« class this concerns the section headings. With tocloft you can add the dots like shown below.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{tocloft}
\usepackage{blindtext}

\renewcommand*{\cftsecdotsep}{\cftdotsep}

\begin{document}
  \tableofcontents

  \bigskip
  \blinddocument
\end{document}
For details and other possibilities for customization please refer to the package manual.

The blindtext package is only used to produce the dummy document, thus not part of the solution.
Post Reply