如何让页眉显示章节名称?

如何让页眉显示章节名称?

我有以下 WME:

\documentclass[twoside]{book}

\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,headheight=28pt,bindingoffset=6mm]{geometry}
\usepackage[pagestyles]{titlesec}
\usepackage{lipsum}
\usepackage{fancyhdr}


\titleformat{\chapter}[display]
  {\normalfont\bfseries}{}{10pt}{\Huge\thechapter.\quad}


\fancypagestyle{mypagestyle}{%
  \fancyhf{}% Clear header/footer
  \fancyhead[OC]{\thechapter.\quad\chaptertitle}% Author on Odd page, Centred
  \fancyhead[EC]{\thechapter.\quad\chaptertitle}% Title on Even page, Centred
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{.4pt}% Header rule of .4pt
}
\pagestyle{mypagestyle}

\begin{document}

\pagestyle{mypagestyle}
\chapter{Introduction}
\lipsum[1-40]

\end{document}

使用此 WME,标题仅显示章节号。如何让标题显示章节名称?

答案1

您可以使用 来执行此操作\leftmark

左标记

\documentclass[twoside]{book}

\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,headheight=28pt,bindingoffset=6mm]{geometry}
\usepackage[pagestyles]{titlesec}
\usepackage{lipsum}
\usepackage{fancyhdr}


\titleformat{\chapter}[display]
  {\normalfont\bfseries}{}{10pt}{\Huge\thechapter.\quad}


\fancypagestyle{mypagestyle}{%
  \fancyhf{}% Clear header/footer
  \fancyhead[OC]{\leftmark}% Author on Odd page, Centred
  \fancyhead[EC]{\leftmark}% Title on Even page, Centred
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{.4pt}% Header rule of .4pt
}
\pagestyle{mypagestyle}

\begin{document}

\pagestyle{mypagestyle}
\chapter{Introduction}
\lipsum[1-40]

\end{document}

以下是没有“CHAPTER”的版本:

没有

\documentclass[twoside]{book}

\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,headheight=28pt,bindingoffset=6mm]{geometry}
\usepackage[pagestyles]{titlesec}
\usepackage{lipsum}
\usepackage{fancyhdr}


\titleformat{\chapter}[display]
  {\normalfont\bfseries}{}{10pt}{\Huge\thechapter.\quad}

\usepackage{expl3,xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \tl_set:Nn {Nx}
\NewDocumentCommand{\replace}{mmm}
 {
  \pintodoido_replace:nnn {#1} {#2} {#3}
 }

\tl_new:N \l_pintodoido_input_text_tl
\tl_new:N \l_pintodoido_search_tl
\tl_new:N \l_pintodoido_replace_tl

\cs_new_protected:Npn \pintodoido_replace:nnn #1 #2 #3
 {
  \tl_set:Nf \l_pintodoido_input_text_tl { #1 }
  \tl_set:Nn \l_pintodoido_search_tl { #2 }
  \tl_set:Nn \l_pintodoido_replace_tl { #3 }
  \regex_replace_all:nnN { \b\u{l_pintodoido_search_tl}\b } { \u{l_pintodoido_replace_tl} } \l_pintodoido_input_text_tl
  \tl_use:N \l_pintodoido_input_text_tl
 }
\ExplSyntaxOff

\fancypagestyle{mypagestyle}{%
  \fancyhf{}% Clear header/footer
  \fancyhead[OC]{\replace{\leftmark}{Chapter}{}}% Author on Odd page, Centred
  \fancyhead[EC]{\replace{\leftmark}{Chapter}{}}% Title on Even page, Centred
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{.4pt}% Header rule of .4pt
}
\pagestyle{mypagestyle}

\begin{document}

\pagestyle{mypagestyle}
\chapter{Introduction}
\lipsum[1-40]

\end{document}

答案2

您可以使用

\AtBeginDocument{%
    \renewcommand*\chaptermark[1]{\markboth{\thechapter.\quad#1}{}}%
}

从标题条目中删除章节名称。

\documentclass[twoside]{book}
\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,headheight=28pt,bindingoffset=6mm]{geometry}
\usepackage{titlesec}
\titleformat{\chapter}[display]
  {\normalfont\bfseries}{}{10pt}{\Huge\thechapter.\quad}

\usepackage{fancyhdr}
\AtBeginDocument{%
    \renewcommand*\chaptermark[1]{\markboth{\thechapter.\quad#1}{}}%
}
\fancypagestyle{mypagestyle}{%
  \fancyhf{}% Clear header/footer
  \fancyhead[C]{\leftmark}%
  \fancyfoot[C]{\thepage}%
  \renewcommand{\headrulewidth}{.4pt}% Header rule of .4pt
}
\pagestyle{mypagestyle}

\usepackage{lipsum}% for dummy text
\begin{document}
\chapter{Introduction}
\lipsum[1-40]
\end{document}

在此处输入图片描述

或者可能不需要额外的页面样式,您可以使用以下fancy样式:

\documentclass[twoside]{book}

\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,headheight=28pt,bindingoffset=6mm]{geometry}
\usepackage{titlesec}
\titleformat{\chapter}[display]
  {\normalfont\bfseries}{}{10pt}{\Huge\thechapter.\quad}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand*\chaptermark[1]{\markboth{\thechapter.\quad#1}{}}
\fancyhf{}% Clear header/footer
\fancyhead[C]{\leftmark}%
\fancyfoot[C]{\thepage}%

\usepackage{lipsum}% for dummy text
\begin{document}
\chapter{Introduction}
\lipsum[1-40]
\end{document}

结果和上面一样。

相关内容