我有一篇论文(书籍类文档),其中包含多个章节和附录。我使用 titletoc 和 titlesec 来格式化章节标题和目录。当我创建附录时,附录的标题格式按预期包含单词“附录”,但目录中的同一项目却包含单词“章节”。我不明白为什么我的代码在目录中返回单词“章节”而不是“附录”。有人知道如何在目录中获取单词“附录”而不是“章节”吗?
\documentclass[letter,12pt]{book}
\usepackage{titlesec}
\usepackage{titletoc}
% Chapter title formatting
\titleformat{\chapter} % command
[display] % shape
{\filcenter} % format
{\MakeUppercase{\chaptertitlename} \thechapter \vspace{1em}} % label
{0pt} % separation
{\MakeUppercase} % before code
% Chapter table of contents formatting
\titlecontents{chapter} % section name to be formatted
[1in] % distance from left margin
{} % code for formatting before the entry
{\contentslabel[\MakeUppercase{\chaptertitlename}~\thecontentslabel]{7em}\uppercase} % format when the chapter has a number
{} % format when the chapter has no number
{\titlerule*[1ex]{.}\contentspage} % format for the filler (leader line) and page number
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{An Example Chapter}
To be or not to be, that is the question...
\chapter{Another Example Chapter}
Whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune...
\appendix
\chapter{The First Appendix}
Note that the chaptername command returns \chaptername~while the appendixname command returns \appendixname~and the chaptertitlename command returns \chaptertitlename~but this behavior does not seem to be preserved in the table of contents.
\end{document}
答案1
\appendix
重新定义了\chaptertitlename
from \chaptername
to \appendixname
。但是它没有在附录开始的 toc 文件中放入任何信息:
\contentsline {chapter}{\numberline {1}An Example Chapter}{1}{}%
\contentsline {chapter}{\numberline {2}Another Example Chapter}{3}{}%
\contentsline {chapter}{\numberline {A}The First Appendix}{5}{}%
\contentsfinish
您可以重新定义\appendix
,将一个宏添加到 toc 文件中,\chaptertitlename
在本地重新定义。一种可能性是
\newcommand\originalappendix{}
\let\originalappendix\appendix
\renewcommand\appendix{\originalappendix\addtocontents{toc}{\protect\appendixintoc}}
\newcommand\appendixintoc{\def\chaptertitlename{\appendixname}}
例子:
\documentclass[letter,12pt]{book}
\usepackage{titlesec}
\usepackage{titletoc}
% Chapter title formatting
\titleformat{\chapter} % command
[display] % shape
{\filcenter} % format
{\MakeUppercase{\chaptertitlename} \thechapter \vspace{1em}} % label
{0pt} % separation
{\MakeUppercase} % before code
% Chapter table of contents formatting
\titlecontents{chapter} % section name to be formatted
[1in] % distance from left margin
{} % code for formatting before the entry
{\contentslabel[\MakeUppercase{\chaptertitlename}~\thecontentslabel]{7em}\uppercase} % format when the chapter has a number
{} % format when the chapter has no number
{\titlerule*[1ex]{.}\contentspage} % format for the filler (leader line) and page number
\newcommand\originalappendix{}
\let\originalappendix\appendix
\renewcommand\appendix{\originalappendix\addtocontents{toc}{\protect\appendixintoc}}
\newcommand\appendixintoc{\def\chaptertitlename{\appendixname}}
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{An Example Chapter}
To be or not to be, that is the question...
\chapter{Another Example Chapter}
Whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune...
\appendix
\chapter{The First Appendix}
Note that the chaptername command returns \chaptername~while the appendixname command returns \appendixname~and the chaptertitlename command returns \chaptertitlename~but this behavior does not seem to be preserved in the table of contents.
\end{document}
答案2
esdd 答案的变体。由于\appendix
如此\gdef\@chapapp{\appendixname}
,我们也需要使用全局定义。
\appendix
因此,我将指令的代码附加到.toc
文件中,以写入定义的变化:
\addtocontents{toc}{\gdef\protect\@chapapp{\protect\appendixname}}
\contentsfinish
以及提供的钩子中撤消它的指令titletoc
。
\documentclass[12pt]{book}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{etoolbox}
\makeatletter
\appto\appendix{\addtocontents{toc}{\gdef\protect\@chapapp{\protect\appendixname}}}
\appto\contentsfinish{\gdef\@chapapp{\chaptername}}
\makeatother
% Chapter title formatting
\titleformat{\chapter} % command
[display] % shape
{\filcenter} % format
{\MakeUppercase{\chaptertitlename} \thechapter \vspace{1em}} % label
{0pt} % separation
{\MakeUppercase} % before code
% Chapter table of contents formatting
\titlecontents{chapter} % section name to be formatted
[1in] % distance from left margin
{} % code for formatting before the entry
{\contentslabel[\MakeUppercase{\chaptertitlename}~\thecontentslabel]{7em}\MakeUppercase} % format when the chapter has a number
{} % format when the chapter has no number
{\titlerule*[1ex]{.}\contentspage} % format for the filler (leader line) and page number
\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{An Example Chapter}
To be or not to be, that is the question...
\chapter{Another Example Chapter}
Whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune...
\appendix
\chapter{The First Appendix}
Note that the chaptername command returns \chaptername~while the appendixname command
returns \appendixname~and the chaptertitlename command returns \chaptertitlename~but
this behavior does not seem to be preserved in the table of contents.
\end{document}
我将\uppercase
命令改为\MakeUppercase
,并删除了letter
不存在的选项。如果您愿意,它应该是letterpaper
(但可以省略,因为它是默认的)。