[Tex/LaTex] Supress page numbers for \part in toc

partstable of contentstocloft

I have created two custom macros for chapter and part command. When I am using following code:

\documentclass[final]{book}

\usepackage{lipsum}
\usepackage{tocloft}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\cftchapfont}{\mdseries}
\renewcommand{\cftchappagefont}{\mdseries}

\long\def \mychapter#1{
\chapter*{#1}
\addcontentsline{toc}{chapter}{#1}
}

\long\def \mypart#1#2{
\part*{#1 \\* #2}
\addcontentsline{toc}{part}{#1\\ #2}
}

\begin{document}

\tableofcontents

\mypart{PART NUMBER 1}{Part Title}
\mychapter{Some chapter 1}
\lipsum[1-10]
\mychapter{Some chapter 2}
\lipsum[1-10]
\mychapter{Some chapter 3}
\lipsum[1-10]
\mypart{PART NUMBER 2}{Part Title}
\mychapter{Some chapter 4}
\lipsum[1-10]
\mychapter{Some chapter 5}
\lipsum[1-10]
\mychapter{Some chapter 6}
\lipsum[1-10]
\mychapter{Some chapter 7}
\lipsum[1-10]
\mypart{PART NUMBER 3}{Part Title}
\mychapter{Some chapter 8}
\lipsum[1-10]
\mychapter{Some chapter 9}
\lipsum[1-10]
\mychapter{Some chapter 10}
\lipsum[1-10]
\mychapter{Some chapter 11}
\lipsum[1-10]

\end{document}

it generates following Table of Contents:

Table of Contents with improper page numbers

Page numbers for \part in this Table of Contents are not correct. Instead of pointing to real page where \part text is printed, they point to page where first chapter within that part starts.

I have used pdflatex two times to make sure page numbers will be correct, but they are not.

Questions:

  1. Why are page numbers for \part not correct in ToC?
  2. How can I supress page numbers for \part altogether in ToC, but leave Table of Contents entry for \part unchanged otherwise?

Best Answer

You're taking the wrong approach. If you don't want any element to be numbered, just tell LaTeX so.

\documentclass[final]{book}

\usepackage{lipsum}
\usepackage{tocloft}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\cftchapfont}{\mdseries}
\renewcommand{\cftchappagefont}{\mdseries}

\setcounter{secnumdepth}{-2}

\newcommand\mypart[2]{\part{#1 \\ #2}}

\begin{document}

\tableofcontents

\mypart{PART NUMBER 1}{Part Title}
\chapter{Some chapter 1}
\lipsum[1-10]
\chapter{Some chapter 2}
\lipsum[1-10]
\chapter{Some chapter 3}
\lipsum[1-10]
\mypart{PART NUMBER 2}{Part Title}
\chapter{Some chapter 4}
\lipsum[1-10]
\chapter{Some chapter 5}
\lipsum[1-10]
\chapter{Some chapter 6}
\lipsum[1-10]
\chapter{Some chapter 7}
\lipsum[1-10]
\mypart{PART NUMBER 3}{Part Title}
\chapter{Some chapter 8}
\lipsum[1-10]
\chapter{Some chapter 9}
\lipsum[1-10]
\chapter{Some chapter 10}
\lipsum[1-10]
\chapter{Some chapter 11}
\lipsum[1-10]

\end{document}

LaTeX will take care of the rest.

enter image description here

If you want to suppress the page number for the parts, then do like this:

\documentclass[final]{book}

\usepackage{lipsum}
\usepackage{tocloft}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
\renewcommand{\cftchapfont}{\mdseries}
\renewcommand{\cftchappagefont}{\mdseries}

% suppress page number in toc for parts
\cftpagenumbersoff{part}

\setcounter{secnumdepth}{-2}

\newcommand\mypart[2]{\part{#1 \\ #2}}

\begin{document}

\tableofcontents

\mypart{PART NUMBER 1}{Part Title}
\chapter{Some chapter 1}
\lipsum[1-10]
\chapter{Some chapter 2}
\lipsum[1-10]
\chapter{Some chapter 3}
\lipsum[1-10]
\mypart{PART NUMBER 2}{Part Title}
\chapter{Some chapter 4}
\lipsum[1-10]
\chapter{Some chapter 5}
\lipsum[1-10]
\chapter{Some chapter 6}
\lipsum[1-10]
\chapter{Some chapter 7}
\lipsum[1-10]
\mypart{PART NUMBER 3}{Part Title}
\chapter{Some chapter 8}
\lipsum[1-10]
\chapter{Some chapter 9}
\lipsum[1-10]
\chapter{Some chapter 10}
\lipsum[1-10]
\chapter{Some chapter 11}
\lipsum[1-10]

\end{document}

enter image description here

Thanks to Mico for suggesting \cftpagenumbersoff.