4

I want to have a custom header like the following: Chapitre5 (Chapter5) in the left and the name of the chapter in the right(Mise en oeuvre) enter image description here

I used this in my root .tex file:

\usepackage{fancyhdr}

\pagestyle{fancy}

%clears header & footer
\fancyhf{}
\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\rhead{\leftmark}
\lhead{
    \ifnum\value{chapter}>0
        \chaptername \thechapter
    \fi
}

But it doesn't take into account unnumbered chapters after the first chapter, i.e \thechapter>0. So when i add a chapter with \chapter*{annexe} right after chapter5, i get the same header as in the picture and i expect it to be empty in the left and have the chapter name annexe in the right side of the header. Can you, please, tell me how to do it? explaining what the code does would be very nice. As i am actually new to Latex.

5
  • I am not sure you need to redefine anything. What is mise en oevre? a chapter heading? Can you make the example compilable, so that it is easier to see where what is coming from?
    – Johannes_B
    Commented Jun 18, 2015 at 13:02
  • @Johannes_B mise en oeuvre is actually the name of the chapter. Commented Jun 18, 2015 at 13:14
  • I forgot to give you a link, in there you can find which details we need to know. I am still a bit clueless on what you really want.
    – Johannes_B
    Commented Jun 18, 2015 at 13:18
  • i want to have a header like the one in the picture : in the left side Chapter X where X is the number of the chapter and the name of the chapter in the right side Commented Jun 18, 2015 at 13:25
  • The rest of the question is an attempt that didn't work as expected Commented Jun 18, 2015 at 13:26

1 Answer 1

2

Use the left field with \hfill in between the two parts.

\documentclass[a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}

\usepackage{fancyhdr}

\usepackage{kantlipsum} % mock text

\pagestyle{fancy}

%clears header & footer
\fancyhf{}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\fancyhead[L]{\chaptername\ \thechapter\hfill\leftmark}

\begin{document}

\chapter{Mise en oeuvre}

\kant[1-10]

\end{document}

enter image description here

For managing unnumbered chapters, you can patch the \chapter command and add the “Chapter n” bit only if a condition is satisfied.

\documentclass[a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}

\usepackage{fancyhdr}
\usepackage{etoolbox}

\usepackage{kantlipsum} % mock text

\pagestyle{fancy}

%clears header & footer
\fancyhf{}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\fancyhead[L]{%
  \insertchaptername\hfill\leftmark
}
\newcommand{\insertchaptername}{%
  \ifstarredchapter
  \else
    \ifnum\value{chapter}>0
      \chaptername\ \thechapter
    \fi
  \fi
}
\newif\ifstarredchapter
\makeatletter
\pretocmd\chapter{\global\starredchapterfalse}{}{}
\pretocmd\@schapter{\global\starredchaptertrue\chaptermark{#1}}{}{}
\let\@mkboth\@gobbletwo % we do the \chaptermark
\makeatother

\begin{document}

\chapter*{Introduction}

\kant[1-10]

\chapter{Mise en oeuvre}

\kant[11-20]

\chapter*{Annexe}

\kant[21-30]

\end{document}

enter image description here

5
  • if you try it with an un-numbered chapter \chapter*{conclusion} after this chapter, it would have the old header Commented Jun 18, 2015 at 13:31
  • And if you add it before it would have Chapter 0 ! Commented Jun 18, 2015 at 13:31
  • 1
    @Lelouch It's quite easy to manage it. Stay tuned.
    – egreg
    Commented Jun 18, 2015 at 13:42
  • That worked for un-named chapters in the end. But i couldn't get rid of Chapter 0 in list of figures. Any clues? Commented Jun 18, 2015 at 15:10
  • @Lelouch Fixed.
    – egreg
    Commented Jun 18, 2015 at 15:36

You must log in to answer this question.

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