[Tex/LaTex] Fancyhdr and redefinition of leftmark/rightmark

fancyhdrheader-footer

I am writing a long document and I decided to change the headers, using fancyhdr :

\documentclass[12pt,a4paper]{book}

\renewcommand*\thesection{\arabic{section}}

\usepackage{fancyhdr}
% Fancyhdr setup
\fancypagestyle{main}{
\fancyhead{} % clear all header fields
\fancyhead[LE]{\footnotesize{\scshape\nouppercase{Chapitre~\thechapter ~-    ~\leftmark}}}
\fancyhead[RO]{\footnotesize{\scshape\nouppercase{\thesection~-~\rightmark}}}
\fancyfoot{} % clear all footer fields
\fancyfoot[LE,RO]{\thepage}
\renewcommand{\headrulewidth}{0 pt}
\renewcommand{\footrulewidth}{0 pt}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
}
\begin{document}
\end{document}

The problem is this code does not compile, and the problem is whit the two lines :

\renewcommand{\sectionmark}[1]{\markright{#1}}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}

To solve the problem I just put them outside the definition of main headers, but it doesn't give the same result.

How should I fix this error without changing my headers ?

PS. I want the headers to be on right pages like "3 – Section title" and on the left pages (except at a begining of a chapter) like "Chapitre 1 – Title of chapter"

Thanks.

Best Answer

Redefine \chaptermark and \sectionmark outside the page style definition after using \pagstyle{fancy}.

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markright{\thesection~- ~#1}}
\renewcommand{\chaptermark}[1]{\markboth{\chaptername~\thechapter~-~ #1}{}}

enter image description here

Code:

\documentclass[12pt,a4paper,french]{book}
\renewcommand*\thesection{\arabic{section}}
\usepackage[T1]{fontenc}
\usepackage{babel}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markright{\thesection~- ~#1}}
\renewcommand{\chaptermark}[1]{\markboth{\chaptername~\thechapter~-~ #1}{}}

% Fancyhdr setup
\fancypagestyle{main}{
\fancyhf{} % clear all header fields
\fancyhead[LE]{\footnotesize\scshape\leftmark}
\fancyhead[RO]{\footnotesize\scshape\rightmark}
\fancyfoot[LE,RO]{\thepage}
\renewcommand{\headrulewidth}{0 pt}
\renewcommand{\footrulewidth}{0 pt}
}

\pagestyle{main}

\usepackage{blindtext}% dummy text
\begin{document}
\blinddocument
\end{document}
\documentclass[extrafontsizes]{memoir}
\InputIfFileExists{scrsize10pt.clo}{}
%\usepackage[fontsize=13pt]{scrextend}
\usepackage{garamondx}
\begin{document}
test
\end{document}