页脚中页面顶部的节名称

页脚中页面顶部的节名称

fancyhdr打印页眉和页脚时,我相信它使用以下顺序:

  1. 打印页眉
  2. 打印页面
  3. 打印页脚

这种顺序的问题是,当你在页面中间有一个部分,比如说第 4 部分,但你想在页脚中打印第 3 部分的章节名称时,通过 and 声明的语句pagestylelfoot嵌入rfoot\section用于跨文档变量输出的宏中)将打印该章节名称,即最近到页脚,而不是位于顶部页面的。

因此,如果不终止自动页脚输出过程(即当前的部分位于页脚),如何才能将页面顶部的部分打印到页脚中?

\documentclass{article}

% I want these packages to remain intact with whatever change you're proposing
\usepackage[toc,titletoc,page]{appendix}
\usepackage{marginnote}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage[heightrounded]{geometry}
\usepackage{lastpage}
\usepackage{lipsum}

% Footer
\usepackage{fancyhdr}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{.4pt}

% Command for pageref for page of last page in footer
\cfoot{\thepage /\pageref{LastPage}\hyperlink{page.\getpagerefnumber{anchor.lastpage}}{ $\rightarrow$}}

% ToC name change
\renewcommand*\contentsname{Inhoudsopgave}



% Global pagestyle commands fancyhdr
\fancypagestyle{contents}{
\lfoot{\textbf{Inhoudsopgave}}
}
\fancypagestyle{document}{
                \rfoot{\hyperlink{page.1}{$\leftarrow$ Page 1}}
                \lfoot{\currentsection}
}
\fancypagestyle{plain}{% Declare plain page style to also print footer on the first page of the document
\lhead{}
\chead{}
\rhead{}
\lfoot{\textbf{Inhoudsopgave}}
\cfoot{\thepage /\pageref{LastPage}\hyperlink{page.\getpagerefnumber{anchor.lastpage}}{ $\rightarrow$}}
\rfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0.4pt}
}

% Cross-referencing
\usepackage[pageanchor]{hyperref}
\hypersetup{colorlinks=true,
    linkcolor=red,
    hypertexnames=false,
    pdfhighlight=/N
    }%

% Title settings (empty in MWE)
\title{}
\author{}
\date{}

\begin{document}

% Footer section name - Declare after ToC to keep section numbering and ToC intact    
\newcommand{\currentsection}{}
\let\oldsection\section
\renewcommand{\section}[1]{\oldsection{#1}\renewcommand{\currentsection}{#1}}

% Set footer style
\pagestyle{document}

\section{Week 1 - Intro \textcolor{blue}{\textbf{R}}}
\subsection{Objecten}
\subsubsection{Objecten met \'e\'en getal}

\newpage

\textbf{\Huge{THIS IS PART OF SECTION 1, BUT FOOTER SHOWS SECTION 2}}
\lipsum[1-3]

\section{Week 2 - Data sets genereren}

% Anchor on last page for rfoot
\label{anchor.lastpage}

\end{document}

答案1

为了实现你想要的,你需要extramarks附加到fancyhdr

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{lipsum}

\pagestyle{fancy}
\lhead{\firstleftmark}
\rhead{\leftmark}

\begin{document}
\section{Section A}
\lipsum[1]

\newpage

\section{Section B}
\lipsum[2]

\section{Section C}
\lipsum[3]

\newpage

\lipsum[4]

\end{document}

相关内容