Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

3 Answers

up vote 8 down vote accepted

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}
share|improve this answer
add comment

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!

share|improve this answer
5  
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
add comment

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}
\addtocounter{section}{-1}
\section{Title}

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.

share|improve this answer
    
This doesn't change anything in my test document? –  Caramdir Jun 3 '11 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 Jun 3 '11 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 Jun 3 '11 at 22:54
    
I see. And yes, I noticed that my solution was incorrect (it is already deleted). –  Caramdir Jun 3 '11 at 22:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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