titlesec & fancyhdr - 章节名称不会打印在页眉处

titlesec & fancyhdr - 章节名称不会打印在页眉处

在使用 titlsec 和 fancyhdr 包的文档中,我无法在页眉中心打印章节名称。

\documentclass[a5paper,openany,14pt,]{extbook}

\usepackage{titlesec}
\titleformat{\chapter}[hang]
  {\filleft\bfseries\Large}
  {\Huge\thechapter}
  {1ex}
  {\titlerule[2pt]\vspace{3pt}\titlerule\vspace{0ex}\filright}
  [\vspace{0ex}]

\usepackage[left=2.34cm, right=2.34cm, top=2.66cm]{geometry}

\usepackage[twoside]{fancyhdr} 

\renewcommand{\chaptermark}[1]{%
  \markboth{#1}{}}
 
\fancypagestyle{plain}{%
  \renewcommand{\headrulewidth}{0pt}
  \fancyhead[RO]{\thepage}
  \fancyhead[C]{\leftmark} % rightmark,leftmark thechapter don't work
\fancyhead[LE]{\thepage}
}
\pagestyle{plain}

\begin{document}
\chapter*{Introduction}
Alright, here we are...

\pagebreak

Alright, one mone page...
\end{document}

在此处输入图片描述

如何打印章节名称,即介绍

答案1

此代码使用了explicit的参数,titlesec以便能够使用格式设置来\chapter添加必要的内容leftmark,使用章节标题。

标准的每个章节的第一页都使用一种plain格式,后面的章节也使用同样的fancy格式。

X

\documentclass[a5paper,openany,14pt,]{extbook}

\usepackage[left=2.34cm, right=2.34cm, top=2.66cm]{geometry}

\usepackage[explicit]{titlesec}% changed <<<<

\titleformat{\chapter}[hang] % changed numbered chapters
{\titlerule[2pt]\vspace{3pt}\titlerule\bfseries\Large\filleft}
{\Huge\thechapter}
{1ex}
{#1\filright \markboth{#1}{}} % add the leftmark <<<

\usepackage{fancyhdr} 

\fancypagestyle{plain}{% used in the chapter first page
    \fancyhf{}
    \fancyhead[C]{} % OPTIONAL \leftmark works here <<<<
    \fancyhead[LE,RO]{\thepage}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
}

\fancypagestyle{fancy}{%
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[C]{\leftmark}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}

\pagestyle{fancy}

\begin{document}

    \chapter*{Introduction 0}
    Alright, here we are...
    
    \pagebreak
    
    Alright, one more page...
    
    \chapter{Introduction I}
    Alright, here we are...
    
    \pagebreak
    
    Alright, one more page...
\end{document}

选项下一个示例将在编号章节的情况下将章节编号添加到页眉。

为此,定义了两种标题格式:第一种用于编号章节,第二种用于未编号章节。

是

\documentclass[a5paper,openany,14pt,]{extbook}

\usepackage[left=2.34cm, right=2.34cm, top=2.66cm]{geometry}

\usepackage[explicit]{titlesec}% changed <<<<

\titleformat{\chapter}[hang] % changed numbered chapters
{\titlerule[2pt]\vspace{3pt}\titlerule\bfseries\Large\filleft}
{\Huge\thechapter}
{1ex}
{#1\filright \markboth{\thechapter\enspace#1}{}} % add the leftmark with the chapter number included<<<

\titleformat{name=\chapter, numberless}[hang]% added  unnumbered chapters <<<<<<<<<
{\titlerule[2pt]\vspace{3pt}\titlerule\bfseries\Large\filleft}
{}
{1ex}
{#1\filright\markboth{#1}{}} % add the leftmark using the chapter title

\usepackage{fancyhdr} 

\fancypagestyle{plain}{% used in the chapter first page
    \fancyhf{}
    \fancyhead[C]{} % OPTIONAL \leftmark works here <<<<
    \fancyhead[LE,RO]{\thepage}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
}

\fancypagestyle{fancy}{%
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[C]{\leftmark}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
}

\pagestyle{fancy}

\begin{document}

    \chapter*{Introduction 0}
    Alright, here we are...
    
    \pagebreak
    
    Alright, one more page...
    
    \chapter{Introduction I}
    Alright, here we are...
    
    \pagebreak
    
    Alright, one more page...
\end{document}

答案2

这对我有用:

\documentclass[a5paper,openany,14pt,]{extbook}

\usepackage{titlesec}
\titleformat{\chapter}[hang]
  {\filleft\bfseries\Large}
  {\Huge\thechapter}
  {1ex}
  {\titlerule[2pt]\vspace{3pt}\titlerule\vspace{0ex}\filright}
  [\vspace{0ex}]

\usepackage[left=2.34cm, right=2.34cm, top=2.66cm]{geometry}

\usepackage[twoside]{fancyhdr} 

\renewcommand{\chaptermark}[1]{%
  \markboth{#1}{}}
 
\fancypagestyle{plain}{%
  \renewcommand{\headrulewidth}{0pt}
  \fancyhead{}
  \fancyhead[RO]{\thepage}
  \fancyhead[C]{\leftmark} % rightmark,leftmark thechapter don't work
  \fancyhead[LE]{\thepage}
}
\pagestyle{plain}

\begin{document}
\chapter*{Introduction}
\chaptermark{Introduction}
Alright, here we are...

\pagebreak

Alright, one mone page...
\end{document}

相关内容