使用 fancyhdr 时缺少章节名称

使用 fancyhdr 时缺少章节名称

我想要获得以下标题:

左页:\thepage\hfill\chaptername{}\thechapter

右页:\chaptername\hfill\thepage

因此我写道:

\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}

但它没有在页眉中打印章节名称。我做错了什么?

编辑

目前最佳选择:

\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}

答案1

这将执行您指定的操作:

\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}

注意一定\pagestyle{fancy}要去标题和的重新定义\chaptermark。当前章节标题\leftmark可用作一定不能被重新定义。

相关内容