为什么 setspace 包会改变我的 fancyhdr 设计?

为什么 setspace 包会改变我的 fancyhdr 设计?

经过几个小时的调试,我终于发现我的书目最后一页的页眉通过文本中的间距进行了更改。现在我的世界已经天翻地覆了。

请看一下这个正确的例如。\begin{spacing}和都\end{spacing}被注释掉了:

\documentclass[12pt, a4paper, 单面, 单列]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection\ #1}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\newcommand{\plainplain}{%
  \fancypagestyle{plain}{%
      \renewcommand{\headrulewidth}{0pt}
      \fancyhead[LO]{}
      \fancyhead[RE]{}
}}
\newcommand{\myplain}{%
  \fancypagestyle{plain}{%
    \fancyhead[LO]{\bfseries\rightmark}
    \fancyhead[RE]{\bfseries\leftmark}
    \renewcommand{\headrulewidth}{0.5pt}
    }}
\plainplain
\pagestyle{plain}

\usepackage{lipsum, filecontents}

\begin{document}

\frontmatter
\pagenumbering{Roman}

\tableofcontents
\clearpage

\listoffigures
\clearpage

\listoftables
\clearpage

\mainmatter % implies arabic pagenumbering
\myplain
\pagestyle{plain}

% \begin{spacing}{1.5}

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]

\clearpage
\plainplain
\pagestyle{plain}

\backmatter
\pagenumbering{roman}
\setcounter{page}{5}

\nocite{mybook1}
\nocite{mybook2}
\nocite{mybook3}
\nocite{mybook5}
\nocite{mybook6}
\nocite{mybook7}
\nocite{mybook8}
\nocite{mybook10}

\bibliographystyle{plainnat}

\begin{filecontents}{bibliography.bib}
@book{mybook1,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}

\bibliography{bibliography}
% \end{spacing}
\end{document}

请注意,右上角只有一个小的罗马页码;这同样适用于前面的参考书目页面。

在此处输入图片描述

这是我使用空格的错误版本;只剩下两行(\begin{spacing}\end{spacing}):

\documentclass[12pt, a4paper, oneside, onecolumn]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection\ #1}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\newcommand{\plainplain}{%
  \fancypagestyle{plain}{%
      \renewcommand{\headrulewidth}{0pt}
      \fancyhead[LO]{}
      \fancyhead[RE]{}
}}
\newcommand{\myplain}{%
  \fancypagestyle{plain}{%
    \fancyhead[LO]{\bfseries\rightmark}
    \fancyhead[RE]{\bfseries\leftmark}
    \renewcommand{\headrulewidth}{0.5pt}
    }}
\plainplain
\pagestyle{plain}

\usepackage{lipsum, filecontents}

\begin{document}

\frontmatter
\pagenumbering{Roman}

\tableofcontents
\clearpage

\listoffigures
\clearpage

\listoftables
\clearpage

\mainmatter % implies arabic pagenumbering
\myplain
\pagestyle{plain}

\begin{spacing}{1.5}

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]

\clearpage
\plainplain
\pagestyle{plain}

\backmatter
\pagenumbering{roman}
\setcounter{page}{5}

\nocite{mybook1}
\nocite{mybook2}
\nocite{mybook3}
\nocite{mybook5}
\nocite{mybook6}
\nocite{mybook7}
\nocite{mybook8}
\nocite{mybook10}

\bibliographystyle{plainnat}

\begin{filecontents}{bibliography.bib}
    \documentclass[12pt, a4paper, oneside, onecolumn]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection\ #1}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\newcommand{\plainplain}{%
  \fancypagestyle{plain}{%
      \renewcommand{\headrulewidth}{0pt}
      \fancyhead[LO]{}
      \fancyhead[RE]{}
}}
\newcommand{\myplain}{%
  \fancypagestyle{plain}{%
    \fancyhead[LO]{\bfseries\rightmark}
    \fancyhead[RE]{\bfseries\leftmark}
    \renewcommand{\headrulewidth}{0.5pt}
    }}
\plainplain
\pagestyle{plain}

\usepackage{lipsum, filecontents}

\begin{document}

\frontmatter
\pagenumbering{Roman}

\tableofcontents
\clearpage

\listoffigures
\clearpage

\listoftables
\clearpage

\mainmatter % implies arabic pagenumbering
\myplain
\pagestyle{plain}

\begin{spacing}{1.5}

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]

\clearpage
\plainplain
\pagestyle{plain}

\backmatter
\pagenumbering{roman}
\setcounter{page}{5}

\nocite{mybook1}
\nocite{mybook2}
\nocite{mybook3}
\nocite{mybook5}
\nocite{mybook6}
\nocite{mybook7}
\nocite{mybook8}
\nocite{mybook10}

\bibliographystyle{plainnat}

\begin{filecontents}{bibliography.bib}
@book{mybook1,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {\lipsum[1]},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}

\bibliography{bibliography}
\end{spacing}
\end{document}

看看错误的结果。它不应该是那样的。另外,参考书目最后一页是唯一错误的页面。所有其他页面都像我正确版本的示例一样。

在此处输入图片描述

问题: 为什么间距会改变我的参考书目最后一页的页眉?

答案1

spacing首先,你不应该在整个文档周围使用环境;这必然会带来问题,你发现了其中的一个。使用spacing环境;这必然会带来问题,您已经发现了其中的一个。仅对小段文本使用环境。正确的方法是(假设您必须使用增加的行间空间)来声明\onehalfspacing

您也不应该多次重新定义普通样式:您可以命名您想要的页面样式数。

\documentclass[12pt,a4paper]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}
\usepackage{lipsum, filecontents}

\setlength{\headheight}{14.5pt}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}

\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}

\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{plainplain}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhead[LO]{}%
  \fancyhead[RE]{}%
}
\fancypagestyle{myplain}{%
  \fancyhead[LO]{\bfseries\rightmark}%
  \fancyhead[RE]{\bfseries\leftmark}%
    \renewcommand{\headrulewidth}{0.5pt}%
}

\pagestyle{plainplain}

\begin{document}

\frontmatter

\tableofcontents

\listoffigures

\listoftables

\mainmatter % implies arabic pagenumbering
\pagestyle{myplain}

\onehalfspacing

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]


\cleardoublepage

\backmatter
\pagestyle{plainplain}
\pagenumbering{roman}
\setcounter{page}{5}

\nocite{*}

\bibliographystyle{plainnat}

\begin{filecontents}{\jobname.bib}
@book{mybook1,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}

\bibliography{\jobname}

\end{document}

半行间距是一种印刷错误,在意大利语中我会使用这个词奥布罗布里奥。另外,在后记中恢复罗马数字也没有任何道理(除非这是某些不懂印刷术的官僚机构制定的规定)。

我删除了该oneside选项,否则RELO选项就毫无意义了。请注意,仅添加 0.5pt\headheight是不够的(但fancyhdr会告诉您所需的长度)。

如果您不希望在开始章节时重新出现页面样式 plain,并且避免在文档正文中进行明确的声明,则修补相关命令会更容易:

\documentclass[12pt,a4paper]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{setspace}
\usepackage[round]{natbib}
\usepackage{lipsum, filecontents}

\usepackage{etoolbox}

% save the last frontmatter page number
\newcounter{savepage} % save the last frontmatter page number
% no plain style at chapter openings
\patchcmd{\chapter}{\thispagestyle{plain}}{}{}{}
% choose a page style for the frontmatter
\apptocmd{\frontmatter}{\pagestyle{frontback}}{}{}
% patch \mainmatter to save the page number and to choose a new page style
\patchcmd{\mainmatter}
  {\cleardoublepage}
  {\cleardoublepage
   \setcounter{savepage}{\value{page}}%
   \pagestyle{normal}%
  }{}{}
% patch \backmatter to choose a new page style and restore the page number
\apptocmd{\backmatter}
  {\pagestyle{frontback}%
   \pagenumbering{roman}%
   \setcounter{page}{\value{savepage}}%
  }{}{}

% set the headheight to a suitable value suggested by fancyhdr
\setlength{\headheight}{14.5pt}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}

\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}

\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{frontback}{%
  \renewcommand{\headrulewidth}{0pt}%
  \fancyhead[LO]{}%
  \fancyhead[RE]{}%
}

\fancypagestyle{normal}{%
  \fancyhead[LO]{\bfseries\rightmark}%
  \fancyhead[RE]{\bfseries\leftmark}%
  \renewcommand{\headrulewidth}{0.5pt}%
}

\begin{document}

\frontmatter

\tableofcontents

\listoffigures

\listoftables

\mainmatter % implies arabic pagenumbering

\onehalfspacing

\chapter{Introduction}
\lipsum[1-7]

\section{New Section}
\lipsum[8-14]


\cleardoublepage

\backmatter

\nocite{*}

\bibliographystyle{plainnat}

\begin{filecontents}{\jobname.bib}
@book{mybook1,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook2,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook3,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook4,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook5,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook6,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook7,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook8,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook9,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
@book{mybook10,
    title = {\lipsum*[1]},
    author = {myauthor},
    year = {2010}
}
\end{filecontents}

\bibliography{\jobname}

\end{document}

相关内容