在 fancyhdr 中获取 currenttitle

在 fancyhdr 中获取 currenttitle

获取标题中的节名的通常方式fancyhdr是这样做: \renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}

但是,这不会获取小节、小小节、段落或小段落。我希望标题显示到当前页面为止创建的最后一节的名称,无论它是节还是小段落。

\currenttitle从包装上看这当然似乎是相符的titleref

但不幸的是,这无法在标题中获取 \currenttitle:( \renewcommand{\currenttitle}[1]{\markright{\thesection.\ #1}}是的,我知道\thesection这是错的,但我首先尝试获取当前标题名称,稍后再修复数字)。

有没有什么解决方法可以通过 获取标题中的当前标题(及其编号)fancyhdr

我想修改的示例,以便标题显示页面中的最后一节/小节/子小节/段落/子段落:

\documentclass[11pt,a4paper]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{14pt}
\fancypagestyle{plain}{
   \fancyhead{}
   \renewcommand{\headrulewidth}{0pt}
}
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}

答案1

像这样吗?

多个级别

\documentclass[11pt,a4paper]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\renewcommand{\subsubsectionmark}[1]{\markright{\thesubsubsection\ #1}}
\renewcommand{\paragraphmark}[1]{\markright{\theparagraph\ #1}}
\renewcommand{\subparagraphmark}[1]{\markright{\thesubparagraph\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{14pt}
\fancypagestyle{plain}{%
   \fancyhead{}%
   \renewcommand{\headrulewidth}{0pt}%
}
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\usepackage{kantlipsum}
\begin{document}
  \section{section}
  \kant[1-4]
  \subsection{subsection}
  \kant[5-7]
  \subsubsection{subsubsection}
  \kant[8-9]
  \paragraph{paragraph}
  \kant[10-12]
  \subparagraph{subparagraph}
  \kant[13]
  \section{another section}
  \kant[14-15]
\end{document}

但是,您应该使用geometry来设置页面尺寸:

调整页面布局

\documentclass[11pt,a4paper]{article}
\usepackage{fancyhdr}
\usepackage[headheight=14pt,vcentering]{geometry}
\pagestyle{fancy}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\renewcommand{\subsubsectionmark}[1]{\markright{\thesubsubsection\ #1}}
\renewcommand{\paragraphmark}[1]{\markright{\theparagraph\ #1}}
\renewcommand{\subparagraphmark}[1]{\markright{\thesubparagraph\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\fancypagestyle{plain}{%
   \fancyhead{}%
   \renewcommand{\headrulewidth}{0pt}%
}
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\usepackage{kantlipsum}
\begin{document}
  \section{section}
  \kant[1-4]
  \subsection{subsection}
  \kant[5-7]
  \subsubsection{subsubsection}
  \kant[8-10]
  \paragraph{paragraph}
  \kant[11-14]
  \subparagraph{subparagraph}
  \kant[15]
  \section{another section}
  \kant[16-18]
\end{document}

请注意,警告您在单面文档中fancyhdr使用是无用的。E

相关内容