Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When fancyhdr prints the header and footer, I believe it uses the following order:

  1. Print header
  2. Print page
  3. Print footer

The problem with this order is that when you have a section in the middle of the page, say, section 4, but you want to print the section name of section 3 in the footer, the declared pagestyle with lfoot through rfoot and \section (embedded in a macro for variable output across the document) will print the section name which is closest to the footer, instead of the section which is at the top of the page.

So, without killing this process of automated footer output (namely the name of the current section at the footer), how can I get the section at the top of the page printed in my footer?

\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}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

To accomplish what you want you need the extramarks add-on to 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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.