使用时如何使“内容”一词位于中心\tableofcontents
?
我正在使用\usepackage{fancyhdr}
,但当我尝试类似的东西时似乎不起作用\renewcommand{\contentsname}{\centering Contents}
有可能解决这个问题吗?
给出一些代码:这是我正在使用的现有框架。
\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lipsum} % mock text
\fancypagestyle{plain}{% emulate the plain style
\renewcommand{\headrulewidth}{0pt}%
\fancyhf{}% clear all fields
\fancyfoot[C]{\thepage}%
}
\fancypagestyle{firstpage}{% a rule at the bottom
\renewcommand{\headrulewidth}{0pt}%
\renewcommand{\footrulewidth}{0.4pt}%
\fancyhf{}% clear all fields
\fancyfoot[L]{All rights reserved}%
}
\pagestyle{plain}
\begin{document}
\title{A paper}
\author{A. Uthor}
\maketitle
\thispagestyle{first page}
\tableofcontents
\section{section1}
\lipsum[1-20] % fill some pages
\end{document}
答案1
快速而肮脏的方法:假设你从来没有使用过\contentsname
其他地方,你可以使用
\let\origcontentsname\contentsname
\renewcommand{\contentsname}{\hfill\origcontentsname\hfill}
这会将旧名称存储到\origcontentsname
,您仍然可以在\renewcommand{}
\hfill\origcontentsname\hfill
\hfill
然后通过填充( )可用的空白使文本居中。
代码如下:
\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lipsum} % mock text
\fancypagestyle{plain}{% emulate the plain style
\renewcommand{\headrulewidth}{0pt}%
\fancyhf{}% clear all fields
\fancyfoot[C]{\thepage}%
}
\fancypagestyle{firstpage}{% a rule at the bottom
\renewcommand{\headrulewidth}{0pt}%
\renewcommand{\footrulewidth}{0.4pt}%
\fancyhf{}% clear all fields
\fancyfoot[L]{All rights reserved}%
}
\let\origcontentsname\contentsname
\renewcommand{\contentsname}{\hfill\origcontentsname\hfill}
\pagestyle{plain}
\begin{document}
\title{A paper}
\author{A. Uthor}
\maketitle
\thispagestyle{firstpage}
\tableofcontents
\section{section1}
\lipsum[1-20] % fill some pages
\end{document}
答案2
您可以使用etoolbox
修补\tableofcontents
并居中标题:
\documentclass{article}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage{lipsum} % mock text
\fancypagestyle{plain}{% emulate the plain style
\renewcommand{\headrulewidth}{0pt}%
\fancyhf{}% clear all fields
\fancyfoot[C]{\thepage}%
}
\fancypagestyle{firstpage}{% a rule at the bottom
\renewcommand{\headrulewidth}{0pt}%
\renewcommand{\footrulewidth}{0.4pt}%
\fancyhf{}% clear all fields
\fancyfoot[L]{All rights reserved}%
}
\pagestyle{plain}
\patchcmd{\tableofcontents}
{\contentsname}
{\hfill\contentsname\hfill}
{}
{}
\title{A paper}
\author{A. Uthor}
\begin{document}
\maketitle
\thispagestyle{firstpage}
\tableofcontents
\section{section1}
\lipsum[1-20] % fill some pages
\end{document}
如果你想居中全部章节标题,
\usepackage{titlesec}
\titleformat*{\section}{\Large\bfseries\centering}
答案3
加载tocloft
并放置
\renewcommand{\cfttoctitlefont}{\hfil \Large\bfseries}
在序言中。
\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lipsum} % mock text
\fancypagestyle{plain}{% emulate the plain style
\renewcommand{\headrulewidth}{0pt}%
\fancyhf{}% clear all fields
\fancyfoot[C]{\thepage}%
}
\fancypagestyle{firstpage}{% a rule at the bottom
\renewcommand{\headrulewidth}{0pt}%
\renewcommand{\footrulewidth}{0.4pt}%
\fancyhf{}% clear all fields
\fancyfoot[L]{All rights reserved}%
}
\usepackage{tocloft}
\usepackage{blindtext}
\renewcommand{\cfttoctitlefont}{\hfil \Large\bfseries}
\begin{document}
\title{A paper}
\author{A. Uthor}
\maketitle
\thispagestyle{firstpage}
\tableofcontents
\Blinddocument
\end{document}
答案4
这是使用etoolbox
包的另一种解决方案
\documentclass{article}
\usepackage{etoolbox}
\let\oldtableofcontents\tableofcontents
\renewcommand{\tableofcontents}{%
\let\olssection\section
\patchcmd{\section}{\bfseries}{\bfseries\centering}{}{}
\oldtableofcontents
\let\section\olssection
}
\usepackage{fancyhdr}
\usepackage{lipsum} % mock text
\fancypagestyle{plain}{% emulate the plain style
\renewcommand{\headrulewidth}{0pt}%
\fancyhf{}% clear all fields
\fancyfoot[C]{\thepage}%
}
\fancypagestyle{firstpage}{% a rule at the bottom
\renewcommand{\headrulewidth}{0pt}%
\renewcommand{\footrulewidth}{0.4pt}%
\fancyhf{}% clear all fields
\fancyfoot[L]{All rights reserved}%
}
\pagestyle{plain}
\begin{document}
\title{A paper}
\author{A. Uthor}
\maketitle
\thispagestyle{firstpage}
\tableofcontents
\section{section1}
\lipsum[1-20] % fill some pages
\end{document}