# Sectionname of section at top of page in footer

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

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{\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
\lfoot{\textbf{Inhoudsopgave}}
\cfoot{\thepage /\pageref{LastPage}\hyperlink{page.\getpagerefnumber{anchor.lastpage}}{ $\rightarrow$}}
\rfoot{}
\renewcommand{\footrulewidth}{0.4pt}
}

% Cross-referencing
\usepackage[pageanchor]{hyperref}
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

% 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}

-

To accomplish what you want you need the extramarks add-on to fancyhdr.

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

\pagestyle{fancy}

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

\newpage

\section{Section B}
\lipsum[2]

\section{Section C}
\lipsum[3]

\newpage

\lipsum[4]

\end{document}

-