3

To explain my problem let me start with my MWE:

\documentclass[a4paper,10pt]{book}
\usepackage{lipsum}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{extramarks}
\makeatletter
 \renewcommand\chapter{\@startsection%                          
  {chapter}{0}{0mm}%
  {2.5\baselineskip}{1.5\baselineskip}%
  {\centering\normalfont\large\scshape
  }%
 }
 \makeatother
 \newcommand{\mychap}[1]{
 \chapter*{#1}
 \markboth{#1}{#1}}
 \renewcommand{\sectionmark}{}
\renewcommand{\chaptermark}{\markboth{\thechapter}}
\pagestyle{fancy}
\fancyhf{}
\fancyhead{}

  \fancyhead[LE]{{\thepage}}
 \fancyhead[RE]{ {\itshape \nouppercase  \firstleftmark}}%higher level \scshape  \MakeUppercase
  \fancyhead[LO]{ {\itshape \nouppercase  \lastrightmark}} 
  \fancyhead[RO]{ {\thepage}}   %odd page
\begin{document}
\mychap{one}
\lipsum
\mychap{two}
aaa
\mychap{five halves}
\mychap{three}
\lipsum
\mychap{fourth}
\lipsum[5]
\mychap{five}
d
\end{document}

Notice that:

  1. I want to treat chapters as sections (no page break, simple title)
  2. In the header I want the name of the chapter

I was able to get these features

BUT

I would like to get also

  1. On the header of the odd pages I want the last chapter appearing in the page (this seems to work using \lastrightmark)
  2. On the header of the even pages I don't want the first chapter beginning on the page but, the current chapter, i.e. the chapter I'm reading while turning the page.

In my example

  1. On page 2 I would like to have "one" since chapter two begins after the pagebreak between page 1 and page 2;
  2. On page 4 I would like to have "four" since chapter five begins after the pagebreak between page 3 and page 4.

How can I get this?

Andrew Swann
  • 95,762
MaPo
  • 1,163
  • 1
    But you probably want the beginning chapter if it is on top of the page. Check the titleps package, see here for some background https://tex.stackexchange.com/questions/42621/how-to-add-cont-section-title-to-the-top-of-pages – Ulrike Fischer Apr 27 '17 at 11:27

1 Answers1

2

You can achieve these headers by setting the \extramarks and using them as follows:

\newcommand{\mychap}[1]{%
\chapter*{#1}%
\markboth{#1}{#1}%
\extramarks{#1}{#1}}

\fancyhead[RE]{\textit{\MakeLowercase{\firstxmark}}}
\fancyhead[LO]{\textit{\MakeLowercase{\lastxmark}}}

So in a document like your sample you get one' in the heading page 2 andfour` in the heading of page 4 as you requested.

Page 1

Page 2

Page 3

Page 4

\documentclass[a4paper,10pt]{book}

\usepackage{lipsum}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{extramarks}

\makeatletter
\renewcommand\chapter{\@startsection%
{chapter}{0}{0mm}%
{2.5\baselineskip}{1.5\baselineskip}%
{\centering\normalfont\large\scshape
}%
}
\makeatother

\newcommand{\mychap}[1]{%
\chapter*{#1}%
\markboth{#1}{#1}%
\extramarks{#1}{#1}}

\pagestyle{fancy}
\fancyhf{}
\fancyhead{}

\fancyhead[LE]{\thepage}
\fancyhead[RE]{\textit{\MakeLowercase{\firstxmark}}}
\fancyhead[LO]{\textit{\MakeLowercase{\lastxmark}}}
\fancyhead[RO]{\thepage}

\begin{document}

\mychap{One}
\lipsum
\mychap{Two}
Two words.
\mychap{Two and a half}
\mychap{Three}
\lipsum
\mychap{Four}
\lipsum[5]
\mychap{Five}
A sentence of five words.
\end{document}

Note that both \markboth and \extramarks are need to get the correct values (which may be reliance on "bug" in extramarks). Also I have use \MakeLowercase to force the heading text to the desired form as we are now no longer using the standard marks.

Andrew Swann
  • 95,762