[Tex/LaTex] Changing fancyhdr for a few pages

booksfancyhdr

I am writing a book with several parts and chapters that has a general introduction. I defined a header style for most of the book, that I can easily locally disable for a few pages with \pagestyle{empty}. The header has my name in even pages and the chapter mark (Chapter XX) in odd pages.

Now I would like to have a different kind of header for the introduction to avoid having an ugly "Chapter 0" as a header. I want the header to have my name in even pages and "introduction" in odd pages.

I defined a new style using fancyhdr. However nothing seems to work. After a few pages, I get the "Chapter 0" back in the introduction, and it lasts until the next chapter, although I have cleared the page. Can you please tell me wat is wrong in my MWE?

  \documentclass[12pt, A4]{book}

  \usepackage{tocbibind}
  \usepackage{blindtext}  
  \usepackage{emptypage}
  %%%%% Header
  \usepackage{fancyhdr}


  \fancypagestyle{style1}{
  \fancyhead[CO]{Introduction}
  \fancyfoot[CE]{Me}
  \fancyfoot[CE,CO]{\thepage}
  \renewcommand{\headrulewidth}{0pt}
  }

  \pagestyle{fancy}
  \fancyhf{}
  \fancyhead[CE]{Me}
  \fancyhead[CO]{\chaptername \  \thechapter}
  \fancyfoot[CE,CO]{\thepage}
  \renewcommand{\headrulewidth}{0pt}





  \begin{document}


  {\pagestyle{empty} 
  \tableofcontents
  \clearpage}


  \clearpage
  {\pagestyle{style1}
  \chapter*{Introduction}

  \Blindtext
  \Blindtext
  \Blindtext
  \Blindtext

    }
  \clearpage

  \pagestyle{fancy}

  \part{Thing}
  \Blindtext
  \Blindtext
  \Blindtext
  \Blindtext
  \Blindtext
  \chapter{Thing thing}
  \Blindtext
  \Blindtext
  \Blindtext
  \Blindtext
  \Blindtext
  \chapter{thingthingthing}

  \Blindtext
  \Blindtext
  \Blindtext
  \Blindtext
  \Blindtext


  \end{document}

Best Answer

Use \leftmark in the header. There is also a \rightmark. However, here in book-class, with default options, \leftmark would hold the chapter-name, with some formatting. This can be changed of course.

A few places throughout your document, you need to change what is in the header, as you haven't yet defined a non-starred \chapter. Then we can update \rightmark with \markkright, but there is no \markleft-command available. A shame really, could be useful. \markboth{left}{right} could be used here, though.

Suggestions

  • Instead of issuing new \pagestyle for when you want a single page to be different. Use \thispagestyle, which only changes the style for that one page. Have the first/main \pagestyle in the preamble, keeps it a bit tidier. Content and settings should in my opinion be separated as much as possible.
  • You don't need so many \clearpages. In book-class, the default option is openright, which means that every chapter will start at only right-hand sides.

Code

  \documentclass[12pt, a4paper]{book}

  \usepackage{tocbibind}
  \usepackage{lipsum}  
  \usepackage{emptypage}
  %%%%% Header
  \usepackage{fancyhdr}

  \pagestyle{fancy}
  \fancyhf{}
  \fancyhead[CE]{Me}
  \fancyhead[CO]{\leftmark}
  \fancyfoot[CE,CO]{\thepage}
  \renewcommand{\headrulewidth}{0pt}
    \pagestyle{fancy}
  \begin{document}

  \thispagestyle{empty}
  {\renewcommand\addcontentsline[3]{}
  \tableofcontents}
  \clearpage

  \chapter*{Introduction}\markboth{Introduction}{}
    \lipsum[1-9]
  \clearpage

  \part{Thing} \markboth{Part thing}{}

  \lipsum[1-10]

  \chapter{Thing thing}

  \lipsum[1-7]
  \chapter{thingthingthing}

  \lipsum[1-8]
  \end{document}
Related Question