0

Good day,

I would like to display a custom shorten version of the \leftmark with Fancyhdr.

Ideally it would be some type of switch with a case for each chapter.

Right now this is what the header's code looks like:

\usepackage{fancyhdr}

\pagestyle{fancy} \fancyhead{} \fancyfoot{} \fancyhead[LO,RE]{\slshape \leftmark} % I would like to implement the string converter here

In pseudo-code it could be something like this:

var headerText = \leftmark

switch headerText: case "The very long chapter name": headerText = "The chapter name" case "Chapter two is also very long": headerText = "Chapter two" ...

\fancyhead[LO,RE]{\slshape headerText}

An important point in what I'm trying to do is that the output string could be totally different from the original.

Thank you in advance!

EDIT: I'm using Pandoc to convert a Markdown file into a PDF. So it would be nice to have a solution compatible with this method of generating books. Thank you!

RilDev
  • 3
  • 3

1 Answers1

0

Here is a solution that more or less implements your pseudo code. You can add this in the beginning of your converted LaTeX code (pandoc output).

Update: we have to be careful not to use this trick for chapters that put non-alphabetic characters in \leftmark or in their title. See below.

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{lipsum}

\pagestyle{fancy} \fancyhead{} \fancyfoot{}

% Pseudo code:

% var headerText = \leftmark

% switch headerText: % case "The very long chapter name": % headerText = "The chapter name" % case "Chapter two is also very long": % headerText = "Chapter two" % ...

% \fancyhead[LO,RE]{\slshape headerText}

% Implementation:

\newcommand\chaptertitle[2]{\expandafter\def\csname=#1=\endcsname{#2}} \newcommand\shorttitle[1]{\expandafter\csname=\leftmark=\endcsname}

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}

\chaptertitle{One chapter with a very large chapter title}{Chapter One} \chaptertitle{Another chapter title that really is much too long}{Chapter Two} \fancyhead[LO,RE]{\slshape \shorttitle{\leftmark}} % I would like to implement the string converter here % \begin{document} \chapter{One chapter with a very large chapter title}

\lipsum

\chapter{Another chapter title that really is much too long}

\lipsum

\end{document}

This gives problems for chapters that use non-alphabetic characters in \markboth, for example \tableofcontents and similar ones. You can solve this by using the original header for these:

Update 2: The \newpage after \tableofcontents should be a \cleardoublepage (if documentclass book is used), otherwise there might be an empty page that still uses the bad \leftmark. Actually it is nicer to do a \newpage first, then change the header to be empty, then do a \cleardoublepage, and then change the header to its new form. In this case, if there is an empty page it will not have a header, which looks nicer. Such a page is always an even page, so we just change the header for even pages. So that is done with this update.

\renewcommand\contentsname{Index}
\fancyhead[RE]{\slshape \leftmark}
\tableofcontents
\newpage
\fancyhead[RE]{}
\cleardoublepage
\fancyhead[RE]{\slshape \shorttitle{\leftmark}}

enter image description here

  • Good day Sir, I have been trying to implement your solution but now I get this error `Package hyperref Warning: Rerun to get /PageLabels entry.

    [1] ! Missing \endcsname inserted.

    \protect l.90 \mainmatter` Do you know how I could solve it?
    – RilDev Dec 23 '21 at 07:52
  • I created a file header.tex that I call in the pandoc command like this: pandoc +RTS -Ksize -RTS -f gfm -V documentclass:book -H ./header.tex .... – RilDev Dec 23 '21 at 08:03
  • Here is what the code looks like: \usepackage{fancyhdr} \pagestyle{fancy} \fancyhead{} \fancyfoot{} \renewcommand{\headrulewidth}{0pt} \fancyhead[LE,RO]{\thepage} \fancyhead[LO]{\slshape \rightmark} \newcommand\chaptertitle[2]{\expandafter\def\csname=#1=\endcsname{#2}} \newcommand\shorttitle[1]{\expandafter\csname=\leftmark=\endcsname} \renewcommand{\chaptermark}[1]{\markboth{#1}{}} \chaptertitle{Le Sainct Evangile de Jesus Christ ; selon Sainct Matthieu.}{Sainct Matthieu} \fancyhead[RE]{\slshape \shorttitle{\leftmark}} – RilDev Dec 23 '21 at 08:03
  • Without seeing how you use the hyperref package, it is difficult to find out what is going wrong. Ideally you would give us a small document that shows the error. – Pieter van Oostrum Dec 23 '21 at 16:44
  • Alright, I created this Gist with 3 files https://gist.github.com/RilDev/a5da407affd43daa7df36b05f6c39c3b: nt.text is the original file, nt.tex is the output produced by pandoc -s nt.text -o nt.tex and error is the error I get when using md2pdf.sh, a custom script to generate the final PDF. md2pdf.sh is found here: https://github.com/RilDev/olivetan/tree/master/tools/pandoc. Thank you very much for your help! – RilDev Dec 24 '21 at 06:00
  • I'll have a look. – Pieter van Oostrum Dec 24 '21 at 10:11
  • The problem is caused by the \tableofcontents that uses \markboth with non-alphabetic stuff in it, most notable \protect and \MakeUppercase. \chaptertitle and \shorttitle can't handle this. So for these kinds of chapters use the original header without the \shorttitle trick. – Pieter van Oostrum Dec 24 '21 at 13:58
  • Thank you very much Sir for taking the time to debug my project. I confirm your solution is working. But not together with a table of content or a title page. No problem, I'll generate multiple PDF, cut them and assemble them together with pdftk! If anyone is interested, I'll be adding the scripts here: https://github.com/RilDev/olivetan/tree/master/tools, – RilDev Dec 25 '21 at 09:20