在这儿在目录中的章节编号前添加单词“Chap”Chapter
在 中添加了 单词\documentclass{book}
。现在我想在 中使用它们\documentclass{report}
。我该怎么做?
答案1
这是对该类的修改report
(基本上,它是相同的代码,但抑制了的条件测试\frontmatter
);我定义了两个命令\AddChap
和\SuppChap
来激活(分别停用)添加:
\documentclass{report}
\makeatletter
\let\orig@chapter\@chapter
\newcommand\SuppChap{%
\let\@chapter\orig@chapter}
\newcommand\AddChap{%
\def\@chapter[##1]##2{\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
\addcontentsline{toc}{chapter}%
{Chap~\protect\numberline{\thechapter}##1}%
\else
\addcontentsline{toc}{chapter}{##1}%
\fi
\chaptermark{##1}%
\addtocontents{lof}{\protect\addvspace{10\p@}}%
\addtocontents{lot}{\protect\addvspace{10\p@}}%
\if@twocolumn
\@topnewpage[\@makechapterhead{##2}]%
\else
\@makechapterhead{##2}%
\@afterheading
\fi}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{Acknowledgements}
\AddChap
\chapter{Test Chapter One}
\chapter{Test Chapter Two}
\SuppChap
\appendix
\chapter{Test Appendix One}
\end{document}
答案2
使用该软件包,解决方案变得更加简单tocloft
。
\documentclass{report}
\usepackage{tocloft}
\newlength\mylength
\renewcommand\cftchappresnum{Chap~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}
\begin{document}
\tableofcontents
\chapter{Intro}
\chapter{Test}
\end{document}
输出(目录):
编辑
如果您想在附录中添加“Appendix”而不是“Chap”,您可以加载该包etoolbox
并在序言中添加以下几行:
\apptocmd{\appendix}
{\addtocontents{toc}{%
\protect\addtolength\protect\cftchapnumwidth{-\mylength}%
\protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
\protect\addtolength\protect\cftchapnumwidth{\mylength}}%
}{}{}
完成 MWE:
\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\apptocmd{\appendix}
{\addtocontents{toc}{%
\protect\addtolength\protect\cftchapnumwidth{-\mylength}%
\protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
\protect\addtolength\protect\cftchapnumwidth{\mylength}}%
}{}{}
\newlength\mylength
\renewcommand\cftchappresnum{Chap~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}
\begin{document}
\tableofcontents
\chapter{Intro}
\chapter{Test}
\appendix
\chapter{Conclusions}
\end{document}
输出(目录):