fancyhdr 重置几何来调整标题高度?

fancyhdr 重置几何来调整标题高度?

fancy对默认页眉和plain章节页眉使用了两种页面样式。这些页眉的高度应该完全不同,但是,LaTeX 似乎将两个页眉高度都调整到最大值,并尝试在整个文档中保持页眉高度不变。这是为什么?我该如何禁用此“功能”?

在此处输入图片描述

\documentclass[a4paper]{book}
\usepackage[includeheadfoot,top=0cm,headheight=16pt,headsep=3cm]{geometry}
\usepackage{color}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}

\fancyfoot[L]{%
  \textcolor{black}{\llap{%
      \colorbox{yellow}{\makebox[\textwidth][r]{\thepage}}}}}

\fancyhead[L]{%
    \textcolor{black}{\rlap{%
        \colorbox{yellow}{\leftmark}}}}

\fancypagestyle{plain}{%
\fancyhead{}%
\fancyhead[R]{%
    \textcolor{black}{\rlap{%
        \hspace{-9\marginparsep}%
        \setlength{\fboxsep}{14pt}
        \colorbox{yellow}{%
            \makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}}%       
      }}}}

\begin{document}
\chapter{foo}
text
\newpage
\section{bar}
text
\end{document}

答案1

由于 pagestyleplain不仅仅用于章节的第一页,因此最好直接覆盖页眉。Tikz 是一种选择。Everypage 是另一种选择(\AddThispageHook)。您可以创建一个\mychapter宏来简化代码。

\documentclass[a4paper]{book}
\usepackage[includeheadfoot,top=0cm,headheight=16pt,headsep=3cm]{geometry}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{calc}

\fancyfoot[L]{%
  \textcolor{black}{\llap{{%
      \colorbox{yellow}{\makebox[\textwidth][r]{\thepage}}}}}}

\fancyhead[L]{%
    \textcolor{black}{\rlap{%
        \colorbox{yellow}{\leftmark}}}}

\begin{document}
\pagestyle{fancy}
\chapter{foo}
\begin{tikzpicture}[remember picture,overlay]
\path (current page.north west) +(1in+\oddsidemargin+\textwidth-9\marginparsep,0pt)
  node[below right,inner sep=14pt,fill=yellow]
    {\makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}};
\draw (current page.north west) ++(1in+\oddsidemargin,-121pt) -- +(\textwidth,0pt);
\end{tikzpicture}

text
\newpage
\section{bar}
text
\end{document}

章节标题

这是使用 everypage 的解决方案。“光标”位于相对于左上角的 (1in,-1in) 处,并且不能移动。

\documentclass[a4paper]{book}
\usepackage[includeheadfoot,top=0cm,headheight=16pt,headsep=3cm]{geometry}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{everypage}

\fancyfoot[L]{%
  \textcolor{black}{\llap{{%
      \colorbox{yellow}{\makebox[\textwidth][r]{\thepage}}}}}}

\fancyhead[L]{%
    \textcolor{black}{\rlap{%
        \colorbox{yellow}{\leftmark}}}}

\newcommand{\chapterhead}{\fboxsep=14pt\relax% local to header
  \raisebox{\dimexpr 1in-109pt}[0pt][0pt]{%
    \rlap{\hspace{\dimexpr \oddsidemargin+\textwidth-9\marginparsep}%
       \colorbox{yellow}{%
         \makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}}}%       
    \rlap{\hspace{\oddsidemargin}\rule[-16pt]{\textwidth}{0.5pt}}%
}}

\begin{document}
\pagestyle{fancy}
\chapter{foo}
\AddThispageHook{\chapterhead}

text
\newpage
\section{bar}
text
\end{document}

另一个章节标题

答案2

这里有一个额外的建议,使用包scrlayer-scrpage代替fancyhdr。然后您可以为章节页面定义新的页面样式。

\documentclass[a4paper]{book}
\usepackage[includeheadfoot,top=0cm,headheight=16pt,headsep=3cm]{geometry}
\usepackage{xcolor}

\usepackage[automark,headsepline]{scrlayer-scrpage}
\clearpairofpagestyles
\addtokomafont{pageheadfoot}{\upshape\color{black}}

\lefoot*{\smash{\llap{\colorbox{yellow}{\makebox[\textwidth][r]{\pagemark}}}}}
\rofoot*{\smash{\rlap{\colorbox{yellow}{\makebox[\textwidth][l]{\pagemark}}}}}

\ohead{\colorbox{yellow}{\leftmark}}

% Declare new layers and a new page style for the first page of a chapter
\DeclareNewPageStyleByLayers{chapterfirstpage}{%
  chapternumber,chapternumberline,%
  plain.scrheadings.foot.odd,plain.scrheadings.foot.oneside%
}
\DeclareNewLayer[
   background,
   head,
   align=t,
   voffset=0pt,
   height=93pt,
   contents={%
      \parbox[b][\layerheight]{\layerwidth}{%
          \setlength{\fboxsep}{14pt}%
          \hfill\rlap{\colorbox{yellow}{%
              \makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}}}%
           \hspace*{9\marginparsep}}}
]{chapternumber}
\DeclareNewLayer[
  clone=plain.scrheadings.head.below.line,
  voffset=107pt,
  addvoffset=-\ht\strutbox,
  contents=\rule{\textwidth}{.4pt}
]{chapternumberline}

% use page style chapterfirstpage instead plain on the first page of a chapter
\usepackage{etoolbox}
\patchcmd{\chapter}{plain}{chapterfirstpage}{}{}

\usepackage{blindtext}% dummy text

\begin{document}
\chapter{foo}
\Blindtext
\section{bar}
\blindtext
\chapter{Foo}
\Blindtext[3]
\blinddocument
\end{document}

在此处输入图片描述

或者如果还有未编号的章节:

\documentclass[a4paper]{book}
\usepackage[includeheadfoot,top=0cm,headheight=16pt,headsep=3cm]{geometry}
\usepackage{xcolor}

\usepackage[automark,headsepline]{scrlayer-scrpage}
\clearpairofpagestyles
\addtokomafont{pageheadfoot}{\upshape\color{black}}

\lefoot*{\smash{\llap{\colorbox{yellow}{\makebox[\textwidth][r]{\pagemark}}}}}
\rofoot*{\smash{\rlap{\colorbox{yellow}{\makebox[\textwidth][l]{\pagemark}}}}}

\ohead{\colorbox{yellow}{\leftmark}}

% Declare new layers and a new page style for the first page of a chapter
\DeclareNewPageStyleByLayers{chapterfirstpage}{%
  chapternumber,chapternumberline,%
  plain.scrheadings.foot.odd,plain.scrheadings.foot.oneside%
}
\DeclareNewLayer[
   background,
   head,
   align=t,
   voffset=0pt,
   height=93pt,
   contents={%
      \parbox[b][\layerheight]{\layerwidth}{%
          \setlength{\fboxsep}{14pt}%
          \hfill\rlap{\colorbox{yellow}{%
              \makebox(23,93)[cb]{\Huge\textbf{\thechapter.}}}}%
           \hspace*{9\marginparsep}}}
]{chapternumber}
\DeclareNewLayer[
  clone=plain.scrheadings.head.below.line,
  voffset=107pt,
  addvoffset=-\ht\strutbox,
  contents=\rule{\textwidth}{.4pt}
]{chapternumberline}

\newcommand\chapternumberon{%
  \IfLayerAtPageStyle{chapterfirstpage}{chapternumber}{}{%
    \AddLayersToPageStyle{chapterfirstpage}{chapternumber,chapternumberline}}%
}
\newcommand\chapternumberoff{%
  \RemoveLayersFromPageStyle{chapterfirstpage}{chapternumber,chapternumberline}}

% use page style chapterfirstpage instead plain on the first page of a chapter
\usepackage{etoolbox}
\patchcmd{\chapter}{plain}{chapterfirstpage}{}{}

\usepackage{blindtext}% dummy text

\begin{document}
\chapternumberoff
\tableofcontents
\cleardoublepage
\chapternumberon
\chapter{foo}
\Blindtext
\section{bar}
\blindtext
\chapter{Foo}
\Blindtext[3]
\blinddocument
\end{document}

在此处输入图片描述

相关内容