3

I want to get the following header:

Left page: \thepage\hfill\chaptername{}\thechapter

Right page: \chaptername\hfill\thepage

So I write:

\documentclass{book}

\usepackage{fontspec} % enagles loading of OpenType fonts
\usepackage{polyglossia} % support for languages

% fonts:
\defaultfontfeatures{Scale=MatchLowercase,Mapping=tex-text} % without this XeLaTeX won't turn "--" into dashes
\setmainfont{Arial}
\setsansfont{Arial}
\setromanfont{Arial}
\setmonofont{DejaVu Sans Mono}

\usepackage{fancyhdr}

\fancyhf{}
\fancyhead[LO]{\thechapter}
\fancyhead[RE]{\chaptername}
\fancyhead[LE,RO]{\thepage}
\pagestyle{fancy}

\begin{document}

\chapter{foo}

Foo!

\chapter{bar}

Bar!

\chapter{baz}

Baz!

\end{document}

but it doesn't print chapter name in header. What do I do wrong?

Edit:

Current best bet:

\documentclass{book}

\usepackage{fontspec} % enagles loading of OpenType fonts
\usepackage{polyglossia} % support for languages

% fonts:
\defaultfontfeatures{Scale=MatchLowercase,Mapping=tex-text} % without this XeLaTeX won't turn "--" into dashes
\setmainfont{Arial}
\setsansfont{Arial}
\setromanfont{Arial}
\setmonofont{DejaVu Sans Mono}

\usepackage{fancyhdr}

\renewcommand{\rightmark}[1]{\markboth{#1}{}}
\renewcommand{\leftmark}[1]{\chaptername{}\thechapter}
\fancyhf{}
\fancyhead[LO]{\leftmark}
\fancyhead[RE]{\rightmark}
\fancyhead[LE,RO]{\thepage}
\pagestyle{fancy}

\begin{document}

\chapter{foo}

Foo!

\chapter{bar}

Bar!

\chapter{baz}

Baz!

\end{document}
10
  • \chaptername doesn't refer to the chapter title, it produces the word "Chapter" (possibly translated). You probably want \leftmark, instead.
    – egreg
    Commented Feb 18, 2013 at 17:51
  • I guess it should be \fancyhead[RE]{\thechapter} and \fancyhead[LO]{\chaptername}, no ?
    – Corentin
    Commented Feb 18, 2013 at 17:52
  • Nope. Only \thepage. I know there are subtleties like \chaptermark and \rightmark` -- but I though it should be working this crude way also.
    – Adobe
    Commented Feb 18, 2013 at 17:53
  • 1
    @egreg: \renewcommand{\chaptermark}[1]{\markboth{#1}{}} with \fancyhead[RE]{\chaptermark} doesn't work either.
    – Adobe
    Commented Feb 18, 2013 at 17:55
  • @Corentin: I'll fix it.
    – Adobe
    Commented Feb 18, 2013 at 17:56

1 Answer 1

5

This does what you specify:

\documentclass{book}

\usepackage{fontspec} % enables loading of OpenType fonts
\usepackage{polyglossia} % support for languages
\usepackage{kantlipsum} % for mock text in the example

% fonts:
\defaultfontfeatures{Scale=MatchLowercase,Ligatures=TeX} % without this XeLaTeX won't turn "--" into dashes
\setmainfont{Arial}
\setsansfont{Arial}
\setmonofont{DejaVu Sans Mono}

\usepackage{fancyhdr}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\fancyhf{}
\fancyhead[RE]{\chaptername~\thechapter}
\fancyhead[LO]{\leftmark}
\fancyhead[LE,RO]{\thepage}

\begin{document}

\chapter{foo}

\kant

\chapter{bar}

\kant

\end{document}

Note that \pagestyle{fancy} must go before the redefinitions of the headers and of \chaptermark. The current chapter title is available as \leftmark which mustn't be redefined.

3
  • Cool. I find that sometimes that order thing is a little bit unclear. How do I know that fancyhf{} redefines chaptermark for example.
    – Adobe
    Commented Feb 18, 2013 at 18:17
  • @Adobe There's plenty of examples in the documentation of fancyhdr. It's \pagestyle{fancy} that does the redefinition, by the way. Last, I repeat: don't use \setromanfont.
    – egreg
    Commented Feb 18, 2013 at 18:21
  • Ok: \setdefaultlanguage is deprecated in favour of \setmainlanguage, and \setromanfont is deprecated in favour of \setmainfont.
    – Adobe
    Commented Feb 18, 2013 at 18:36

You must log in to answer this question.

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