0

I'm using a template based on the memoir class. In some edgecases the chapter and section titles don't linebreak, and therefore overlaps with the page numbers in the header. Is there a way to prevent this from happening? Abbreviating the chapter or section titles is not an option, as a require the full name in the table of contents and the headers (as suggested here: fancyhdr long section name overlaps page number). Thanks!

Chapter and page number overlapping: Chapter overlapping page numbers

Section and page number overlapping: Section overlapping page numbers

I have narrowed it down to the following minimum working example:

\documentclass[b5paper, twoside]{memoir}
\usepackage{lipsum}

% Chapter style
\makechapterstyle{chstyle}{%
    \renewcommand\chapnamefont{%
        \normalfont\Huge\sffamily
    }
    \renewcommand\chaptitlefont{%
        \normalfont\HUGE\sffamily\bfseries
    }
    \renewcommand\printchaptertitle[1]{%
        \chaptitlefont \begin{Spacing}{1}##1\end{Spacing}
    }
    \renewcommand\printchapternum{\chapnamefont\Huge\space\thechapter}
}
\chapterstyle{chstyle}

% Headers and footers
% Normal page style
\makepagestyle{hfpage}
\makeevenhead{hfpage}{\thepage}{}{\leftmark}
\makeoddhead{hfpage}{\rightmark}{}{\thepage}
\makeevenfoot{hfpage}{}{}{}
\makeoddfoot{hfpage}{}{}{}
\makeheadrule{hfpage}{\textwidth}{\normalrulethickness}
\makepsmarks{hfpage}{%
    \nouppercaseheads
}
\pagestyle{hfpage}
\aliaspagestyle{chapter}{empty}

\begin{document}


\chapter{Hello 1 Hello 1 Hello 1 Hello 1 Hello 1 Hello 1 Hello 1 Helloooooo}

\lipsum[1-7]

\section{Section Section Section Section Section Section Section Section Sectionn}

\lipsum[8-9]
\end{document}
2
  • Welcome to tex.sx. I think that this has already been answered, but I can't find it. If the full text of the section or chapter header is required in the TOC, but a shorter version is permitted in a page header, that can be accomplished by using \markboth or \rightmark directly with the shorter form. Commented Nov 17, 2022 at 15:01
  • Thanks! The preferable solution would be to avoid using any sort of abbreviation - both in the the TOC and headers.
    – Thomas
    Commented Nov 17, 2022 at 15:17

2 Answers 2

0

Abbreviating the title in the heading should be an option.

The memoir class provides a second optional argument to the sectioning commands.

\chapter[<for toc>][<for heading>]{<title in the body>}

but I find the logic faulty, because it's much more frequent to have a shortened version for the heading, rather than for the table of contents.

Here's a way to specify the modified versions of the title with a key-value based syntax.

\documentclass[b5paper, twoside]{memoir}
\usepackage{lipsum}

% Chapter style
\makechapterstyle{chstyle}{%
    \renewcommand\chapnamefont{%
        \normalfont\Huge\sffamily
    }%
    \renewcommand\chaptitlefont{%
        \normalfont\HUGE\sffamily\bfseries
    }%
    \renewcommand\printchaptertitle[1]{%
        \chaptitlefont \begin{Spacing}{1}##1\end{Spacing}
    }%
    \renewcommand\printchapternum{\chapnamefont\Huge\space\thechapter}%
}
\chapterstyle{chstyle}

% Headers and footers
% Normal page style
\makepagestyle{hfpage}
\makeevenhead{hfpage}{\thepage}{}{\leftmark}
\makeoddhead{hfpage}{\rightmark}{}{\thepage}
\makeevenfoot{hfpage}{}{}{}
\makeoddfoot{hfpage}{}{}{}
\makeheadrule{hfpage}{\textwidth}{\normalrulethickness}
\makepsmarks{hfpage}{%
    \nouppercaseheads
}
\pagestyle{hfpage}
\aliaspagestyle{chapter}{empty}

\ExplSyntaxOn
\NewDocumentCommand{\fixedchapter}{sO{}m}
 {
  \IfBooleanTF{#1}
   {
    \memoirchapter*{#2}
   }
   {
    \thomas_fixtitles:Nnn \memoirchapter { #2 } { #3 }
   }
 }
\NewDocumentCommand{\fixedsection}{sO{}m}
 {
  \IfBooleanTF{#1}
   {
    \memoirsection*{#2}
   }
   {
    \thomas_fixtitles:Nnn \memoirsection { #2 } { #3 }
   }
 }

\keys_define:nn { thomas/fixtitles }
 {
  head .tl_set:N = \l__thomas_fixtitles_head_tl,
  toc  .tl_set:N = \l__thomas_fixtitles_toc_tl,
 }

\cs_new_protected:Nn \thomas_fixtitles:Nnn
 {
  \keys_set:nn { thomas/fixtitles }
   {% initialize
    head = { #3 },
    toc = { #3 },
    % evaluate the keys
    #2
   }
  #1 [ \l__thomas_fixtitles_toc_tl ] [ \l__thomas_fixtitles_head_tl ] { #3 }
 }

\ExplSyntaxOff

\AtBeginDocument{%
  \NewCommandCopy{\memoirchapter}{\chapter}%
  \RenewCommandCopy{\chapter}{\fixedchapter}%
  \NewCommandCopy{\memoirsection}{\section}%
  \RenewCommandCopy{\section}{\fixedsection}%
}

\begin{document}

\tableofcontents*


\chapter[
  head={Hello 1 Hello 1 \dots}
]{Hello 1 Hello 1 Hello 1 Hello 1 Hello 1 Hello 1 Hello 1 Helloooooo}

\lipsum[1-7]

\section[
  head={Section Section \dots},
  toc={Something for the toc},
]{Section Section Section Section Section Section Section Section Sectionn}

\lipsum[8-9]

\end{document}

Table of contents

enter image description here

Even page heading

enter image description here

Odd page heading

enter image description here

1
  • I followed the original LaTeX scheme where sectioning commands had one optional argument described as giving special text for ToC and header entries in that order. \section[toc and header]{title}. It seemed sensible to me to match use the same ordering. If Lamport said "toc and header" then optional arguments in the same order of "toc" and "header". There is a case for both our points of view. Commented Nov 18, 2022 at 18:42
0

memoir sectioning commands provide a second optional argument, so that you can specify a version for the table of contents and another one for running headers. E.g.

\section[Very long title, goes to Table of contents][Abridged title]{Very long title}
2
  • Hi, thank you for you answer. The preferable solution would be to avoid using any sort of abbreviation - both in the the TOC and headers.
    – Thomas
    Commented Nov 17, 2022 at 15:17
  • You could typeset the headings within a box, something like \makeevenhead{hfpage}{\thepage}{}{\parbox[t][2\baselineskip][t]{.8\textwidth}{\raggedleft\leftmark}} and \makeoddhead{hfpage}{\parbox[t][2\baselineskip][t]{.8\textwidth}{\rightmark}}{}{\thepage}. But then you should adjust the layout: the box takes more vertical space. How about decreasing the font size? Commented Nov 17, 2022 at 16:00

You must log in to answer this question.

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