Page Layouttocloft | Chapter Leaders in ToC

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
kilppari
Posts: 4
Joined: Sat Jan 07, 2012 1:34 pm

tocloft | Chapter Leaders in ToC

Post by kilppari »

Hi,

I'm having problems with customizing the table of contents of my thesis according to my university's requirements.

The main problem is with the dotting in the chapter and section leaders when using the report class along with the tocloft package. I need to have the dotting in the normal chapter leaders but not in the early 'chapters' like 'abstract' 'preface' and such.

Also I can't get the dotting and page numbers to align correctly. I've tried to change the box size for the page numbers but it doesn't work as I would like.

Here's a picture of how it should look: ( Don't mind the text, it's from the thesis guide written in Finnish )
ToC01.png
ToC01.png (31.99 KiB) Viewed 7594 times
And here's how my attempt looks: ( all the formatting not related to the problems removed )
ToC02.png
ToC02.png (15.14 KiB) Viewed 7594 times
I'm using '\chapter*' for the early chapters that should not have dotting in their leaders. The dotting for normal chapter leaders is otherwise good but the page numbering gets aligned to left when there are at least 10 pages.

I have tried to adjust the 'pnumwidth' to get the alignment correctly, but when I give the numbers more space, the dots won't reach all the way for the chapters and sections for early pages, as seen below:
ToC03.png
ToC03.png (7.4 KiB) Viewed 7594 times
Here is a working code for the problem. ( I tried to remove all the unnecessary formatting not related to my problems )

Code: Select all

\documentclass[a4paper, 12pt]{report}
\usepackage{times}
\usepackage{tocloft}

\makeatletter
  \renewcommand{\contentsname}{TABLE OF CONTENTS}
  \renewcommand{\cftchapfont}{\normalfont}
  \renewcommand{\cftchappagefont}{\normalfont}
  \renewcommand{\cftdotsep}{1}
  \renewcommand{\cftchapleader}{\cftdotfill{\cftsecdotsep}}

  % Try to remove margin between leader dots and page numbers:
  \cftsetpnumwidth{12pt}

  \renewcommand{\chapter}{ \newpage
    \@startsection {chapter}{0}{0mm}{0mm}{\baselineskip}
                   {\normalfont\large\bfseries\centering} }
\makeatother

\begin{document}
\addtocontents{toc}{\protect\contentsline{chapter}{ABSTRACT}{} }
\chapter*{ABSTRACT}

\newpage
\addtocontents{toc}{\protect\contentsline{chapter}{TABLE OF CONTENTS}{} }
\tableofcontents

\addtocontents{toc}{\protect\contentsline{chapter}{FOREWORD}{} }
\chapter*{FOREWORD}

\addtocontents{toc}{\protect\contentsline{chapter}{ABBREVIATIONS}{} }
\chapter*{ABBREVIATIONS}

\chapter{FIRST CHAPTER}
  \section{First section}
    \subsection{Fist subsection}
  \section{Second section}

% Dummy pages to get two digit page numbers to TOC
\newpage 1 \newpage 2 \newpage 3 \newpage 4 \newpage 5

\chapter{SECOND CHAPTER}
\chapter{THIRD CHAPTER}
\end{document}
I would really appreciate if someone could help me. I've been fighting with this one for a couple of days now and can't seem to find a solution. I'm getting quite desperate here, to the point of thinking about changing to using Word..

Hopefully this haven't been asked multiple times, I couldn't find posts with the same problem. I'm fairly new to LaTeX, so please tell if there's some obvious faults or if it's just generally bad code.

Br,
Pekka

// Edit: Solved, see 2nd and 4th reply.
Last edited by kilppari on Fri Jan 13, 2012 10:00 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
TikZ book
kilppari
Posts: 4
Joined: Sat Jan 07, 2012 1:34 pm

Re: tocloft | Chapter Leaders in ToC

Post by kilppari »

I'm still trying to fight this problem, is this really something that cannot be done with LaTeX?

Any hints on other good packages for customizing table of contents?

-Pekka
User avatar
Stefan Kottwitz
Site Admin
Posts: 10312
Joined: Mon Mar 10, 2008 9:44 pm

tocloft | Chapter Leaders in ToC

Post by Stefan Kottwitz »

Hi Pekka,

welcome to the board!
You provided a compilable minimal working example, that's very good! So I could easily test it and found a solution.

Remove this line from the preamble:

Code: Select all

\renewcommand{\cftchapleader}{\cftdotfill{\cftsecdotsep}}
Make the redefinition later. This way the redefinition can be inserted into the .toc file, so it's read in together with the ToC entries:

Code: Select all

\addtocontents{toc}{\protect\renewcommand{\protect\cftchapleader}
    {\protect\cftdotfill{\cftsecdotsep}}}
I used \protect, because I would like to have those macros not to be expanded at the time when they are written to the .toc file. Place this line right before your first real chapter.

Stefan
LaTeX.org admin
kilppari
Posts: 4
Joined: Sat Jan 07, 2012 1:34 pm

tocloft | Chapter Leaders in ToC

Post by kilppari »

Thanks for the reply and the clarifications Stefan, that works great! :)

And thanks to you I also got a little bit forward with getting the dotted leaders to reach all the way to the page numbers.

I found (from the manual) that the leader and the page numbers in TOC are typeset together with a following command: (this one for chapters)

Code: Select all

\newcommand{\cftchapfillnum}[1]{%
{\cftchapleader}\nobreak
\hb@xt@\@pnumwidth{\hfil\cftchappagefont #1}\cftchapafterpnum\par}
With a little bit of tweaking I can get those leader dots also inside the box where the page numbers are set:

Code: Select all

  \renewcommand{\cftchapfillnum}[1]{
    {\cftsecleader}\nobreak
    \hb@xt@\@pnumwidth{\cftsecleader\cftchappagefont #1}\cftchapafterpnum\par} 
Again, this works great in the preamble, but it sets the leaders also for the early chapters. Now if I try to add this to the .toc file just before the first real chapter (like the similar example you gave), I get following error:
'You can't use `macro parameter character #' in restricted horizontal mode'

I guess defining the parameter does not work like that in this case..

Here's the same code when the new redefinition is issued in the preamble. ( and the broken part is commented out just before the first real chapter)

Code: Select all

\documentclass[a4paper, 12pt]{report}
\usepackage{times}
\usepackage{tocloft}

% ------------------------------------------------------------------------------
\makeatletter
  \renewcommand{\contentsname}{TABLE OF CONTENTS}
  \renewcommand{\cftchapfont}{\normalfont}
  \renewcommand{\cftchappagefont}{\normalfont}
  \renewcommand{\cftdotsep}{0.454}


  \renewcommand{\cftsecfillnum}[1]{
    {\cftsecleader}\nobreak
    \hb@xt@\@pnumwidth{\cftsecleader\cftsecpagefont #1}\cftsecafterpnum\par}

  \renewcommand{\cftsubsecfillnum}[1]{
    {\cftsecleader}\nobreak
    \hb@xt@\@pnumwidth{\cftsecleader\cftsubsecpagefont #1}\cftsubsecafterpnum\par}

  % How can I get this defined after the early chapters? (abstract and such)
  \renewcommand{\cftchapfillnum}[1]{
    {\cftsecleader}\nobreak
    \hb@xt@\@pnumwidth{\cftsecleader\cftchappagefont #1}\cftchapafterpnum\par}

  \cftsetpnumwidth{18pt}

  \renewcommand{\chapter}{ \newpage
    \@startsection {chapter}{0}{0mm}{0mm}{\baselineskip}
                   {\normalfont\large\bfseries\centering} }
\makeatother
% ------------------------------------------------------------------------------

\begin{document}

\addtocontents{toc}{\protect\contentsline{chapter}{ABSTRACT}{} }
\chapter*{ABSTRACT}

\newpage
\addtocontents{toc}{\protect\contentsline{chapter}{TABLE OF CONTENTS}{} }
\tableofcontents

\addtocontents{toc}{\protect\contentsline{chapter}{FOREWORD}{} }
\chapter*{FOREWORD}

\addtocontents{toc}{\protect\contentsline{chapter}{ABBREVIATIONS}{} }
\chapter*{ABBREVIATIONS}

% I can't get this to work :/
%\addtocontents{toc}{
%  \protect\renewcommand{\protect\cftchapfillnum}[1]{
%    {\protect\cftsecleader}\protect\nobreak
%    \protect\hbox to\protect\@pnumwidth{
%      \protect\hfil\protect\cftsecleader\protect\cftchappagefont #1}
%    \protect\cftchapafterpnum\par
%  }
%}

% This works great, only the dotting does not reach all the way to the page numbers.
%\addtocontents{toc}{\protect\renewcommand{\protect\cftchapleader}{\protect\cftdotfill{\cftsecdotsep}}}

\chapter{FIRST CHAPTER}
\section{First section}
\subsection{First subsection}
\section{Second section}

% Dummy pages to get two digit page numbers to TOC
\newpage 1 \newpage 2 \newpage 3 \newpage 4 \newpage 5

\chapter{SECOND CHAPTER}
\chapter{THIRD CHAPTER}

\end{document}
Any suggestions?

-Pekka
kilppari
Posts: 4
Joined: Sat Jan 07, 2012 1:34 pm

tocloft | Chapter Leaders in ToC

Post by kilppari »

Ah, got it now.. oh man.

I was using the 'cftsecleader' macro in the preamble where the cftchapfillnum is redefined. By changing this to cftchapleader, everything works as it should..

Like this in the preamble:

Code: Select all

\renewcommand{\cftchapfillnum}[1]{
    {\cftchapleader}\nobreak %by default cftchapleader does nothing
    \hb@xt@\@pnumwidth{\cftchapleader\cftchappagefont #1}\cftchapafterpnum\par}
An this just before the first real chapter. ( props to Stefan )

Code: Select all

\addtocontents{toc}{\protect\renewcommand{
  \protect\cftchapleader}{\protect\cftdotfill{\cftsecdotsep}}}
-Pekka
Post Reply