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}

在此輸入影像描述

相關內容