0

While trying to format a novel I've started writing, I've come across a problem with fancyhdr which I couldn't quite fix with the package documentation.

What I want is to have the same header on both chapter pages and non-chapter pages. Every header is supposed to have the page number on the right and the chapter title on the left.

Now, I'm already aware that \pagesyle{fancy} doesn't work on chapter pages; therefore, I redefined it via etoolbox, and that part worked flawlessly. On non-chapter pages however, fancyhdr doesn't want to display the headrule. This is what I want to fix: I want the headers on chapter and non-chapter pages to look alike.

Here's a mostly-MWE of what I've done up to this point:

\documentclass[12pt,ngerman,oneside]{book}
\usepackage{babel}
\usepackage[onehalfspacing]{setspace}

\usepackage{geometry}
    \geometry{a4paper,left=20mm,right=20mm, top=20mm, bottom=20mm} 
    \setlength\parindent{0pt}
    \setlength\parskip{18pt}
    \setlength{\headheight}{30pt} 

\usepackage{etoolbox}
    \makeatletter
        \patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%Sets pagestyle on chapter pages
        \pretocmd{\chapter}{\addtocontents{toc}{\protect\addvspace{-25\p@}}}{}{}%Sets parskip in TOC
    \makeatother

\usepackage{fancyhdr}
    \fancyhead[R]{\thepage}
    \fancyfoot{}

\usepackage{titlesec}
    \titleformat{\chapter}[display]{\normalfont\bfseries}{}{0pt}{\Large}%Font and font size of chapter titles
    \titlespacing*{\chapter}{0pt}{-70pt}{0pt}%Spacing above and below chapter titles

\begin{document}

\tableofcontents
\thispagestyle{empty}

\chapter{Lorem Ipsum A}
\setcounter{page}{1}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 

\pagebreak

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. 

\pagebreak

\chapter{Lorem Ipsum B}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

\end{document}



2
  • 2
    There is an automatic \thispagestyle{plain} in \chapter. See page 10 of the fancyhdr manual. Dec 21, 2020 at 1:57
  • You are missing \pagestyle{fancy}. Loading fancyhdr doesn't do it explicitly.
    – campa
    Dec 21, 2020 at 15:16

1 Answer 1

2

For (my) convenience I added the package kantlipsum to test more pages. More important also added a new pagestyle to put the chapter name in all the headers. The headers now appear on both chapter pages and non-chapter pages: the page number on the right and the chapter title on the left, with the rule.

% !TeX TS-program = pdflatex

\documentclass[12pt,ngerman,oneside]{book}
\usepackage{babel}
\usepackage[onehalfspacing]{setspace}

\usepackage{kantlipsum} % added dummy text

\usepackage{geometry}
\geometry{a4paper,left=20mm,right=20mm, top=20mm, bottom=20mm} 
\setlength\parindent{0pt}
\setlength\parskip{18pt}
\setlength{\headheight}{30pt} 

\usepackage{etoolbox}
\makeatletter
    \patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%Sets pagestyle on chapter pages
    \pretocmd{\chapter}{\addtocontents{toc}{\protect\addvspace{-25\p@}}}{}{}%Sets parskip in TOC
\makeatother

\usepackage{fancyhdr}   
 %\fancyhead[R]{\thepage}
 %\fancyfoot{}

\fancypagestyle{mystyle}{% added
    \fancyhf{}
    \fancyhead[L]{\leftmark}
    \fancyhead[R]{\thepage}
    \fancyfoot{}
}

\usepackage{titlesec}
\titleformat{\chapter}[display]{\normalfont\bfseries}{}{0pt}{\Large}%Font and font size of chapter titles
\titlespacing*{\chapter}{0pt}{-70pt}{0pt}%Spacing above and below chapter titles

\begin{document}
\tableofcontents
\thispagestyle{empty}

\setcounter{page}{1}
\pagestyle{mystyle} % apply from here to the end
\chapter{Lorem Ipsum A}
\kant[1-10]

\chapter{Lorem Ipsum B}
\kant[14-23]

\end{document}

D

or with the same result, perhaps a simpler code

\documentclass[12pt,ngerman,oneside]{book}
\usepackage{babel}
\usepackage[onehalfspacing]{setspace}

\usepackage{kantlipsum}

\usepackage{geometry}
\geometry{a4paper,left=20mm,right=20mm, top=20mm, bottom=20mm} 
\setlength\parindent{0pt}
\setlength\parskip{18pt}
\setlength{\headheight}{30pt} 

\usepackage{etoolbox}
\makeatletter
    %   \patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}%Sets pagestyle on chapter pages NOT LONGER NEEDED
    \pretocmd{\chapter}{\addtocontents{toc}{\protect\addvspace{-25\p@}}}{}{}%Sets parskip in TOC
\makeatother

\usepackage{fancyhdr}   
\fancypagestyle{plain}{%
\fancyhf{}
\fancyhead[L]{\leftmark}
\fancyhead[R]{\thepage}
\fancyfoot{}
}

\usepackage{titlesec}
\titleformat{\chapter}[display]{\normalfont\bfseries}{}{0pt}{\Large}%Font and font size of chapter titles
\titlespacing*{\chapter}{0pt}{-70pt}{0pt}%Spacing above and below chapter titles

\begin{document}
\tableofcontents
\thispagestyle{empty}   
\setcounter{page}{1}
\pagestyle{plain}
\chapter{Lorem Ipsum A}
\kant[1-10] 
\chapter{Lorem Ipsum B}
\kant[14-23]    
\end{document}

Although is not related with the question I would recommend to test the package memoir, devoted to writing books. Not only integrates many packages, easing the upgrades, but also offers many chapter styles, TOC, headers, title pages, etc. in a single environment.

2
  • Thanks, that did it! Also, is there a way to get rid of the the \chaptername (i.e. "Kapitel") in the header, to just display \thechapter and \chaptermark? Dec 21, 2020 at 22:50
  • It is originated by babel. Put addto\captionsngerman{\renewcommand{\chaptername}{}} in the preamble and "Kapitel" will go away. Dec 22, 2020 at 12:40

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.