3

I am having trouble tracking down what I need to do in order to replace the page number from the standard Memoir \tableofcontents.

Using the standard \tableofcontents I get: (where 5 & 9 are page numbers)

1 Chapter....................... 5

2 Another Chapter.......... 9

And the goal is:

Chapter....................... 1

Another Chapter.......... 2

\renewcommand{\chapternumberline}[1]{} I found will remove the chapter number from the left of the contents.

And \renewcommand{\cftchapterformatpnum}[1]{} will allow me to modify the part number but I think I may be heading in the wrong direction? Possibly the command that describes the format and simply remove the page number and replace that section with the code that outputs the chapter number but I haven't had any luck identifying that.

  • Opinion based comment: I would not remove the page numbers, because this would be difficult to search the chapter then (unless using hyperlinks) – user31729 Nov 19 '15 at 22:49
  • Yes I am using hyperref and converting to PDF. As to the page number this is being used to generate document packages where only the 'chapter' is relevant. Each chapter has a cover page with printing on the right border for paging through when printed. – Kozbot Nov 19 '15 at 22:53
  • I don't think that the \cft... macros will help you since they are used after the \contentsline is written to the .aux and .toc file. You must prevent that the page number is added to the .aux file right from the start! – user31729 Nov 19 '15 at 23:09
2

It's possible to define the \mempreaddchaptertotochook and \mempostaddchaptertotochook hooks to prevent that \addcontentsline writes the page number to he .aux file already.

However, this does not yet work with hyperref due to the special additions by hyperref.

\documentclass{memoir}



\let\origaddcontentsline\addcontentsline % Store the original definition of `\addcontentsline`

\renewcommand{\mempreaddchaptertotochook}{%
  \renewcommand{\addcontentsline}[3]{%
      \addtocontents{##1}{\protect\contentsline{##2}{##3}{\thechapter}}} % Use the chapter number, not the page number
}

\renewcommand{\mempostaddchaptertotochook}{%
  \let\addcontentsline\origaddcontentsline%  We've written the chapter entry to the toc, now switch back to the original version to get section etc. and other entries correctly!
}

\renewcommand{\chapternumberline}[1]{}

\usepackage{blindtext}

\begin{document}
\makeatletter
\tableofcontents
\chapter{My Chapter}
\blindtext[20]
\section{A section}
\blindtext
\chapter{Another Chapter}
\end{document}
  • This worked perfectly, I never would have found that \mempreaddchaptertotochook (!) was what I needed. I will definitely take a deeper look into those commands and what you actually did there. Thanks! – Kozbot Nov 19 '15 at 23:28
  • @Kozbot: Yes, do that please. You have seen my comment about hyperref? – user31729 Nov 19 '15 at 23:29
  • Yes, although I am generating this through PyLatex so it was not a deal-breaker for me since I could automate that elsewhere. Also, I see egreg posted a hyperref compatible version but I will leave this as my accepted since it satisfies the original question. – Kozbot Nov 19 '15 at 23:34
1

I'm not sure what the purpose of this is, but here you find an implementation, also compatible with hyperref.

\documentclass{memoir}
\usepackage{blindtext} % just for the example

\usepackage{hyperref}

\makeatletter
\let\original@l@chapter\l@chapter
\renewcommand{\l@chapter}[2]{%
  \gdef\saved@chapter@number{}%
  \sbox\z@{\def\chapternumberline##1{\gdef\saved@chapter@number{##1}}#1}%
  \original@l@chapter{#1}{\saved@chapter@number}%
}
\let\chapternumberline\@gobble
\makeatother

\settocdepth{chapter} % only chapters in TOC

\begin{document}

\tableofcontents
\chapter{My Chapter}
\blindtext[20]
\section{A section}
\blindtext
\chapter{Another Chapter}
\end{document}

enter image description here

I typeset the first argument of \l@chapter in a box, redefining \chapternumberline so that it saves its own argument. Such a redefinition is local, but the argument is saved locally. Then I pass the saved number as the second argument to the original \l@chapter, while \chapternumberline is just \@gobble when doing the real typesetting.

  • Thank you for your alternate hyperref compatible solution as well! – Kozbot Nov 19 '15 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.