[Tex/LaTex] Customize headers without fancyhdr

header-footersectioningtitlesec

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?

Best Answer

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}{
  \sethead{\toptitlemarks\thesection\ \sectiontitle}{}{}}

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