使用 Fancyhdr 设置页码

使用 Fancyhdr 设置页码

我浏览过该网站上的许多帖子,但仍然无法按照我想要的方式在乳胶中放置页码。

本质上,我想模仿 MS Word 中放置在右上角的页码格式。我使用的代码将页码放置在与我的章节标题大致相同的级别。从表面上看,如果我可以将页码向上移动几个点,但章节标题保持不变,那么一切看起来都很好。任何帮助都将不胜感激!

这是我的代码:

\documentclass[12pt, a4paper]{article} 
\usepackage{fullpage}
\usepackage{setspace}
\usepackage{subfigure}
\usepackage{amsfonts, amsmath, amssymb}
\usepackage{dcolumn, pstricks, multirow}
\usepackage{epsfig, subfigure, subfloat, graphicx, float}
\usepackage{anysize, setspace}
\usepackage{verbatim, rotating}
\usepackage{epsfig,graphicx}
\usepackage[abbr]{harvard}
\usepackage{graphicx}
\usepackage{graphics}
\usepackage{hyperref}
\usepackage{epsfig}
\usepackage{epstopdf}
\usepackage{xcolor}
\usepackage{caption}
\usepackage[top=1in, bottom=1.5in, left=1in, right=1in]{geometry}
\usepackage{titlesec}
\titleformat{\section}[block] 
{\normalfont\Large\bfseries\filcenter}{\fbox{\itshape\thesection}}{1em}{}
\usepackage{fancyhdr}
\renewcommand*{\headrulewidth}{0pt}
\fancyhf{}
\pagestyle{fancy}
\fancyhead[RO,RE]{\thepage} %RO=right odd, RE=right even 
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\begin{document}

 \setcounter{page}{34} 
\section*{Appendix A: Methodology} 

stuff here

\end{document}

答案1

处理文档时,您会收到三个警告;其中两个与您的问题直接相关:

软件包 Fancyhdr 警告:\fancyhead 的 `E' 选项如果没有 twoside 选项,在输入行 27 上是无用的。

包 Fancyhdr 警告:\headheight 太小(0.0pt):至少设置为 14.49998pt。

第一个告诉您,除非您激活twoside类选项,否则E(对于 Even)不会有任何效果(类似的注释适用于)。我在下面的示例代码中添加了该选项(如果您不想要该选项,请删除它并从您的定义中O删除E和)。此外,没有必要使用,因为这相当于。Ofancyhdr[RE,RO][R]

第二个,告诉你,你的标题的默认保留间距太小,你需要使用

\setlength\headheight{14.5pt}

合并更改后的代码:

\documentclass[12pt,twoside,a4paper]{article} 
\usepackage{fullpage}
\usepackage{setspace}
\usepackage{subfigure}
\usepackage{amsfonts, amsmath, amssymb}
\usepackage{dcolumn, pstricks, multirow}
\usepackage{epsfig, subfigure, subfloat, graphicx, float}
\usepackage{anysize, setspace}
\usepackage{verbatim, rotating}
\usepackage{epsfig,graphicx}
\usepackage[abbr]{harvard}
\usepackage{graphicx}
\usepackage{graphics}
\usepackage{hyperref}
\usepackage{epsfig}
\usepackage{epstopdf}
\usepackage{xcolor}
\usepackage{caption}
\usepackage[top=1in, bottom=1.5in, left=1in, right=1in]{geometry}
\usepackage{titlesec}
\titleformat{\section}[block] 
{\normalfont\Large\bfseries\filcenter}{\fbox{\itshape\thesection}}{1em}{}
\usepackage{fancyhdr}
\renewcommand*{\headrulewidth}{0pt}
\fancyhf{}
\pagestyle{fancy}
\fancyhead[R]{\thepage} %R right on all pages 
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\setlength\headheight{14.5pt}
\begin{document}

 \setcounter{page}{34} 
\section*{Appendix A: Methodology} 

stuff here

\end{document}

我添加了一些框架作为视觉指南的showframe选项:geometry

在此处输入图片描述

顺便问一下,你真的需要在序言中列出所有这些包吗?还要注意,你多次加载了同一个包(避免这种情况)。此外,你加载了graphicsgraphicx,只需加载后者就足够了。

如果要将标题进一步上移,可以增加长度headsep;由于您已经在加载geometry,因此可以使用其headsep键:

\usepackage[top=1in, bottom=1.5in, left=1in, right=1in,headsep=1cm]{geometry}

要将页码向右移动,可以使用\fancyheadoffset,例如

\fancyhead[R]{\thepage} 
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\setlength\headheight{14.5pt}
\fancyheadoffset[R]{1cm}

当然,不要1cm使用,而是使用最适合您需要的长度。

相关内容