3

Using fancyhdr and extramarks package, I would like to set a header whith the range of sections actually displayed on the page. Which means I have 3 possible cases:

  • Only 1 section is displayed : only one number should appear in header.
  • At least 2 sections are displayed : first and last number should appear in header.
  • A previous section ends on top of the page, then a new section begins : number from last section, then last new number of the page should appear.

So far I manage to set up my code for the two fists cases with the ifthen package, but I can't figure out how to express the third case properly.

Here's a example :

\documentclass[a4paper, twoside, 12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[frenchb]{babel}
\usepackage{extramarks}
\usepackage{ifthen}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyfoot{}
\fancyhead{}

\fancyhead[CE, CO]{\ifthenelse{\equal{\firstleftmark}{\lastleftmark}}
    {\firstleftmark}
    {\firstleftmark -- \lastleftmark}}
\fancyfoot[C]{\thepage}
\usepackage{lipsum}

\begin{document}
    \section{}
    \lipsum[1]
    
    \section{}
    \lipsum[1-5]

    \section{}
    \lipsum[1-4]
    
\end{document}

Once compiled, the problem appears on page 2. Only the new section is taken into account. How to create a range number in the header "inheriting" the number of the ending section on top of the page?

1
  • As far as I can tell from the manual of fancyhdr the problem arises from the way \rightmark and \leftmark are defined: they refer to instances on the same page . Here you want to compare an instance on a previous page to an instance on a new page. Or better said: compare the stored value of \lastleftmark of the previous page to the new value of \lastleftmark on the new page. Provided I correctly understand the sentence The leftmark contains the Left argument of the Last \markboth on the page. @cabohah answer seems to do that comparing.
    – alchemist
    Feb 16, 2023 at 14:08

1 Answer 1

2

I do not know, how to do it using fancyhdr and extramarks, but here is a suggestion using package scrlayer-scrpage:

\documentclass[a4paper, twoside, 12pt]{article}
\usepackage[utf8]{inputenc}% shouldn't be needed any more
\usepackage[frenchb]{babel}
\usepackage[headsepline]{scrlayer-scrpage}
\automark[section]{section}
\clearpairofpagestyles
\chead{%
  \Ifstr{\lefttopmark}{}% After deleting the marks or at the very beginning of
                        % the document, use \leftfirstmark instead of
                        % \lefttopmark
  {%
    \Ifstr{\leftfirstmark}{\rightbotmark}%
          {\leftfirstmark}%
          {\leftfirstmark\ -- \rightbotmark}%
  }%
  {%
    \Ifstr{\lefttopmark}{\rightbotmark}%
          {\lefttopmark}%
          {\lefttopmark\ -- \rightbotmark}%
  }%
}
\cfoot*{\pagemark}
\renewcommand*{\sectionmark}[1]{\markboth{\thesection}{\thesection}}

\usepackage{lipsum}

\begin{document}
    \section{}
    \lipsum[1]
    
    \section{}
    \lipsum[1-5]

    \section{}
    \lipsum[1-4]
    
\end{document}

three pages of output

2
  • Thank you for your solution that gives the behavior I was hoping for. I am however not familiar at all with KOMA-script packages, so I guess I'll have some reading to do. Since this header behavior is only required for a part of my work, is it possible to use it as a pagestyle different from the main style in a master-file?
    – Aktemon
    Feb 17, 2023 at 10:31
  • 1
    @Aktemon Yes. You can either change the pagestyle using \chead etc. inside the document wherever you want (and AFAIK it would even be local to groups), or you can define additional pairs of pagestyles (see section 18.2 of the KOMA-Script manual). The first approach should be easier, the second cleaner.
    – cabohah
    Feb 17, 2023 at 10:39

You must log in to answer this question.

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