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 thepagestyles
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
.To see the full
\...titlemarks
in action, use theTitleMarks
page style (as opposed toHeadings
). It shows the top, first and bottom (via\top...
,\first...
and\bot...
) subsection for every page.