Fancyhdr - 没有“章节”字样,但有部分编号

Fancyhdr - 没有“章节”字样,但有部分编号

我在使用 fancyhdr 包定义标题时遇到了麻烦。我想要的是: - 在左页,在左侧:partnumber.chapternumber。章节名称 - 在右页,在右侧:partnumber.chapternumber.sectionnumber 章节名称

我尝试重新定义章节标记,但没有成功。这是 MWE。

\documentclass[twoside,12pt,openright,a4paper,usenames,dvipsnames]{book}
\usepackage[linktoc=all,hyperindex]{hyperref}
\usepackage{chngcntr}
\counterwithin{chapter}{part}
\usepackage{lipsum}

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\renewcommand{\thechapter}{\arabic{chapter}}
\titleformat{name=\chapter}[display]
{\bfseries\LARGE}
{\filleft\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{0ex}
{%\titlerule
\vspace{2ex}%
\filleft}
[\vspace{4ex}%
\titlerule]

\titleformat{name=\chapter,numberless}
{\bfseries\LARGE}
{}
{0ex}
{%\titlerule
\filleft\MakeUppercase}
[\vspace{4ex}%
\titlerule]
\titlespacing*{\chapter}{0pt}{-25pt}{40pt}

\titleformat{\part}[frame]
  {\bfseries\Huge}
  {\filright\large\enspace{\partname}\enspace}
  {40pt}
  {\Large\filcenter\MakeUppercase}
\titleclass{\part}{top}

\makeatletter
\renewcommand{\thechapter}{\@arabic\c@chapter}
\renewcommand{\p@chapter}{\thepart.}
\makeatother

\renewcommand{\chaptermark}[1]{%
\markboth{\thepart.\thechapter.\ #1}{}}

\renewcommand{\sectionmark}[1]{%
\markboth{\thechapter.\ #1}{}}

\usepackage{fancyhdr}
\fancypagestyle{bookstyle}{
\fancyhf{}
\fancyhead[LE]{\leftmark}
\fancyhead[RO]{\rightmark}

\fancyfoot[RO,LE]{\thepage}}
\fancypagestyle{plain}{
\renewcommand\headrulewidth{0pt}
\fancyhf{}
}

\begin{document}
\pagestyle{bookstyle}
\part{Part 1}
\chapter{Chap 1}
\label{chap:chapI1}
\section{Sec 1}
\lipsum[1-10]
\chapter{Chap 2}
\part{Part 2}
\chapter{Chap 1}
\chapter{Chap 2}
\end{document}

也许与 titlesec 包有冲突,我无法找出。

答案1

您基本上忘记发出\pagestyle{fancy}

但是,您的软件包顺序也是错误的。这是修复后的版本。

\documentclass[twoside,12pt,openright,a4paper]{book}

\usepackage{titlesec}
\usepackage[titles]{tocloft}
\usepackage{fancyhdr}
\usepackage{chngcntr}
\usepackage{lipsum}
\usepackage[linktoc=all,hyperindex]{hyperref}

\counterwithin*{chapter}{part}
\makeatletter
\renewcommand{\p@chapter}{\thepart.}
\makeatother


\titleformat{name=\chapter}[display]
  {\bfseries\LARGE}
  {\filleft\MakeUppercase{\chaptertitlename} \Huge\thechapter}
  {0ex}
  {%\titlerule
   \vspace{2ex}%
   \filleft}
  [\vspace{4ex}%
   \titlerule]

\titleformat{name=\chapter,numberless}
  {\bfseries\LARGE}
  {}
  {0ex}
  {%\titlerule
   \filleft\MakeUppercase}
  [\vspace{4ex}%
   \titlerule]
\titlespacing*{\chapter}{0pt}{-25pt}{40pt}

\titleformat{\part}[frame]
  {\bfseries\Huge}
  {\filright\large\enspace{\partname}\enspace}
  {40pt}
  {\Large\filcenter\MakeUppercase}
\titleclass{\part}{top}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{%
  \markboth{\thepart.\thechapter.\ #1}{}}

\renewcommand{\sectionmark}[1]{%
  \markright{\thepart.\thesection.\ #1}}

\fancypagestyle{bookstyle}{%
  \fancyhf{}%
  \fancyhead[LE]{\leftmark}%
  \fancyhead[RO]{\rightmark}%
  \fancyfoot[RO,LE]{\thepage}%
}
\fancypagestyle{plain}{%
  \renewcommand\headrulewidth{0pt}
  \fancyhf{}%
}
\setlength{\headheight}{14.5pt}
\pagestyle{bookstyle}

\begin{document}
\part{Part 1}
\chapter{Chap 1}
\label{chap:chapI1}
\section{Sec 1}
\lipsum[1-10]
\chapter{Chap 2}
\part{Part 2}
\chapter{Chap 1}
\chapter{Chap 2}
\end{document}

在此处输入图片描述

相关内容