2

I am having trouble defining my headers with the fancyhdr package. Here is what I want : - on the left page, on the left side : partnumber.chapternumber. Chapter Name - on the right page, on the right side :partnumber.chapternumber.sectionnumber Section Name

I tried to redefine the chaptermark, with no success. Here is a MWE.

\documentclass[twoside,12pt,openright,a4paper,usenames,dvipsnames]{book}
\usepackage[linktoc=all,hyperindex]{hyperref}
\usepackage{chngcntr}
\counterwithin{chapter}{part}
\usepackage{lipsum}

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\renewcommand{\thechapter}{\arabic{chapter}}
\titleformat{name=\chapter}[display]
{\bfseries\LARGE}
{\filleft\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{0ex}
{%\titlerule
\vspace{2ex}%
\filleft}
[\vspace{4ex}%
\titlerule]

\titleformat{name=\chapter,numberless}
{\bfseries\LARGE}
{}
{0ex}
{%\titlerule
\filleft\MakeUppercase}
[\vspace{4ex}%
\titlerule]
\titlespacing*{\chapter}{0pt}{-25pt}{40pt}

\titleformat{\part}[frame]
  {\bfseries\Huge}
  {\filright\large\enspace{\partname}\enspace}
  {40pt}
  {\Large\filcenter\MakeUppercase}
\titleclass{\part}{top}

\makeatletter
\renewcommand{\thechapter}{\@arabic\c@chapter}
\renewcommand{\p@chapter}{\thepart.}
\makeatother

\renewcommand{\chaptermark}[1]{%
\markboth{\thepart.\thechapter.\ #1}{}}

\renewcommand{\sectionmark}[1]{%
\markboth{\thechapter.\ #1}{}}

\usepackage{fancyhdr}
\fancypagestyle{bookstyle}{
\fancyhf{}
\fancyhead[LE]{\leftmark}
\fancyhead[RO]{\rightmark}

\fancyfoot[RO,LE]{\thepage}}
\fancypagestyle{plain}{
\renewcommand\headrulewidth{0pt}
\fancyhf{}
}

\begin{document}
\pagestyle{bookstyle}
\part{Part 1}
\chapter{Chap 1}
\label{chap:chapI1}
\section{Sec 1}
\lipsum[1-10]
\chapter{Chap 2}
\part{Part 2}
\chapter{Chap 1}
\chapter{Chap 2}
\end{document}

Maybe there is a conflict with the titlesec package, I can't find out.

1
  • I don't think it good idea make (1) plain pages style as empty (2) clearpage in book style
    – touhami
    Apr 25, 2015 at 10:51

1 Answer 1

1

You're basically forgetting to issue \pagestyle{fancy}.

However, also the order of your packages is wrong. Here's a fixed version.

\documentclass[twoside,12pt,openright,a4paper]{book}

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\usepackage{fancyhdr}
\usepackage{chngcntr}
\usepackage{lipsum}
\usepackage[linktoc=all,hyperindex]{hyperref}

\counterwithin*{chapter}{part}
\makeatletter
\renewcommand{\p@chapter}{\thepart.}
\makeatother


\titleformat{name=\chapter}[display]
  {\bfseries\LARGE}
  {\filleft\MakeUppercase{\chaptertitlename} \Huge\thechapter}
  {0ex}
  {%\titlerule
   \vspace{2ex}%
   \filleft}
  [\vspace{4ex}%
   \titlerule]

\titleformat{name=\chapter,numberless}
  {\bfseries\LARGE}
  {}
  {0ex}
  {%\titlerule
   \filleft\MakeUppercase}
  [\vspace{4ex}%
   \titlerule]
\titlespacing*{\chapter}{0pt}{-25pt}{40pt}

\titleformat{\part}[frame]
  {\bfseries\Huge}
  {\filright\large\enspace{\partname}\enspace}
  {40pt}
  {\Large\filcenter\MakeUppercase}
\titleclass{\part}{top}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{%
  \markboth{\thepart.\thechapter.\ #1}{}}

\renewcommand{\sectionmark}[1]{%
  \markright{\thepart.\thesection.\ #1}}

\fancypagestyle{bookstyle}{%
  \fancyhf{}%
  \fancyhead[LE]{\leftmark}%
  \fancyhead[RO]{\rightmark}%
  \fancyfoot[RO,LE]{\thepage}%
}
\fancypagestyle{plain}{%
  \renewcommand\headrulewidth{0pt}
  \fancyhf{}%
}
\setlength{\headheight}{14.5pt}
\pagestyle{bookstyle}

\begin{document}
\part{Part 1}
\chapter{Chap 1}
\label{chap:chapI1}
\section{Sec 1}
\lipsum[1-10]
\chapter{Chap 2}
\part{Part 2}
\chapter{Chap 1}
\chapter{Chap 2}
\end{document}

enter image description here

2
  • Thanks for the help. It works... almost... I forgot to mention that I'd like to have the header in uppercase. Where do I have to write the command ? Other things (don't know if related or not) : the header decorative line disappear with the above preamble and a modification of margins. Do you know why ?
    – Basil1402
    Apr 25, 2015 at 12:02
  • @Basil1402 Use \MakeUppercase{#1} in \chaptermark and \sectionmark. Changes to the pagination parameters should be given before \pagestyle{fancy}.
    – egreg
    Apr 25, 2015 at 12:32

You must log in to answer this question.

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