# How to change header to list first section on page and not last section

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}

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

-
The headers are drawn after the page is written, so you'd need to store what the question number was at the top of the page and then use that in the header, rather than what it is at the end of the page... This would make questions that start at the top of the page tricky... – Seamus Jul 12 '11 at 10:06
Yep, but I don't know how to get a variable to be set at the proper time: ie, clear the variable after the headers are drawn so that it can be set to the new value for the next page. – Peter Grill Jul 12 '11 at 18:12
The problem is you can't set the value at the bottom of the last page as I do in this MWE because a question might start at the top of the page. But the question I linked to should give you a start... – Seamus Jul 19 '11 at 20:43
@Seamus: Yep, been attempting to use that solution for my needs. Had already cross referenced the two questions. Also posted an expanded MWE to your question, which illustrates a problem if the macro changes at the top of a page. – Peter Grill Jul 19 '11 at 20:53
@PeterGrill: Has this been resolved? I proposed an answer that seems to solve your problem. – Werner Jan 4 '12 at 19:30

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}{%
[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
}
[]% even-center
[Question~\toptitlemarks\thesubsection]% even-right
{Page~\thepage}% odd-left
{}% odd-center
{Question~\toptitlemarks\thesubsection}% odd-right
}

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

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.

-
This does produce the correct output for the give MWE. But, if you add \newpage immediately following \MyInclude{1}, then the top of Page 2 starts Question 2, but the title is Question 1. – Peter Grill Jan 5 '12 at 2:32
The original MWE has Question 4 beginning on the top of Page 3, but the pagination changes with titlesec, hence the need to adjust the MWE to have a question begin at the top of a page. – Peter Grill Jan 5 '12 at 2:39

You could use the command \toptitlemarks of the titlesec package. An example is given by Gonzalo Medina here: Customize headers without fancyhdr.

-
I have not been able to get this to work. I think the problem is that I am not using auto generated section numbers, but my own tokens that I change. – Peter Grill Jun 20 '11 at 17:43