Latex fancy package settings -- header and footer

The fancyhdr package manual can be obtained from texdoc fancydhr in CMD.

To use custom headers and footers in latex, you generally need to use the fancy macro package. The key is to think about the following examples, and after looking at the corresponding instructions, you can generally get the results you want. The following content mainly comes from CTEX - Online documentation, the example part has been modified and can be compiled directly. When using LaTeX to typeset articles and books, four header and footer formats are defined by default:

 
empty No header and footer
plain There is no header, and the page number is placed in the middle of the footer.
headings There is no footer, the header contains the title and page number of the chapter.
myheadings There are no footers, header page numbers and user-defined information.
article defaults to plain format, while book uses headings format. You can also use the \pagestyle command to set the format used in your document, eg using \pagestyle{empty} in the document will make subsequent pages without headers and footers. In general, these four formats can basically meet the requirements of typesetting. But in some cases, especially when users want to define their own header and footer formats, they will encounter many limitations and troubles. In this case, using fancyhdr package can easily achieve the goal.

Using the commands provided by the fancyhdr package, you can easily do:

  • Customize headers and footers.
  • Add decorative horizontal lines to headers and footers.
  • The width of the header and footer can exceed the width of the body text.
  • Multi-line headers and footers.
  • Parity pages use different formats for headers and footers.
  • The first page of each chapter uses a different format for headers and footers.
  • Floating object pages use headers and footers in different formats.
  • Control the font of headers and footers, including glyph, font family, capitalization, etc.

Simple example:

\documentclass{article}

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{} 
\chead{} 
\rhead{\bfseries The performance of new graduates} 
\lfoot{From: K. Grant} 
\cfoot{To: Dean A. Smith}
\rfoot{\thepage} 
\renewcommand{\headrulewidth}{0.4pt} 
\renewcommand{\footrulewidth}{0.4pt}
%......
\begin{document}
......

test!
\end{document}

In the above example, \thepage gives the page number of the current page, while \bfseries causes LaTeX to typeset the header in bold. If you want to use other forms in the document, such as no header and footer on the first page, you can use the command after \begin{document} and \maketitle

\thispagestyle{empty}

By default, the \maketitle command automatically formats its page as plain. Therefore, if you want to use fancy format on this page, you should use the command \thispagestyle{fancy} after \maketile.

Here's an example in a two-sided page layout:

\documentclass{book} 
\usepackage{fancyhdr}
\fancyhead{} % clear all fields 
\fancyhead[RO,LE]{\bfseries The performance of new graduates} \fancyfoot[LE,RO]{\thepage} 
\fancyfoot[LO,CE]{From: K. Grant} 
\fancyfoot[CO,RE]{To: Dean A. Smith} 
\renewcommand{\headrulewidth}{0.4pt} \renewcommand{\footrulewidth}{0.4pt}
\begin{document}
......

test!
\end{document}

The meanings of the letters in square brackets here are:

 
E even pages
O odd-numbered pages
L the left part of the header or footer
C The middle part of the header or footer
R The right part of the header or footer
H header
F footer
When formatting Chinese documents with CJK, the definitions of headers and footers with Chinese should be enclosed in \begin{CJK}{…}{…} and \end{CJK}. The easiest way is to put it after \begin{document} and \begin{CJK}{…}{…}. Such as:
\documentclass{book} 
\usepackage{CJK}
\usepackage{fancyhdr}
%......
\begin{document}
\begin{CJK}{GBK}{song}
\pagestyle{fancy}
\fancyhead{} % clear all fields 
\fancyhead[RO,LE]{\CJKfamily{hei} \bfseries \LaTeX{} 排版系统} 
\fancyhead[LO,RE]{\CJKfamily{hei>} \bfseries \leftmark}
\fancyfoot[LE,RO]{ \thepage } 
 \fancyfoot [LO,RE]{ \CJKfamily {kai} July 2000 AD} 
 \renewcommand { \headrulewidth } { 0.4pt } 
 \renewcommand { \footrulewidth } { 0.4pt } 
 % ......

test!
\end{CJK}
\end{document}

Author: visaya fan <visayafan[AT]gmail.com>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324733146&siteId=291194637