如何从前言中删除页眉

如何从前言中删除页眉

我有一个使用 创建的页眉fancyhdr。它重复出现在所有页面,包括目录和前言部分。我尝试使用,\thispagestyle{empty}但它只在第一页有效,而我需要在frontmatter

\documentclass[11pt]{book}
\usepackage{lipsum}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\nouppercase{\leftmark}}
\fancyhead[LO]{\nouppercase{\rightmark}}

\renewcommand\headrulewidth{1.5pt}
\makeatletter
\def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
        \hrule\@height\headrulewidth\@width\headwidth
        \vskip 2pt% 2pt between lines
        \hrule\@height.5pt\@width\headwidth% lower line with .5pt line width
        \vskip-\headrulewidth
        \vskip-1.5pt}}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\thispagestyle{empty}
\frontmatter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Table of contents

\tableofcontents
\chapter{Preface}
\lipsum
\lipsum
\mainmatter
\chapter{Ch1}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\lipsum
\section{test}
\chapter{ch2}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\end {document}

答案1

改变这个

\thispagestyle{empty}

\pagestyle{empty}

并在此处插入 \pagestyle{fancy}

\mainmatter
\chapter{Ch1}

现在看起来像

\mainmatter
\pagestyle{fancy}
\chapter{Ch1}

答案2

这是你想要的吗?在前言中,只有章节标题(目录/前言),第一章页面和空白页有空样式(整个文档)。我添加了选项twoside,并使用emptypagexpatche包来实现这一点。

\documentclass[11pt, twoside]{book}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{xpatch, emptypage}%
 \pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\nouppercase{\leftmark}}
\fancyhead[LO]{\nouppercase{\rightmark}}

\renewcommand\headrulewidth{1.5pt}
\makeatletter
\def\headrule{{\if@fancyplain\let\headrulewidth\plainheadrulewidth\fi
        \hrule\@height\headrulewidth\@width\headwidth
        \vskip 2pt% 2pt between lines
        \hrule\@height.5pt\@width\headwidth% lower line with .5pt line width
        \vskip-\headrulewidth
        \vskip-1.5pt}}
\makeatother

%%
\xpatchcmd{\chapter}{%
\thispagestyle{plain}}{%
\thispagestyle{empty}}{}{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\frontmatter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Table of contents
\tableofcontents
\chapter*{Preface}
\markboth{Preface}{Preface}
\addcontentsline{toc}{chapter}{Preface}
\lipsum
\lipsum[1-4]
\mainmatter
\pagestyle{fancy}
\chapter{Ch1}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\lipsum
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\chapter{ch2}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\lipsum
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}
\section{test}

\end {document} 

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

相关内容