# fancyhdr long section name overlaps page number

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[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}


• You can use a shorter version of the section title as an optional argument of \section. 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
• In this case, the only simple solution I can see would be to use a smaller font size for the header. 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. Dec 5 '21 at 19:37

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.

This uses adjustbox to reduce the width only if needed.

\documentclass{book}
\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{fancyhdr}
\fancyhf{}
\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}