我希望它\contentsname
是“目录”,并且与目录列表中的条目相同。
我正在使用report
文档类。这是我的代码:
\documentclass[a4paper, 12pt, twoside, titlepage, openright]{report}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{hyperref}
\title{Some Title}
\begin{document}
\pagenumbering{roman}
\maketitle
\cleardoublepage
\renewcommand{\contentsname}{\centering Table of Contents \par}
\addcontentsline{toc}{chapter}{\contentsname}
\tableofcontents
\pagenumbering{arabic}
\chapter{Introduction}
\end{document}
运行此程序时,我收到很多如下错误:
Paragraph ended before \HyPsd@@ProtectSpacesFi was complete. ...dcontentsline{toc}{chapter}{\contentsname}
Undefined control sequence. ...dcontentsline{toc}{chapter}{\contentsname}
Use of \HyPsd@RemoveMask doesn't match its definition. ...dcontentsline{toc}{chapter}{\contentsname}
Paragraph ended before \HyPsd@RemoveBraces was complete. ...dcontentsline{toc}{chapter}{\contentsname}
Extra }, or forgotten \endgroup. ...dcontentsline{toc}{chapter}{\contentsname}
但是当我将某些行的顺序更改为以下内容时:
\cleardoublepage
\addcontentsline{toc}{chapter}{\contentsname}
\renewcommand{\contentsname}{\centering Table of Contents \par}
\tableofcontents
没有错误,但当然\contentsname
在添加到之前没有改变toc
答案1
该宏\contentsname
指定用于内容名称和仅有的内容名称。您不应在此处放置\centering
或\par
。尽管如果您使用
\renewcommand{\contentsname}{\hfill Table of Contents \hspace*{\fill}}
它可以工作,但是当你使用
\addcontentsline{toc}{chapter}{\contentsname}
ToC 条目也将居中(试试看)。如果您使用
\addcontentsline{toc}{chapter}{Table of Contents}
一切都会好起来的。但是,正确/优雅的方法是使用诸如托克洛夫特:
\documentclass[a4paper, 12pt, twoside, titlepage, openright]{report}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{hyperref}
\usepackage{tocloft}
\renewcommand{\cfttoctitlefont}{\hfill\Large\bfseries}
\renewcommand{\cftaftertoctitle}{\hspace*{\fill}}
\title{Some Title}
\begin{document}
\pagenumbering{roman}
\maketitle
\cleardoublepage
\renewcommand{\contentsname}{Table of Contents}
\addcontentsline{toc}{chapter}{\contentsname}
\tableofcontents
\pagenumbering{arabic}
\chapter{Introduction}
\end{document}
答案2
不需要该\addtocontents
行。无论如何\tableofcontents
都会使用你的。但是也用于例如标题中,如果你添加类似命令,你会遇到问题。在我看来,将目录标题居中而不是其他章节居中是没有意义的,但如果你真的想要它,你最好修补\contentsname
\contentsname
\centering
\tableofcontents
\documentclass[a4paper, 12pt, twoside, titlepage, openright]{report}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{hyperref}
\title{Some Title}
\usepackage{xpatch}
\renewcommand\contentsname{Table of Contents}
\patchcmd\tableofcontents{\contentsname}{\hfill\contentsname\hfill\mbox{}}{}{\failed}
\begin{document}
\pagenumbering{roman}
\maketitle
\tableofcontents
\cleardoublepage
\pagenumbering{arabic}
\chapter{Introduction}
\end{document}
答案3
我认为您想要加载tocbibind
,这也会解决参考书目和索引的问题(如果有的话)。
\documentclass[a4paper, 12pt, twoside, titlepage, openright]{report}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{tocbibind}
\usepackage{hyperref}
\renewcommand{\contentsname}{Table of Contents}
\title{Some Title}
\begin{document}
\pagenumbering{roman}
\maketitle
\cleardoublepage
\tableofcontents
\cleardoublepage %%%<--- Don't forget this before \pagenumbering
\pagenumbering{arabic}
\chapter{Introduction}
\end{document}
另一方面,通常不会将目录单独列出。如果您后来决定不使用自引用,只需将加载更改为
\usepackage[nottoc]{tocbibind}
并运行 LaTeX 两次。
如果您想将目录标题置于中央,则需要一些技巧;这里有一个基于的技巧sectsty
。
\documentclass[a4paper, 12pt, twoside, titlepage, openright]{report}
\usepackage[left=1.5in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{tocbibind}
\usepackage{sectsty}
\usepackage{hyperref}
\renewcommand{\contentsname}{Table of Contents}
\newcommand{\centertitleofcontents}{}
\chapterfont{\centertitleofcontents}
\title{Some Title}
\begin{document}
\pagenumbering{roman}
\maketitle
\cleardoublepage
\begingroup
\renewcommand{\centertitleofcontents}{\centering}
\tableofcontents
\endgroup
\cleardoublepage % <--- Don't forget this before \pagenumbering
\pagenumbering{arabic}
\chapter{Introduction}
\end{document}