6

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.

2
  • 1
    You shouldn't have the redefinition of \sectionmark and \chaptermark in the code for the page style.
    – egreg
    Commented May 17, 2016 at 12:16
  • @egreg I know but where to put it ? When I put it elsewhere it is not taken into acompt during the compilation. Commented May 17, 2016 at 12:35

1 Answer 1

9

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}
3
  • Well, that does not work in my computer. Everything is in capital letters in the headings and there is a dot, not an hyphon. Commented May 17, 2016 at 13:18
  • 3
    See my changed answer. You have to use \pagestyle{fancy} before redefining \sectionmark and \chaptermark.
    – esdd
    Commented May 17, 2016 at 13:34
  • How would you put the subsection title in the header for this type of document (book) ?
    – Peluche
    Commented Nov 17, 2022 at 0:43

You must log in to answer this question.

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