0

While writing my Thesis, I have some chapters with very long names. When specifying the header with fancyhdr, I set the left (even) pages to contain the chapter name and the right (odd) pages to contain the section name. The problem is that sometimes my title is long and the header is split into two lines. As a result, the second line is aligned with the page number on the left of the page and not with the text. Like this:

enter image description here

And this, for odd pages

enter image description here

So I'd like to align the text according to the red vertical lines in each case on the headers and then leave the page number alone on the leftmost side (even pages) or rightmost side (odd pages). The problem is more prominent for even pages, but I'd also like to solve it for odd ones.

My MWE

\documentclass[a4paper,10pt, twoside,titlepage,english]{book}                                       %
\usepackage{fancyhdr}
\usepackage[english]{babel} % for proper hyphenation patterns
\usepackage{titlesec} % 


\usepackage{geometry}
    \setlength{\textwidth}{12.5cm}
    \setlength{\textheight}{19.7cm}
    \setlength{\oddsidemargin}{3.8cm} % Margen de las paginas impares  
    \setlength{\evensidemargin}{-0.74cm} % Margen de las paginas pares 
    \setlength{\topmargin}{-1.3cm} 
    \setlength{\headheight}{8pt}
    \setlength{\footskip}{0cm}

\pagestyle{fancy}%
\addtolength{\headheight}{10pt}%
\renewcommand{\chaptermark}[1]{\markboth{Chapter  \thechapter.\ #1 }  {}}%
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}%

\fancyhf{}%

\fancyhead[LE]{\fontfamily{phv} \footnotesize  \thepage \qquad \textsc \leftmark}%
\fancyhead[RO]{\fontfamily{phv} \footnotesize \textsc \rightmark \qquad \thepage}%

\renewcommand{\headrulewidth}{0pt}% 
\renewcommand{\footrulewidth}{0pt}% 
\addtolength{\headheight}{0.5pt}%




\titleformat{\chapter}[display] % cambiamos el formato de los capítulos
{\bfseries \Huge} % por defecto se usarán caracteres de tamañao \Huge en negrita
{ % contenido de la etiqueta
\filleft %texto alineado a la derecha
\fontfamily{phv}\Large\chaptertitlename \hspace{0.1cm}   % Formato de la palabra "Capítulo"
\Huge\thechapter % Formato del número del capítulo
}
{0mm} % espacio mínimo entre etiqueta y cuerpo
{\filleft} % Formato del texto del capítulo


\titlespacing*{\section}{0in}{12pt}{8pt}
\titlespacing*{\chapter}{0cm}{0cm}{0cm}
\titlespacing*{\subsection}{0in}{12pt}{8pt}

\parskip=8.5pt

\begin{document}


\chapter{A very very long chapter that will definitely need to be shown in two or more lines in the header}

And this is what the header would look like.
\newpage
\section{And this is a new section that hopefully will also expand in two or more lines in the header section, so that you can definitely see what I am talking about}

This is the new section.
\newpage 

Same as before for odd pages.

\end{document}

I'd really appreciate any help

1
  • I would use \chapter[short title˙for header and ToC]{A very very long chapter title definitely need to be shown in two or more lines in the document but not in headers or ToC} . In a case, that you can use memoir document class, than you can use \chapter[⟨toc-title⟩][⟨head-title⟩]{⟨title⟩} ... Multiline headers doesn't look nice.
    – Zarko
    Commented Nov 9, 2022 at 14:15

1 Answer 1

0

You can redefine the fancy heads:

\fancyhead[LE,RO]{\fontfamily{phv} \footnotesize \textsc \thepage }%
\fancyhead[RE]{\parbox[t]{0.95\textwidth}{\fontfamily{phv} \footnotesize  \textsc \leftmark}}%
\fancyhead[LO]{\parbox[b]{0.95\textwidth}{\fontfamily{phv} \footnotesize \textsc \rightmark}}%

This will place the page number on the left (even) or right (odd) side of the page and enclose the marks in a parbox to limit their width. The content of a parbox can be top-aligned [t] or bottom-aligned [b].

a

b

\documentclass[a4paper,10pt, twoside,titlepage,english]{book}                                       %
\usepackage{fancyhdr}
\usepackage[english]{babel} % for proper hyphenation patterns
\usepackage{titlesec} % 
    
\usepackage[showframe]{geometry}% changed to show the margins <<<<<<<<<<<<<<<<<<<<<<<<

\setlength{\textwidth}{12.5cm}
\setlength{\textheight}{19.7cm}
\setlength{\oddsidemargin}{3.8cm} % Margen de las paginas impares  
\setlength{\evensidemargin}{-0.74cm} % Margen de las paginas pares 
\setlength{\topmargin}{-1.3cm} 
\setlength{\headheight}{8pt}
\setlength{\footskip}{0cm}

\pagestyle{fancy}%
\addtolength{\headheight}{10pt}%
\renewcommand{\chaptermark}[1]{\markboth{Chapter  \thechapter.\ #1 }  {}}%
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}%

\fancyhf{}%

\fancyhead[LE,RO]{\fontfamily{phv} \footnotesize \textsc \thepage }% added <<<<<<<<<<<<<<<<<<<<<<<<
\fancyhead[RE]{\parbox[t]{0.95\textwidth}{\fontfamily{phv} \footnotesize  \textsc \leftmark}}% added <<<<<<<<<<<<<<<<<<<<<<<<
\fancyhead[LO]{\parbox[b]{0.95\textwidth}{\fontfamily{phv} \footnotesize \textsc \rightmark}}%a added <<<<<<<<<<<<<<<<<<<<<<<<

\renewcommand{\headrulewidth}{0pt}% 
\renewcommand{\footrulewidth}{0pt}% 
\addtolength{\headheight}{0.5pt}%


\titleformat{\chapter}[display] % cambiamos el formato de los capítulos
{\bfseries \Huge} % por defecto se usarán caracteres de tamañao \Huge en negrita
{ % contenido de la etiqueta
    \filleft %texto alineado a la derecha
    \fontfamily{phv}\Large\chaptertitlename \hspace{0.1cm}   % Formato de la palabra "Capítulo"
    \Huge\thechapter % Formato del número del capítulo
}
{0mm} % espacio mínimo entre etiqueta y cuerpo
{\filleft} % Formato del texto del capítulo


\titlespacing*{\section}{0in}{12pt}{8pt}
\titlespacing*{\chapter}{0cm}{0cm}{0cm}
\titlespacing*{\subsection}{0in}{12pt}{8pt}

\parskip=8.5pt

\begin{document}
    
    
    \chapter{A very very long chapter that will definitely need to be shown in two or more lines in the header}
    
    And this is what the header would look like.
    \newpage
    \section{And this is a new section that hopefully will also expand in two or more lines in the header section, so that you can definitely see what I am talking about}
    
    This is the new section.
    \newpage 
    
    Same as before for odd pages.
    
\end{document}
5
  • This leads to two headers, one on the top left and one on the top right of the document, which in the case of even pages makes the text overlap Commented Nov 9, 2022 at 14:26
  • @Katerina Kornilia I am sorry but I do not understand the issue: the page numbers are on the opposite side of the page from the headers, which are limited in size. They cannot overlap. Commented Nov 9, 2022 at 14:32
  • Thank you very much for the code. I am sorry, I wrote it in a wrong way. The code you provided works for the MWE but in my real document, I am getting the header twice with this code, once on the top-left and once on the top-right side. As I am not including anything else regarding headers in the real document, do you have any idea why this might happen? Commented Nov 9, 2022 at 14:41
  • @Katerina Kornilia Perhaps you did not remove older definitions of fancy headers? Try to create a MWE that shows the issue. Replace your content with dummy text while keeping your full preamble. There are ways to modify the headers without using fancyhdr. Commented Nov 9, 2022 at 14:48
  • Thank you very much! I was redifining fancyhdr across the document leading to double heading, one from the original and one from the modified. Commented Nov 9, 2022 at 15:05

You must log in to answer this question.

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