Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am writing a class based on memoir, which provides \tableofcontents (which lists the ToC within the ToC) and \tableofcontents* (which does not list the ToC within the ToC).

I have seen this question and its many duplicates, but I want to prevent the Table of Contents from itself being listed in the Table of Contents regardless of the user's choice of \tableofcontents* vs. \tableofcontents.

Based on the memoir manual (Section 9.2), I am currently using etoolbox to do

\pretocmd{\tableofcontents}{\begin{KeepFromToc}}{}{<class warning omitted>}
\apptocmd{\tableofcontents}{\end{KeepFromToc}}{}{<class warning omitted>}

which works as expected (both starred and unstarred variants do not write a ToC entry).

Is this a recommended way to do this? Does this method have any bad side effects I've not thought of?

Here is an MWE for any testing you may wish to do:

\documentclass{memoir}
\usepackage{etoolbox}

\pretocmd{\tableofcontents}{\begin{KeepFromToc}}{}{}
\apptocmd{\tableofcontents}{\end{KeepFromToc}}{}{}

\begin{document}

\tableofcontents   
\chapter{Test Chapter}
\section{Test Section}

\end{document}
share|improve this question

2 Answers 2

up vote 2 down vote accepted

The \tableofcontents command is defined as

> \tableofcontents=macro:
->\@ifstar {\@nameuse {mem@tableofcontents}{01}}{\@nameuse {mem@tableofcontents}{00}}.

I had to use \show\tableofcontents, because the definition is indirect, via

\newlistof{tableofcontents}{toc}{\contentsname}

Thus

\makeatletter
\renewcommand{\tableofcontents}{%
  \@ifstar{\mem@tableofcontents{01}}
          {\mem@tableofcontents{01}}%
}
\makeatother

will do.

Complete example:

\documentclass{memoir}
\makeatletter
\renewcommand{\tableofcontents}{%
  \@ifstar{\mem@tableofcontents{01}}
          {\mem@tableofcontents{01}}%
}
\makeatother

\begin{document}

\tableofcontents
\chapter{Test Chapter}
\section{Test Section}

\end{document}

With \tableofcontents* it would be the same.

share|improve this answer

Without the etoolbox package, you can do something like this:

\documentclass{memoir}

\makeatletter
\let\oldtableofcontents\tableofcontents
\def\tableofcontents{\@ifstar{\oldtableofcontents*}{\oldtableofcontents*}}
\makeatother

\begin{document}

\tableofcontents  
\chapter{Test Chapter}
\section{Test Section}

\end{document}

(of course, in your .cls file you don't need \makeatletter, \makeatother).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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