LaTeX/自定义页面页眉和页脚

此页面可能需要更新以反映当前知识。您可以帮助更新它,讨论进度,或请求帮助。

此模块可能需要完全重写才能适合其目标受众。您可以帮助重写它。请参阅相关讨论。

LaTeX 中的页面样式并非指页面尺寸,而是指文档的页眉和页脚。这些页眉通常包含文档标题、章节或节号/名称和页码。

标准页面样式[编辑 | 编辑源代码]

在普通 LaTeX 中更改页眉的可能性实际上非常有限。有两个可用的命令:\pagestyle{''style''} 将将指定的样式应用于当前页面和所有后续页面,而 \thispagestyle{''style''} 仅影响当前页面。可能的样式有

empty

页眉和页脚都清除

plain

页眉清除,但页脚包含页面中心处的页码。

headings

页脚为空,页眉根据文档类显示信息(例如,节名称)和页码右上角。

myheadings

页码位于右上角,并且可以控制页眉的其余部分。

命令 \markright 和 \markboth 可用于手动设置标题内容。以下命令放置在文章文档的开头将设置所有页面的页眉(单面)以包含左上角的“John Smith”,居中的“关于页面样式”和右上角的页码

\pagestyle{myheadings}

\markright{John Smith\hfill On page styles\hfill}

有一些特殊命令包含有关文档当前页面的详细信息。

\thepage

当前页面的编号

\leftmark

当前章节名称,如“第 3 章。这是章节标题”

\rightmark

当前节名称,如“1.6. 这是节标题”

\chaptername

当前语言的名称chapter。如果这是英语,它将显示“Chapter”

\thechapter

当前章节编号

\thesection

当前节编号

注意,\leftmark 和 \rightmark 会将名称转换为大写,无论文本的格式是什么。如果您希望它们打印章节的实际名称,而不会将其转换为大写,请使用以下命令

\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }

\renewcommand{\sectionmark}[1]{ \markright{#1}{} }

现在,\leftmark 和 \rightmark 将只打印章节和节的名称,没有数字,也不会影响格式。注意,这些重新定义必须插入after第一次调用\pagestyle{fancy}之后。\chaptermark的标准书籍格式为

\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{\chaptername\ \thechapter.\ #1}}{}}

注意:如果您在页脚或页眉中只提供两个不同“部分”中的长文本,您可能会看到文本重叠。

此外,使用以下命令,您可以定义页眉和页脚装饰线的粗细

\renewcommand{\headrulewidth}{0.5pt}

\renewcommand{\footrulewidth}{0pt}

第一行用于页眉,第二行用于页脚。将其设置为零意味着将没有线。

普通页面问题[编辑 | 编辑源代码]

需要注意的一个问题是,主要的节命令(\part、\chapter 或 \maketitle)指定 \thispagestyle{plain}。因此,如果您希望通过在文档开头插入 \pagestyle{empty} 来抑制所有样式,那么每个节的样式命令将覆盖您的初始规则,仅针对这些页面。为了实现预期的结果,可以在新的节命令之后加上 \thispagestyle{empty}。然而,\part 命令无法以这种方式修复,因为它设置了页面样式,但也前进到下一页,因此 \thispagestyle{} 无法应用于该页面。两种解决方案

只需在序言中写入 \usepackage{nopageno}。此软件包将使 \pagestyle{plain} 的效果与 \pagestyle{empty} 相同,在使用时有效地抑制页码。

如以下所述,使用 fancyhdr。

自定义页眉和页脚时,棘手的问题是如何在其中添加运行的节和章节名称。标准 LaTeX 使用两阶段方法来实现这一点。在页眉和页脚定义中,使用命令 \rightmark 和 \leftmark 分别代表当前节和章节标题。每当处理章节或节命令时,这两个命令的值都会被覆盖。为了最大程度地灵活,\chapter 命令及其朋友不会自己重新定义 \rightmark 和 \leftmark。它们调用另一个命令(\chaptermark、\sectionmark 或 \subsectionmark)负责重新定义 \rightmark 和 \leftmark,除非它们带星号——在这种情况下,如果要更新页眉和页脚行,则必须在节命令中使用 \markboth{Chapter/Section name}{}。

同样,一些软件包提供了解决方案

软件包 titleps 提供了另一种单阶段机制;

fancyhdr 将以自己的方式处理该过程。

使用fancyhdr自定义[编辑 | 编辑源代码]

为了更好地控制页眉,可以使用 Piet van Oostrum 编写的软件包 fancyhdr。它提供了多个命令,允许您自定义文档的页眉和页脚行。有关更完整的指南,该软件包的作者制作了这份文档。

首先,将以下行添加到序言中

\usepackage{fancyhdr}

\setlength{\headheight}{15.2pt}

\pagestyle{fancy}

你现在可以在文档中观察到一种新的样式。

代码 \headheight 需要大于或等于 13.6pt,否则您将收到警告,并且可能出现格式问题。页眉和页脚都包含三个元素,每个元素根据其水平位置(左、中或右)排列。

由 fancyhdr 支持的样式

四个 LaTeX 样式;

fancy 为所有页面定义一个新页眉,但普通样式页面除外,例如章节和标题页;

fancyplain 与之相同,但适用于所有页面(此页面样式已弃用,对于新文档,应使用 fancy 和 \fancypagestyle 的组合,请参阅手册的第 26 节)。

样式定制[编辑 | 编辑源代码]

可以使用 fancyhdr 特定的命令定制样式。这两种样式可以直接配置,而对于 LaTeX 样式,您需要调用 \fancypagestyle 命令。

要设置页眉和页脚样式,fancyhdr 提供了三种接口。它们都提供相同的功能,您只需要以不同的方式使用它们。选择您最喜欢的那个。

您可以使用以下六个命令(这些命令已弃用,请使用下面定义的新命令)。

\lhead[]{}

\chead[]{}

\rhead[]{}

\lfoot[]{}

\cfoot[]{}

\rfoot[]{}

希望以上命令的行为是相当直观的:如果其中包含head,它会影响页眉等等,显然,l、c 和 r 分别代表左、中和右。

您也可以使用命令 \fancyhead 用于页眉,以及 \fancyfoot 用于页脚。它们的工作方式相同,因此我们只解释第一个命令。语法是

\fancyhead[selectors]{output you want}

您可以选择性地用逗号分隔多个选择器。选择器如下

E

偶数页

O

奇数页

L

左侧

C

居中

R

右侧

因此,CE,RO 将引用偶数页的中心和奇数页的右侧。

\fancyhf 是 \fancyhead 和 \fancyfoot 的合并,因此得名。还有两个额外的选择器 H 和 F 分别用于指定页眉或页脚。如果您省略了 H 和 F,它将为两者设置字段。

这些命令只适用于 fancy 和 fancyplain。要定制 LaTeX 默认样式,您需要使用 \fancyplainstyle 命令。请参阅下面的示例。

为了干净地定制,我们建议您从头开始。为此,您应该擦除当前的页码样式。提供空值将使该字段为空白。因此

\fancyhf{}

将只是删除当前的页眉/页脚配置,以便您可以创建自己的配置。

普通页面[编辑 | 编辑源代码]

有两种方法可以更改普通页面的样式,例如章节和标题页。

首先,您可以使用 fancyplain 样式。如果您这样做,您可以在 fancyhdr 命令(例如 \lhead{...} 等等)中使用命令 \fancyplain{...}{...}。

当 LaTeX 想要创建一个空样式的页面时,它将在所有其他情况下插入 \fancyplain 的第一个参数,它将使用第二个参数。例如

\pagestyle{fancyplain}

\fancyhf{}

\lhead{ \fancyplain{}{Author Name} }

\rhead{ \fancyplain{}{\today} }

\rfoot{ \fancyplain{}{\thepage} }

它具有与先前代码相同的功能,但您将在标题和章节开头获得空页眉和页脚。

或者,您可以重新定义plain 样式,例如,当您想要一个真正普通的页面时。要使用的命令是 \fancypagestyle{plain}{...},并且参数可以包含之前解释的所有命令。一个例子如下

\pagestyle{fancy}

\fancypagestyle{plain}{ %

\fancyhf{} % remove everything

\renewcommand{\headrulewidth}{0pt} % remove lines as well

\renewcommand{\footrulewidth}{0pt}

}

在这种情况下,您可以使用除 fancyplain 之外的任何样式,因为它会覆盖您的重新定义。

示例[编辑 | 编辑源代码]

对于双面文档,通常会镜像相对页面的样式,您倾向于以内侧和外侧来思考。因此,与上述相同示例的双面文档版本是

\lhead[Author Name]{}

\rhead[]{Author Name}

\lhead[]{\today}

\rhead[\today]{}

\lfoot[\thepage]{}

\rfoot[]{\thepage}

这实际上表示作者姓名位于顶部外侧,今天的日期位于顶部内侧,当前页码位于底部外侧。使用 \fancyhf 可以使它更简洁

\fancyhf[HLE,HRO]{Author's Name}

\fancyhf[HRE,HLO]{\today}

\fancyhf[FLE,FRO]{\thepage}

以下是您可能用于双面文档的可能的样式的完整代码

\usepackage{fancyhdr}

\setlength{\headheight}{15pt}

\pagestyle{fancy}

\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }

\renewcommand{\sectionmark}[1]{ \markright{#1} }

\fancyhf{}

\fancyhead[LE,RO]{\thepage}

\fancyhead[RE]{\textit{ \nouppercase{\leftmark}} }

\fancyhead[LO]{\textit{ \nouppercase{\rightmark}} }

\fancypagestyle{plain}{ %

\fancyhf{} % remove everything

\renewcommand{\headrulewidth}{0pt} % remove lines as well

\renewcommand{\footrulewidth}{0pt}

}

使用 \fancypagestyle,还可以为您的文档定义多个样式,这些样式易于切换。以下是一个用于双面书籍样式的复杂示例

\fancypagestyle{fancybook}{%

\fancyhf{}%

% Note the ## here. It's required because \fancypagestyle is making a macro (\ps@fancybook).

% If we just wrote #1, TeX would think that it's the argument to \ps@fancybook, but

% \ps@fancybook doesn't take any arguments, so TeX would complain with an error message.

% You are not expected to understand this.

\renewcommand*{\sectionmark}[1]{ \markright{\thesection\ ##1} }%

\renewcommand*{\chaptermark}[1]{ \markboth{\chaptername\ \thechapter: ##1}{} }%

% Increase the length of the header such that the folios

% (typography jargon for page numbers) move into the margin

\fancyhfoffset[LE]{6mm}% slightly less than 0.25in

\fancyhfoffset[RO]{6mm}%

% Put some space and a vertical bar between the folio and the rest of the header

\fancyhead[LE]{\thepage\hskip3mm\vrule\hskip3mm\leftmark}%

\fancyhead[RO]{\rightmark\hskip3mm\vrule\hskip3mm\thepage}%

}

使用 scrlayer-scrpage 进行定制[编辑 | 编辑源代码]

软件包 scrlayer-scrpage 是 KOMA-script 套件的一部分。建议在使用每个 KOMA-script 类时使用此软件包来定制页眉和页脚。此软件包也可以与标准类一起使用。

\documentclass{book}

\usepackage{scrlayer-scrpage}

\usepackage{mwe}

\begin{document}

\chapter{Manta Ray}

\blindtext

\section{Taxonomy}

\blindtext[10]

\chapter{Mobula}

\blindtext

\section{Life style}

\blindtext[10]

\end{document}

仅仅加载该软件包并不会改变默认行为。章节标题位于左侧页面,节标题位于右侧页面。页码显示在外页眉中。

现在,我们如何定制页眉和页脚?

我们可以使用该软件包提供的命令,这些命令在软件包文档中进行了更详细的描述。

以下是真实文档中可能需要的几个示例。

如何将页码移动到页脚的中心,并移除页眉的字母大小写?[编辑 | 编辑源代码]

\documentclass{book}

\usepackage{mwe}

\usepackage[markcase=noupper% remove the uppercasing

]{scrlayer-scrpage}

\ohead{}% clear the outer head

\cfoot*{\pagemark}% the pagenumber in the center of the foot, also on plain pages

\begin{document}

\chapter{Manta Ray}

\blindtext

\section{Taxonomy}

\blindtext[10]

\chapter{Mobula}

\blindtext

\section{Life style}

\blindtext[10]

\end{document}

页眉和页脚都分为内侧、中心和外侧部分,可以独立设置。星号定义了在 plain 页面上也使用相同的内容。通常,在包含章节开始的页面上会使用 plain 样式。

如何在内侧页脚中显示我的姓名和论文标题?[编辑 | 编辑源代码]

\documentclass{book}

\usepackage{mwe}

\usepackage[markcase=noupper% remove the uppercasing

]{scrlayer-scrpage}

\ohead{}% clear the outer head

\ofoot*{\pagemark}% the pagenumber in the outer part of the foot, also on plain pages

\ifoot*{Walter Wombat\\ Is life on Mars possible?}% Name and title beneath each other in the inner part of the foot

\setlength{\footheight}{24.0pt}

\begin{document}

\chapter{Manta Ray}

\blindtext

\section{Taxonomy}

\blindtext[10]

\chapter{Mobula}

\blindtext

\section{Life style}

\blindtext[10]

\end{document}

如何更改页眉和页脚中的字体样式?[编辑 | 编辑源代码]

该软件包提供了一个与 KOMA-script 类相似的接口。您可以向页眉和页脚添加字体属性。页码可以独立设置。

\documentclass{book}

\usepackage{mwe}

\usepackage{xcolor}

\usepackage[markcase=noupper% remove the uppercasing

]{scrlayer-scrpage}

\ohead{}% clear the outer head

\addtokomafont{pagehead}{\sffamily}

\addtokomafont{pagefoot}{\tiny}% Making the foot extra tiny to demonstrate

\addtokomafont{pagenumber}{\large\bfseries\color{red!80!black}}% that the page number can be controlled on its own.

\ofoot*{\pagemark}% the pagenumber in the outer part of the foot, also on plain pages

\ifoot*{Walter Wombat\\ Is life on Mars possible?}% Name and title beneath each other in the inner part of the foot

\setlength{\footheight}{24.0pt}

\begin{document}

\chapter{Manta Ray}

\blindtext

\section{Taxonomy}

\blindtext[10]

\chapter{Mobula}

\blindtext

\section{Life style}

\blindtext[10]

\end{document}

第 n 页,共 m 页[编辑 | 编辑源代码]

有些人喜欢将当前页码与整个文档放在一起。LaTeX 仅提供对当前页码的访问权限。但是,您可以使用 lastpage 软件包来查找总页数,如下所示

\usepackage{lastpage}

...

\cfoot{\thepage\ of \pageref{LastPage} }

请注意大写字母。此外,在 \thepage 后添加一个反斜杠,以确保页码和“of”之间有足够的空格。请记住,在使用引用时,您需要额外运行 LaTeX 一次来解析交叉引用。

使用 titleps 进行定制[编辑 | 编辑源代码]

titleps 采用了一种单阶段方法,无需使用 \leftmark 或 \rightmark。

例如

\newpagestyle{main}{

\sethead[\thepage][\chaptertitle][(\thesection] % even

{\thesection)}{\sectiontitle}{\thepage}} % odd

\pagestyle{main}

定义了一个名为main的页码样式,在偶数页上页码位于左侧,奇数页上页码位于右侧,偶数页上章标题位于中央,等等。

使用 titleps,所有格式都在页码样式中完成,而不是部分在标记发出时完成,部分在标记检索时完成。以下示例类似于上一个示例,但元素是彩色的。

\usepackage[dvipsnames,usenames]{color}

\usepackage{titleps}

\newpagestyle{main}[\small]{

\setheadrule{.55pt}%

\sethead[\colorbox{black}{\color{white}{\thepage}}]% even-left

[\chaptertitle]% even-center

[\colorbox{CornflowerBlue}{\thesection}]% even-right

{\colorbox{CornflowerBlue}{\thesection}}% odd-left

{\sectiontitle}% odd-center

{\colorbox{black}{\color{white}{\thepage}}}% odd-right

}