I have strange, old book chapter to rewrite in LaTeX. The most iritating thing is the header. On odd pages the header should consist of the section name etc. But the problem is that if the page starts new section this sections name should appear in header (standard) and if the does not (the page is continuation of previous started section) and somewhere lower on this page the next section starts then header should show the name of this continuation section (the name of the section that first line of text on current page belongs to).

I've googled a lot and stil have nothing. So simply - how to insert in the header the section name that the first line of text on current page belongs to instead of standard - the section name that starts somewhere lower on current page?

  • Is there a reason why you don't want to use fancyhdr to do this?
    – Alan Munn
    Commented Jun 3, 2011 at 22:21
  • Ok, lets start even with fancyhdr, then I could just get rid of this.
    – Necklon
    Commented Jun 3, 2011 at 22:26

3 Answers 3


The titlesec package offers \toptitlemarks to achieve the desired result; you could look at the file ttlps.def to see how this feature was implemented. A little example:


  \sethead{\toptitlemarks\thesection\ \sectiontitle}{}{}}


\chapter{Here is a title that needs to wrap over several lines and has short words}
\section{Test section one}
\section{Test section two}
\section{Test section three}
\section{Test section four}
\section{Test section five}


This is in fact a complex problem, as explained in the TeXbook (p260), because you need emit two marks, one just before the title and other just after it -- in other words, sectioning commands have to be modified somehow, and this is what titlesec does. AFAIK, no other package (including fancyhdr, which doesn't modify the mark mechanism in sections) automates this layout. And being the author of titlesec, I can assure you it was far from trivial!

  • 5
    Welcome to tex.sx -- hopefully, you will visit our site now and then and answer tricky questions about titlesec!
    – lockstep
    Commented Jun 4, 2011 at 9:40
  • @lockstep Thanx. My time is sparse but I'll do my best. Commented Jun 6, 2011 at 8:57

Perhaps this code in the preamble does what you need


EDIT. After rereading the question, it seems that I misunderstood, as the code puts in the header the last section title on the page.

What is needed is a local hack:


Perhaps it's possible to check if a section title starts at the very top of the page and in this case issue the commands.

  • This doesn't change anything in my test document?
    – Caramdir
    Commented Jun 3, 2011 at 22:35
  • @Caramdir Yes, it does. On page 5 the header becomes FIFTH SECTION, while it's FOURTH SECTION without the code. However this doesn't answer the OP's question, unfortunately. Yours neither, I'd say. Try adding \enlargethispage{2\baselineskip} after \section{second section}; in this way the third section title is at the top of the page, but the header still says SECOND SECTION.
    – egreg
    Commented Jun 3, 2011 at 22:51
  • And also when SECOND SECTION starts page, THIRD SECTION is somewhere lower on the same page in the header appers THIRD SECTION...
    – Necklon
    Commented Jun 3, 2011 at 22:54
  • I see. And yes, I noticed that my solution was incorrect (it is already deleted).
    – Caramdir
    Commented Jun 3, 2011 at 22:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .