1

Consider the following code inspired from egreg's answer to this topic:

\documentclass[11pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[top=1in, bottom=1in, left=1in, right=1in, headheight=13.6pt]{geometry}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage{hyperref}
\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhead[LE]{\leftmark}
\fancyhead[RO]{\rightmark}

\begingroup\lccode`~=`:
\lowercase{\endgroup
  \newcommand{\partmark}[1]{%
    \markboth{\MakeUppercase{\partname\space\thepart~ #1}}{}%
  }
  \renewcommand{\chaptermark}[1]{%
    \markright{\MakeUppercase{\chaptername\space\thechapter~ #1}}%
  }
}% end of \lowercase
\makeatletter
\patchcmd{\H@old@part}% would be \@part without hyperref
  {\markboth{}{}}
  {\partmark{#1}}
  {}{}
\makeatother

\begin{document}
\frontmatter
\part*{Préambule}
\tableofcontents
\chapter{Introduction}
\lipsum
\section{Première section}
\lipsum
\lipsum
\lipsum
\section{Deuxième section}
\lipsum
\lipsum
\lipsum

\mainmatter
\part{Premiere partie}
\chapter{Premier chapitre}
\lipsum
\section{Première section}
\lipsum
\lipsum
\lipsum
\section{Deuxième section}
\lipsum
\lipsum
\lipsum
\chapter{Second chapitre}
\lipsum
\section{Première section}
\lipsum
\lipsum
\lipsum
\section{Deuxième section}
\lipsum
\lipsum
\lipsum

\part{Deuxieme partie}
\chapter{Troisieme chapitre}
\lipsum
\section{Première section}
\lipsum
\lipsum
\lipsum
\section{Deuxième section}
\lipsum
\lipsum
\lipsum
\chapter{Quatrieme chapitre}
\lipsum
\section{Première section}
\lipsum
\lipsum
\lipsum
\section{Deuxième section}
\lipsum
\lipsum
\lipsum

\appendix
\part*{Annexes}
\chapter{Premère annexe}
\lipsum
\section{Première section}
\lipsum
\lipsum
\lipsum
\section{Deuxième section}
\lipsum
\lipsum
\lipsum
\chapter{Seconde annexe}
\lipsum
\section{Première section}
\lipsum
\lipsum
\lipsum
\section{Deuxième section}
\lipsum
\lipsum
\lipsum

\end{document}

First this does not work properly, regarding to the headings which is the subject of this question:

  • in the front matter, the section title (which should be Préambule) in heading is wrong.
  • the formatting of section title is weird (example : "Première PARTIE I: PREMIERE PARTIE")
  • in chapters, headings are based on the section and not on the chapter

Actually, I would like to be able to define my own headers "by hand" using a command during the book (this is not shown by the example, but I have a complex structure for a book). Like by redefining a command on each part/chapter:

\renewcommand{\currentpartheader}{Blablabla}
\renewcommand{\currentchapterheader}{BLABLABLA}

knowing that I want the \currentpartheader to be shown on even pages and \currentchapterheader to be shown on odd pages.

How to do that?

Note: I do not need to use fancyhdr if other packages can do the job.

  • 2
    It will probably easier to do with the titlesec package, and its titleps sibling, rather than fancyhdr. – Bernard May 3 '15 at 23:31
  • It's difficult to help since you only say that you would like to be able to defined your own headers but without giving the details as to what they should look like. – Andrew May 4 '15 at 1:22
0

\part*{} does not alter headers. If you want them changed, you can issue a \markboth{}{} manually with the content you desire. To avoid the sections appearing in the headers, you can redefine \sectionmark to just \relax. Finally, with egreg's premable, I don't have any odd mixing of cases in the headers.

\documentclass[11pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[top=1in, bottom=1in, left=1in, right=1in, headheight=13.6pt]{geometry}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage{hyperref}
\usepackage{lipsum}

%Note: this is a very preliminary attempt...,
%Fancier solutions are welcome
%The page numbering does not work correctly
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhead[LE]{\leftmark}
\fancyhead[RO]{\rightmark}

\begingroup\lccode`~=`:
\lowercase{\endgroup
  \newcommand{\partmark}[1]{%
    \markboth{\MakeUppercase{\partname\space\thepart~ #1}}{}%
  }
  \renewcommand{\chaptermark}[1]{%
    \markright{\MakeUppercase{\chaptername\space\thechapter~ #1}}%
  }
}% end of \lowercase
\makeatletter
\patchcmd{\H@old@part}% would be \@part without hyperref
{\markboth{}{}}
{\partmark{#1}}
{}{}
\makeatother
\def\sectionmark#1{\relax}

\begin{document}

  \frontmatter
  \part*{Préambule}
  \tableofcontents
  \markboth{\MakeUppercase{Préambule}}{}
  \chapter{Introduction}
  \lipsum
  \section{Première section}
  \lipsum
  \lipsum
  \lipsum
  \section{Deuxième section}
  \lipsum
  \lipsum
  \lipsum

  \mainmatter
  \part{Premiere partie}
  \chapter{Premier chapitre}
  \lipsum
  \section{Première section}
  \lipsum
  \lipsum
  \lipsum
  \section{Deuxième section}
  \lipsum
  \lipsum
  \lipsum
  \chapter{Second chapitre}
  \lipsum
  \section{Première section}
  \lipsum
  \lipsum
  \lipsum
  \section{Deuxième section}
  \lipsum
  \lipsum
  \lipsum

  \part{Deuxieme partie}
  \chapter{Troisieme chapitre}
  \lipsum
  \section{Première section}
  \lipsum
  \lipsum
  \lipsum
  \section{Deuxième section}
  \lipsum
  \lipsum
  \lipsum
  \chapter{Quatrieme chapitre}
  \lipsum
  \section{Première section}
  \lipsum
  \lipsum
  \lipsum
  \section{Deuxième section}
  \lipsum
  \lipsum
  \lipsum

  \appendix
  \part*{Annexes}
  \chapter{Premère annexe}
  \lipsum
  \section{Première section}
  \lipsum
  \lipsum
  \lipsum
  \section{Deuxième section}
  \lipsum
  \lipsum
  \lipsum
  \chapter{Seconde annexe}
  \lipsum
  \section{Première section}
  \lipsum
  \lipsum
  \lipsum
  \section{Deuxième section}
  \lipsum
  \lipsum
  \lipsum

\end{document}

If you want to set the headers manually, why not just say

\fancyhf[LE]{whatever}
\fancyhf[RO]{whatever else}

The only reason to mess around with marks is to have things automated. If you aren't automating them, you don't need that level of complexity.

0

The default headers for book class are given by

\def\@evenhead{\thepage\hfil\slshape\leftmark}%
\def\@oddhead{{\slshape\rightmark}\hfil\thepage}%

although the default does not include a line beneath the headers. These give you complete control over the header content, although for your purposes simply changing \leftmark and/or \rightmark should do.

Even more exotic headers or footers can be created using the everypage package, so long as you turn off the default headers. The following draws a line beneath the header.

\documentclass{book}
\usepackage{everypage}
\usepackage{lipsum}

\newif\ifspecialpage

\let\oldpagestyle=\thispagestyle
\renewcommand\thispagestyle[1]{\specialpagetrue\oldpagestyle{#1}}

\makeatletter
\AddEverypageHook{% draw line in \headsep
  \ifspecialpage\global\specialpagefalse\else
  \raisebox{\dimexpr -\topmargin-\headheight-0.5\headsep-.2pt}[0pt][0pt]%
  {\rlap{\ifodd\c@page\relax\hspace{\oddsidemargin}\else\hspace{\evensidemargin}\fi
  \rule{\textwidth}{.4pt}}}\fi}
\makeatother

\begin{document}

\chapter{Chapter One}
\lipsum[1-12]

\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.