[Tex/LaTex] How to change header to list first section on page and not last section

fancyhdrheader-footer

The standard use of header/footer is to display the chapter/section information, which normally begins on a new page. What I would like to do is to have the header show the first question that is on a particular page, not the last. This is useful when a question crosses over a page boundary, and I can refer to the header to know what question is at the top of the page.

In the MWE, when I look at Page 2, I can only tell that the first part belongs to Question 2 since I see Question 3 following it. Even worse is that the Page 2 header shows Question 4, but Question 4 does not begin until Page 3.

So, my question is, how do I adjust the setting of QuestionNumber so that it only changes value upon a page boundary, and have the header show the first question on a page.

\documentclass{article}
\usepackage{lipsum}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[LE, LO] {Page \thepage}
\fancyhead[RE, RO] {Question \the\QuestionNumber}

% Each question is included from a separate file. But,
% for these purposes can imitate that with this
\newtoks{\QuestionNumber}
\newcommand{\MyInclude}[1]{%
    \QuestionNumber={#1}
    Question #1\newline\smallskip \lipsum[1-3]
}

\begin{document}
\MyInclude{1} 
\MyInclude{2} 
\MyInclude{3} 
\MyInclude{4} 
\MyInclude{5} 
\end{document}

So, to summarize, I want Page 1 to show Question 1, Page 2 to display Question 2, and Page 3 ti display Question 4.

This question seems related Saving the value of a text macro at the start of the page.

Best Answer

The following code produces what you're after using titlesec and \toptitlemarks (as suggested by @StefanKottwitz). The main requirement is to use the pagestyles package option, and use a sectional command to define your numbered environment/command. In the example below, I used \subsection. However, it should also work for \section and \subsubsection too. I don't think it works for \paragraph. In order to (somewhat) match your formatting definition of \MyInclude, titlesec provides \titleformat. In this case, it removed the subsection number and typeset it in \normalfont.

\documentclass[twoside]{article}
\usepackage[pagestyles]{titlesec}% http://ctan.org/pkg/titlesec
\usepackage{lipsum}% http://ctan.org/pkg/lipsum

% Definition of the page style with required headers
\newpagestyle{TitleMarks}{%
  \sethead[Top is~\toptitlemarks\thesubsection]% even-left
    [First is~\firsttitlemarks\thesubsection]% even-center
    [Bottom is~\bottitlemarks\thesubsection]% even-right
    {Top is~\toptitlemarks\thesubsection}% odd-left
    {First is~\firsttitlemarks\thesubsection}% odd-center
    {Bottom is~\bottitlemarks\thesubsection}% odd-right
}
\newpagestyle{Headings}{%
 \sethead[Page~\thepage]% even-left
   []% even-center
   [Question~\toptitlemarks\thesubsection]% even-right
   {Page~\thepage}% odd-left
   {}% odd-center
   {Question~\toptitlemarks\thesubsection}% odd-right
}
\pagestyle{Headings}% Setting the page style to the above headings

% Reformatting \subsection to look like a normal numbered paragraph (without the number)
\titleformat{\subsection}[block]{\normalfont}{}{0pt}{\smallskip}
% Strip subsection numbering from other sectional counters
\renewcommand\thesubsection{\arabic{subsection}}

% Definition of \MyInclude{...}
\newcommand{\MyInclude}[1]{%
    \subsection{Question #1} \lipsum[1-3]
}

\begin{document}
\MyInclude{1}
\MyInclude{2}
\MyInclude{3}
\MyInclude{4}
\MyInclude{5}
\end{document}

enter image description here

To see the full \...titlemarks in action, use the TitleMarks page style (as opposed to Headings). It shows the top, first and bottom (via \top..., \first... and \bot...) subsection for every page.

Related Question