zl程序教程

您现在的位置是:首页 >  其他

当前栏目

LaTeX页眉页脚自定义【有图有代码】

代码 自定义 LaTeX 页脚 页眉
2023-09-14 09:14:59 时间

平时在写报告或者论文时,老师或者学校给的模板的页眉页脚都不太一样,一般都需要自己来自定义。所以笔者在这里简单的介绍页眉页脚自定义的代码及其参数。希望能够帮助大家更好的理解和学习!

一、自定义页眉页脚示例【双页文档】\fancyhead \fancyfoot

自定义双边文档的页面样式
如果你的文档是双边的(即奇数页和偶数页的样式不同,例如书籍)且你希望自定义页眉和页脚,那么你可以使用\fancyhead\fancyfoot命令来做到这一点。下面给出一个例子:

\documentclass[a4paper,12pt,twoside]{book}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{Overleaf}
\fancyhead[RE,LO]{Guides and tutorials}
\fancyfoot[CE,CO]{\leftmark}
\fancyfoot[LE,RO]{\thepage}


\begin{document}

\chapter{Using different page styles}

Lorem ipsum dolor sit amet, consectetur adipiscing ...

在这里插入图片描述

1、代码讲解

为了自定义文档中的页眉和页脚,你需要首先引入fancyhdr包:

\usepackage{fancyhdr}

然后,设置"fancy"样式:\pagestyle{fancy}
命令\fancyhf{}会清除页眉和页脚。如果不使用这个命令,默认的“plain”样式会被使用。

然后\fancyhead是页眉,\fancyfoot是页脚。
上面两个命令的可选参数包括:【中括号里面的代码】

  • E:偶数页
  • O:奇数页
  • L:左侧
  • R:右侧
  • C:居中

例如,\fancyhead[LE,RO]{Overleaf}会在偶数页的页眉左侧显示“Overleaf”,在奇数页的右侧显示“Overleaf”。

单页文档不用E和O。

2、自定义代码

你可以使用下面的命令在页眉和页脚中添加自定义信息:

  • \thepage
    显示当前页的页码
  • \thechapter
    显示当前章(Chapter)的编码
  • \thesection
    显示当前节(Section)的编码
  • \chaptername
    显示文字Chapter。如果文档的默认语言不是英语,则显示Chapter的对应语言的翻译文字。
  • \leftmark\rightmark
    显示当前文档类型的最高级文档结构的名字和编码(例如,对于报告reports和书籍books,显示Chapter;对于文章articles,显示Section)。名字大写显示。

3、页眉和页脚的装饰线

当你使用fancyhdr包的时候,它会在页面显示两种装饰线(分别在页眉和页脚)。页脚的装饰线的宽度被设置为0pt,所以它默认是不可见的。我们可以改变它的宽度:

\documentclass[a4paper,12pt,twoside]{book}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{Overleaf}
\fancyhead[RE,LO]{Guides and tutorials}
\fancyfoot[CE,CO]{\leftmark}
\fancyfoot[LE,RO]{\thepage}

\renewcommand{\headrulewidth}{2pt}
\renewcommand{\footrulewidth}{1pt}

\begin{document}

\chapter{Using different page styles}

Lorem ipsum dolor sit amet, consectetur adipiscing ...

在这里插入图片描述
\renewcommand{\headrulewidth}{2pt}
命令将页眉线的宽度设置为2pt

\renewcommand{\footrulewidth}{1pt}
命令将页脚线的宽度设置为1pt

4、总页数

若要用到总页数,则需要导入包lastpage:

\usepackage{lastpage}

此时可以设置页码为如x/x的格式:(LastPage首字母大写)

\lhead{\thepage/\pageref{LastPage}}

如图:
在这里插入图片描述

二、自定义页眉页脚示例【单页文档】\rhead \rfoot

你可以使用·fancyhdr·来改变默认的页面结构。下面给出一个例子:

\documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\rhead{Overleaf}
\lhead{Guides and tutorials}
\rfoot{Page \thepage}

\begin{document}

\section{First Section}

Hello,  here  is  some  text  without  a  meaning.   This  
text  should  show  what  a printed text will look like at 
this place.  If you read this text, you will get no information.  
Really?  Is there no information?  Is there a difference between 
this ...

\end{document}

在这里插入图片描述

下面,我们介绍其他命令的用法:

  • \rhead{Overleaf}
    在页眉的右侧显示大括号之中的文字。

  • lhead{Guides and tutorials}
    在页眉的左侧显示大括号之中的文字。

  • \chead{}
    与上面的例子相似,大括号之中的文字会居中显示。

  • \rfoot{Page \thepage}
    在页脚右侧显示文字“Page”以及当前页的页码(\thepage)。文末列出了一系列自动生成内容的命令(例如章节编码等)。

  • \lfoot{ }
    在页脚左侧显示大括号之中的文字。

  • \cfoot{ }
    在页脚中间显示大括号之中的文字。

三、\pagestyle{}介绍

页面中页眉和页脚中显示的内容取决于当前页所激活的样式。这些样式的复杂性在书籍中尤为突出:

\documentclass[a4paper,12pt,twoside]{book}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}

\pagestyle{headings}

\begin{document}
\chapter{Sample Chapter}
\section{New section}

Hello, here is some text without a meaning.  This text should 
show what a printed text will look like at this place.  If you 
read this text, you will get no information.  Really?  Is there 
no information?  Is there a difference between this text and some 
nonsense like ``Huardest gefburn?  Kjift " not at all!...

\end{document}

在这里插入图片描述
命令\pagestyle{headings}将当前文档的页面样式设置为headings
注意上面用的是\pagestyle{fancy}

\pagestyle{}的参数还有有:

  • empty:不显示页眉和页脚
  • plain:这是默认样式;不显示页眉;页脚中显示居中的页码
  • myheadings:不显示页脚;页眉中显示页码,偶数页的页码显示在右侧,奇数页的页码显示在左侧;除了页码之外,还显示用户自定义的信息;还有一个例外,在每一章的第一页,不显示页眉,页脚显示居中的页码。
  • headings:没有页脚,页眉包含章节的标题和页码。

四、设置当前页面样式\thispagestyle{}

有些时候我们只需要改变某一页的页面样式,例如,创建一个空页,或者删除当前页的页眉和页脚等:

\chapter{Sample Chapter}
\thispagestyle{empty}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do 
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim 
}ad minim veniam, quis nostrud exercitation ullamco laboris nisi 
ut aliquip ex ea commodo consequat. Duis aute irure dolor in 
reprehenderit in voluptate velit es...
\end{document}

在这里插入图片描述
当然,你可以选择除了empty之外的任何样式。