[Tex/LaTex] Omitting only the page number of chapter’s heading in ToC

page-numberingtable of contents

How can I remove the page number of the chapter's header in ToC, without removing the page numbers of the preface pages?

For example, in my Table of Contents, I have,

Acknowledgments               ii
table of contents            iii
...
abstract                    viii

Chapter
1 Chapter one                 1
  1.1 Section one             1
...
2 Chapter two                 2 
2.1 Section one               2 
... and so on...

How to remove the first page number 1 or 2 that related to the chapters' headers without touching the page numbers of the preface pages?

As for the document class, I am using \documentclass[doublelespace,tocchapterhead], together with file .cls of what I think was originally called chicagothesis or something like that.

I have tried to to removed #2 in:

\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par

But the result was, the page numbers of all headers have been removed. Is there any way to control that? I do not want to use tocloft package or etoolbox package.

Best Answer

The following should work for most document classes, although without the actual document class it is difficult to test.

You can selectively turn on the use of page numbers in the ToC using the following macros: \gobbletocpage and \restoretocpage. The former redefines \addtocontents, replacing \thepage with \relax (effectively doing nothing), while the latter restores this back to \thepage.

enter image description here

\documentclass{book}
\newcommand{\gobbletocpage}{%
  \renewcommand{\addcontentsline}[3]{%
    \addtocontents{##1}{\protect\contentsline{##2}{##3}{\relax}}}
}%
\newcommand{\restoretocpage}{%
  \renewcommand{\addcontentsline}[3]{%
    \addtocontents{##1}{\protect\contentsline{##2}{##3}{\thepage}}}
}%
\begin{document}
\tableofcontents
\chapter{A chapter}
\section{A section}
\section{A section}
\gobbletocpage
\section{A section}
\chapter{Another chapter}
\restoretocpage
\section{A section}
\section{A section}
\section{A section}
\end{document}

By means of example, the above ToC is void of page numbers after a call to \gobbletocpage (Section 1.3 and Chapter 2), and this is restored with \restoretocpage.


Since you have access to the .cls, replace your definition of \l@chapter (and some surrounding code) with the following (lines 182-219):

% Change style of printing chapters in ToC to match chapter headings.
\setcounter{secnumdepth}{3}% Always number up to \subsubsection
\setcounter{tocdepth}{2}% Include up to \subsection in ToC
\newcommand{\thechappagenum}{}% Empty page number for chapter
\newif\ifchappagenum\chappagenumtrue

\renewcommand*\l@chapter[2]{%
  \renewcommand{\thechappagenum}{\ifchappagenum#2\fi}% Include chapter page num based on \chappagenum boolean
  \typeout{\thechappagenum}
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip .1cm \@plus \p@ %{the v-distance between the headings in the contents}
    \setlength\@tempdima{1.5em}% %{the distance between the number of the chapter and its title}
    \begingroup
           \parindent\z@ \rightskip \@pnumwidth
            \parfillskip -\@pnumwidth
      \leavevmode
      \advance \leftskip \@tempdima 
      \hskip  -\leftskip 
      \etchapterheadstyle{#1}\nobreak
% : The following 3 lines add dots to the chapter TOC listings
      \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
      \hfil \nobreak\hb@xt@\@pnumwidth{\hss \thechappagenum}\par
      \penalty\@highpenalty
    \endgroup
  \fi}
%---------------------------------------------------  
% The lines below responsible on the mechanism of removing pages numbers of selecting headers in ToC.
% We need to add the command \gobbletocpage in the preamble before the \include{chapter} to delete their page numbers in ToC. And,
% we need to add the command \restoretocpage to restore chapter page numbering in ToC
%---------------------------------------------------
\newcommand{\gobbletocpage}{%
  \addtocontents{toc}{\protect\chappagenumfalse}}%
\newcommand{\restoretocpage}{%
  \addtocontents{toc}{\protect\chappagenumtrue}}%
%---------------------------------------------------

The above defines a new boolean "condition" \ifchappagenum that is set to false (\chappagenumfalse) by \gobbletocpage and true (\chappagenumtrue) by \restoretocpage. All of this is written to the .toc file, since this is processed without any knowledge/before the rest of your document.

Use \gobbletocpage before the chapter(s) that you want no page numbers for in the ToC, and \restoretocpage before you want to restore the chapter page numbers. This condition can be modified and used to reformat chapter ToC entries as needed (not just page numbers). Here's your easythesis.cls with the above modifications used in a MWE:

enter image description here

\documentclass{easythesis}
\begin{document}
\tableofcontents
\chapter{A chapter}
\section{A section}
\section{A section}
\section{A section}
\gobbletocpage
\chapter{Another chapter}
\section{A section}
\section{A section}
\section{A section}
\end{document}