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 將以自己的方式處理該過程。
為了更好地控制頁首,可以使用 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[<even output>]{<odd output>}
\chead[<even output>]{<odd output>}
\rhead[<even output>]{<odd output>}
|
\lfoot[<even output>]{<odd output>}
\cfoot[<even output>]{<odd output>}
\rfoot[<even output>]{<odd output>}
|
希望以上命令的行為是相當直觀的:如果其中包含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 是 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}
|
有些人喜歡將當前頁碼與整個文件放在一起。LaTeX 僅提供對當前頁碼的訪問許可權。但是,您可以使用 lastpage 軟體包來查詢總頁數,如下所示
\usepackage{lastpage}
...
\cfoot{\thepage\ of \pageref{LastPage} }
|
請注意大寫字母。此外,在 \thepage 後新增一個反斜槓,以確保頁碼和“of”之間有足夠的空格。請記住,在使用引用時,您需要額外執行 LaTeX 一次來解析交叉引用。
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
}
|