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 Jun 3 '11 at 22:21
Ok, lets start even with fancyhdr, then I could just get rid of this. –  Necklon Jun 3 '11 at 22:26

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:

\documentclass{book}
\usepackage[pagestyles]{titlesec}
\usepackage{lipsum}

\newpagestyle{mystyle}{

\begin{document}
\pagestyle{mystyle}

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

\end{document}

-

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!

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

Perhaps this code in the preamble does what you need

\makeatletter
\def\rightmark{\expandafter\@rightmark\botmark\@empty\@empty}
\makeatother


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:

\stepcounter{section}{1}
\sectionmark{Title}

@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 Jun 3 '11 at 22:51