我正在編寫一個基於 的類memoir
,它提供了\tableofcontents
(在目錄中列出了目錄)和\tableofcontents*
(在目錄中沒有列出目錄)。
我見過這個問題及其許多重複項,但我想防止目錄本身被列在目錄中,無論使用者選擇什麼\tableofcontents*
與 \tableofcontents
。
根據memoir
手冊(第9.2節),我目前正在使用etoolbox
做
\pretocmd{\tableofcontents}{\begin{KeepFromToc}}{}{<class warning omitted>}
\apptocmd{\tableofcontents}{\end{KeepFromToc}}{}{<class warning omitted>}
其按預期工作(帶星號和不帶星號的變體都不會寫入目錄條目)。
這是推薦的方法嗎?這個方法有沒有我沒有想到的不良副作用?
以下是您可能想要進行的任何測試的 MWE:
\documentclass{memoir}
\usepackage{etoolbox}
\pretocmd{\tableofcontents}{\begin{KeepFromToc}}{}{}
\apptocmd{\tableofcontents}{\end{KeepFromToc}}{}{}
\begin{document}
\tableofcontents
\chapter{Test Chapter}
\section{Test Section}
\end{document}
答案1
該\tableofcontents
命令定義為
> \tableofcontents=macro:
->\@ifstar {\@nameuse {mem@tableofcontents}{01}}{\@nameuse {mem@tableofcontents}{00}}.
我必須使用\show\tableofcontents
,因為定義是間接的,透過
\newlistof{tableofcontents}{toc}{\contentsname}
因此
\makeatletter
\renewcommand{\tableofcontents}{%
\@ifstar{\mem@tableofcontents{01}}
{\mem@tableofcontents{01}}%
}
\makeatother
會做。
完整範例:
\documentclass{memoir}
\makeatletter
\renewcommand{\tableofcontents}{%
\@ifstar{\mem@tableofcontents{01}}
{\mem@tableofcontents{01}}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{Test Chapter}
\section{Test Section}
\end{document}
有了\tableofcontents*
它就一樣了。
答案2
如果沒有該etoolbox
包,您可以執行以下操作:
\documentclass{memoir}
\makeatletter
\let\oldtableofcontents\tableofcontents
\def\tableofcontents{\@ifstar{\oldtableofcontents*}{\oldtableofcontents*}}
\makeatother
\begin{document}
\tableofcontents
\chapter{Test Chapter}
\section{Test Section}
\end{document}
(當然,在您的.cls
文件中您不需要\makeatletter
, \makeatother
)。