Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
\chaptername doesn't refer to the chapter title, it produces the word "Chapter" (possibly translated). You probably want \leftmark, instead. –  egreg Feb 18 '13 at 17:51
    
I guess it should be \fancyhead[RE]{\thechapter} and \fancyhead[LO]{\chaptername}, no ? –  Corentin Feb 18 '13 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 Feb 18 '13 at 17:53
1  
@egreg: \renewcommand{\chaptermark}[1]{\markboth{#1}{}} with \fancyhead[RE]{\chaptermark} doesn't work either. –  Adobe Feb 18 '13 at 17:55
    
@Corentin: I'll fix it. –  Adobe Feb 18 '13 at 17:56

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Cool. I find that sometimes that order thing is a little bit unclear. How do I know that fancyhf{} redefines chaptermark for example. –  Adobe Feb 18 '13 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 Feb 18 '13 at 18:21
    
Ok: \setdefaultlanguage is deprecated in favour of \setmainlanguage, and \setromanfont is deprecated in favour of \setmainfont. –  Adobe Feb 18 '13 at 18:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.