标题中没有带有 fancyhdr 的章节 #num

标题中没有带有 fancyhdr 的章节 #num

我正在使用fancyhdr,我想让标题的章节名称与节名相对,但不带编号。我尝试了很多配置,但在本例中都不起作用。

如何修复?

附言:本短文档的章节页样式有所不同,但在实际文档中已修复。

\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[italian]{babel}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{sectsty}

\usepackage{geometry}
\geometry{
    left=35mm,
    right=30mm,
    top=27mm,
    bottom=30mm
}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\fancyhead[LE,RO]{\rightmark}
\fancyhead[LO,RE]{\chaptermark}

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}

\fancyfoot[LE,RO]{\thepage} % Left side on Even pages; Right side on Odd pages

\begin{document}
\chapter{Chapter Intro}
\lipsum
\chapter{Chapter with subsections}
\section{section foo}
\lipsum
\subsection{subsection bar}
\lipsum
\end{document}

答案1

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}

每个章节都将左标记设置为章节名称。因此,您必须使用

\fancyhead[LO,RE]{\leftmark}

以获得期望的结果。

在此处输入图片描述

代码:

\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}% only for dummy text

\usepackage{geometry}
\geometry{
    left=35mm,
    right=30mm,
    top=27mm,
    bottom=30mm,
    headheight=13.6pt% <- added as suggest by fancyhdr
}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\fancyhead[LE,RO]{\rightmark}
\fancyhead[LO,RE]{\leftmark}% <- changed

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}

\fancyfoot[LE,RO]{\thepage}

\begin{document}
\chapter{Chapter Intro}
\lipsum
\chapter{Chapter with subsections}
\section{section foo}
\lipsum
\subsection{subsection bar}
\lipsum
\end{document}

如果有些带星号的章节也应该有标题条目,请\markboth手动使用。例如:

\chapter*{Introduction}\markboth{Introduction}{}

当然,您可以定义一个宏来执行此操作。

答案2

最后编辑答案

有了这个答案,您可以使用 \MaxLevel 变量选择 fancyhead 中出现的分段深度。(例如,如果您使用数字 2,则只有部分会显示在 fancy header 上)

此外,您还可以选择使用带有可选参数的星号部分来从章节或部分等的名称中使用不同的花式头部内容(这是普通乳胶中不允许的)。

一个可能不需要的额外功能是,标签会自动添加到每个章节、部分或小节中,如 {c:1} 表示第一章(真正的第一章),{s:2} 表示第二节,{sss:3} 表示第三小节,等等。

\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{sectsty}
\usepackage{bophook}
\usepackage{refcount}

\usepackage{geometry}
\geometry{
    left=35mm,
    right=30mm,
    top=27mm,
    bottom=30mm
}

\usepackage{fancyhdr}

\newcounter{realchapter}
\newcounter{realsection}
\newcounter{realsubsection}
\newcounter{realsubsubsection}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\newcount\Level
\def\MaxLevel{3} % Change this to show only desired depth on fancy header




\def\PrevSec{}

\makeatletter
\let\oldchapter=\chapter
\def\chapter{%
\xdef\ind{c:0}
\stepcounter{realchapter}
\xdef\PrevSec{\curfmark}
\global\Level=1%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\xdef\ind{c:\therealchapter}
\oldchapter*{#2}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\def\@StarredWithout#1{%
\xdef\ind{c:\therealchapter}
\oldchapter*{#1}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\xdef\ind{c:\therealchapter}
\oldchapter[#1]{#2}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}
\def\@nonStarredWithout#1{%
\xdef\ind{c:\therealchapter}
\oldchapter{#1}\index{\ind}\label{\ind}\def\Chaptername{#1}%
}

\let\oldsection=\section
\def\section{%
\stepcounter{realsection}
\xdef\PrevSec{\curfmark}
\global\Level=2%
\@ifstar{\@StarredS}{\@nonStarredS}%
}
\def\@StarredS{%
\@ifnextchar[%
{\@StarredWithS}%
{\@StarredWithoutS}%
}      
\def\@StarredWithS[#1]#2{%
\xdef\ind{s:\therealsection}
\oldsection*{#2}\index{\ind}\label{\ind}\def\Sectionname{#1}% Here you can use starred chapter, section etc with optional argument for chaptermark or sectionmark etc
}
\def\@StarredWithoutS#1{%
\xdef\ind{s:\therealsection}
\oldsection*{#1}\index{\ind}\label{\ind}\def\Sectionname{#1}%
}
\def\@nonStarredS{%
\@ifnextchar[%
{\@nonStarredWithS}%
{\@nonStarredWithoutS}%
}
\def\@nonStarredWithS[#1]#2{%
\xdef\ind{s:\therealsection}
\oldsection[#1]{#2}\index{\ind}\label{\ind}\def\Sectionname{#2}%
}
\def\@nonStarredWithoutS#1{%
\xdef\ind{s:\therealsection}
\oldsection{#1}\index{\ind}\label{\ind}\def\Sectionname{#1}%
}

\let\oldsubsection=\subsection
\def\subsection{%
\stepcounter{realsubsection}
\xdef\PrevSec{\curfmark}
\global\Level=3%
\@ifstar{\@StarredSS}{\@nonStarredSS}%
}
\def\@StarredSS{%
\@ifnextchar[%
{\@StarredWithSS}%
{\@StarredWithoutSS}%
}      
\def\@StarredWithSS[#1]#2{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection*{#2}\index{\ind}\label{\ind}\def\Subsectionname{#1}% Here you can use starred chapter, section etc with optional argument for chaptermark or sectionmark etc
}
\def\@StarredWithoutSS#1{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection*{#1}\index{\ind}\label{\ind}\def\Subsectionname{#1}%
}
\def\@nonStarredSS{%
\@ifnextchar[%
{\@nonStarredWithSS}%
{\@nonStarredWithoutSS}%
}
\def\@nonStarredWithSS[#1]#2{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection[#1]{#2}\index{\ind}\label{\ind}\def\Subsectionname{#1}%
}
\def\@nonStarredWithoutSS#1{%
\xdef\ind{ss:\therealsubsection}
\oldsubsection{#1}\index{\ind}\label{\ind}\def\Subsectionname{#1}%
}

\let\oldsubsubsection=\subsubsection
\def\subsubsection{%
\stepcounter{realsubsubsection}
\xdef\PrevSec{\curfmark}
\global\Level=4%
\@ifstar{\@StarredSS}{\@nonStarredSS}%
}
\def\@StarredSSS{%
\@ifnextchar[%
{\@StarredWithSSS}%
{\@StarredWithoutSSS}%
}      
\def\@StarredWithSSS[#1]#2{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection*{#2}\index{\ind}\label{\ind}\def\Subsubectionname{#1}% Here you can use starred chapter, section etc with optional argument for chaptermark or sectionmark etc
}
\def\@StarredWithoutSSS#1{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection*{#1}\index{\ind}\label{\ind}\def\Subsubsectionname{#1}%
}
\def\@nonStarredSSS{%
\@ifnextchar[%
{\@nonStarredWithSSS}%
{\@nonStarredWithoutSSS}%
}
\def\@nonStarredWithSSS[#1]#2{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection[#1]{#2}\index{\ind}\label{\ind}\def\Subsubsectionname{#2}%
}
\def\@nonStarredWithoutSSS#1{%
\xdef\ind{sss:\therealsubsubsection}
\oldsubsubsection{#1}\index{\ind}\label{\ind}\def\Subsubsectionname{#1}%
}
\makeatother




\def\mysecdepth#1{\ifcase#1  
Part
\or \empty%
\or Sectionname%
\or Subsectionname%
\or Subsubsectionname%
\fi}


\fancyhead[LE,RO]{\fmark}
\fancyhead[LO,RE]{\smark}

\def\curfmark{\ifnum\thepage<\getpagerefnumber{\ind}\PrevSec\else\ifnum\Level>\MaxLevel\csname\mysecdepth{\MaxLevel}\endcsname\else\csname\mysecdepth{\the\Level}\endcsname\fi\fi}



\newcommand{\fmark}{\curfmark}
\newcommand{\smark}{\Chaptername}

\fancyfoot[LE,RO]{\thepage} % Left side on Even pages; Right side on Odd pages

\begin{document}

\chapter{Chapter Intro}
\lipsum
\lipsum

\chapter*{Chapter 1 with subsections}
\section{section foo 1}
\lipsum[2-13]
\subsection{subsection bar 1}
\lipsum

\chapter{Chapter 2 with subsections}
\section{section foo 2}
\lipsum[2-13]
\subsection{subsection bar 2}
\lipsum

\chapter*[Different title in fancyheader]{Chapter 3 with subsections}
\section{section foo 3}
\lipsum
\subsection{subsection bar 3}
\lipsum

the last sebsection was \ref{ss:3} with starting on page \pageref{ss:3}

\end{document}

(用过的检测是否在 \section 内)以及我的答案

旧答案 测试一下看看是否正常

\documentclass[11pt,a4paper,twoside,titlepage,openany]{book}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{amsfonts}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{lmodern}
\usepackage{sectsty}

\usepackage{geometry}
\geometry{
    left=35mm,
    right=30mm,
    top=27mm,
    bottom=30mm
}

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhf{}
\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\fancyhead[LE,RO]{\fmark}
\fancyhead[LO,RE]{\smark}

\let\Chaptermark\chaptermark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}

\newcommand{\fmark}{\rightmark}
\newcommand{\smark}{\Chaptername}

\fancyfoot[LE,RO]{\thepage} % Left side on Even pages; Right side on Odd pages

\begin{document}

\chapter{Chapter Intro}
\lipsum
\lipsum

\chapter{Chapter with subsections}
\section{section foo}
\lipsum
\subsection{subsection bar}
\lipsum
\end{document}

这里使用的答案:如何获取当前章节名称、节名称、小节名称等?

相关内容