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.


\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
    Question #1\newline\smallskip \lipsum[1-3]


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, 2011 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. Jul 12, 2011 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, 2011 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. Jul 19, 2011 at 20:53
  • @PeterGrill: Has this been resolved? I proposed an answer that seems to solve your problem.
    – Werner
    Jan 4, 2012 at 19:30

2 Answers 2


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.

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

% Definition of the page style with required headers
  \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
 \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)
% Strip subsection numbering from other sectional counters

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


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.

  • 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. Jan 5, 2012 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. Jan 5, 2012 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. Jun 20, 2011 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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