2

Hi I use fancy header but the page number overlaps with the section name in the header. Is there a way to force either a tiny bit smaller text or just allow for longer margins in the header? Thanks.

MRE:

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[LE]{\small\leftmark}%left on even
\fancyhead[RO]{\small\rightmark}%right on odd
\fancyhead[RE,LO]{ {\textbf{Page \thepage}} } %page number is right on even left on odd
\pagestyle{fancy}
\begin{document}
\chapter{Introduction}
\section{something}
Filler text
\subsection{something something}
Filler text
\newpage
\section{REALLY REALLY LONG TITLE OF SECTION}
\newpage
\section{REALLY REALLYy REALLY REALLY LONG TITLE OF SECTION}
\end{document}

5
  • 1
    You can use a shorter version of the section title as an optional argument of \section.
    – Bernard
    Dec 5 '21 at 19:09
  • sorry I really want/need the section title and the section number to be present. Dec 5 '21 at 19:20
  • 2
    In this case, the only simple solution I can see would be to use a smaller font size for the header.
    – Bernard
    Dec 5 '21 at 19:30
  • @Bernard Is it possible to change the margin for just the header? Dec 5 '21 at 19:32
  • With fancyhdr, I'm not sure. With titleps (a companion package of titlesec, you have a \widenhead command.
    – Bernard
    Dec 5 '21 at 19:37
5

The normal way is to use \section[short title for ToC and header]{Long title for main document}.

\section[ABBREVIATED TITLE]{REALLY REALLY REALLY REALLY REALLY REALLY LONG TITLE OF SECTION}

I'm sure that you could abbreviate your title's text to fit in the header (and ToC); they are basically indicators to the full glory of the section title.

2

This uses adjustbox to reduce the width only if needed.

\documentclass{book}
\usepackage[headheight=20pt]{geometry}
\usepackage[utf8]{inputenc}
\newlength{\markwidth}
\newcommand{\setmarkwidth}{\sbox0{\textbf{Page \thepage}}%
  \setlength{\markwidth}{\dimexpr\textwidth-\columnsep-\wd0}%
  \ifodd\value{page}
    \sbox1{\rightmark}%
    \ifdim\wd1<\markwidth \setlength{\markwidth}{\wd1}\fi
  \else
    \sbox1{\leftmark}%
    \ifdim\wd1<\markwidth \setlength{\markwidth}{\wd1}\fi
  \fi}
\usepackage{adjustbox}
\usepackage{fancyhdr}
\fancyhf{}
\fancyhead[LE]{\setmarkwidth\adjustbox{width=\markwidth}{\leftmark}}%left on even
\fancyhead[RO]{\setmarkwidth\adjustbox{width=\markwidth}{\rightmark}}%right on odd
\fancyhead[RE,LO]{ {\textbf{Page \thepage}} } %page number is right on even left on odd
\pagestyle{fancy}
\begin{document}
\chapter{Introduction}
\section{something}
Filler text
\subsection{something something}
Filler text
\newpage
\section{REALLY REALLY REALLY REALLY REALLY REALLY LONG TITLE OF SECTION}
\newpage
some text here
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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