![如何获取当前章节名称、节名称、小节名称等?](https://linux22.com/image/278653/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E7%AB%A0%E8%8A%82%E5%90%8D%E7%A7%B0%E3%80%81%E8%8A%82%E5%90%8D%E7%A7%B0%E3%80%81%E5%B0%8F%E8%8A%82%E5%90%8D%E7%A7%B0%E7%AD%89%EF%BC%9F.png)
我碰到这个答案声称它甚至可以与带星号的\chapter
et al 版本一起使用。以下示例显示它不工作,因为未调用宏。有没有办法“修复”这个问题,并且即使有 * 类型调用也\...mark
能设置宏的相应值?\...name
\documentclass{book}
\let\Chaptermark\chaptermark
\let\Sectionmark\sectionmark
\let\Subsectionmark\subsectionmark
\let\Subsubsectionmark\subsubsectionmark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}
\def\sectionmark#1{\def\Sectionname{#1}\Sectionmark{#1}}
\def\subsectionmark#1{\def\Subsectionname{#1}\Subsectionmark{#1}}
\def\subsubsectionmark#1{\def\Subsubsectionname{#1}\Subsubsectionmark{#1}}
\begin{document}
\chapter{First chapter}
Title: ``\Chaptername''.
\section{First section}
Title: ``\Sectionname''.
\subsection{First subsection}
Title: ``\Subsectionname''.
\subsubsection{First subsubsection}
Title: ``\Subsubsectionname''.
\chapter*{Second chapter}
Title: ``\Chaptername''.
\section*{Second section}
Title: ``\Sectionname''.
\subsection*{Second subsection}
Title: ``\Subsectionname''.
\subsubsection*{Second subsubsection}
Title: ``\Subsubsectionname''.
\end{document}
假设我有一个包含许多\section
(等等) 调用的长文件。但我想更改一个宏以包含实际section
(等等) 的标题。
答案1
给定的宏不适用于 -versions *
,因为\...mark
只有不存在时才会发出命令*
。例如,\chapter{Title}
发出\chaptermark{Title}
,但\chapter*{Title}
不发出 。
这是一组补丁,将提供命令
\chaptertitle
\sectiontitle
\subsectiontitle
\subsubsectiontitle
它将始终扩展至相应部分单元的最新标题。
\documentclass{book}
\usepackage{etoolbox}
% Patch the sectioning commands to provide a hook to be used later
\preto{\chapter}{\def\leveltitle{\chaptertitle}}
\preto{\section}{\def\leveltitle{\sectiontitle}}
\preto{\subsection}{\def\leveltitle{\subsectiontitle}}
\preto{\subsubsection}{\def\leveltitle{\subsubsectiontitle}}
\makeatletter
% \@sect is called with normal sectioning commands
% Argument #8 to \@sect is the title
% Thus \section{Title} will do \gdef\sectiontitle{Title}
\pretocmd{\@sect}
{\expandafter\gdef\leveltitle{#8}}
{}{}
% \@ssect is called with *-sectioning commands
% Argument #5 to \@ssect is the title
\pretocmd{\@ssect}
{\expandafter\gdef\leveltitle{#5}}
{}{}
% \@chapter is called by \chapter (without *)
% Argument #2 to \@chapter is the title
\pretocmd{\@chapter}
{\expandafter\gdef\leveltitle{#2}}
{}{}
% \@schapter is called with \chapter*
% Argument #1 to \@schapter is the title
\pretocmd{\@schapter}
{\expandafter\gdef\leveltitle{#1}}
{}{}
\makeatother
\newcommand\test{%
\noindent
The chapter title is \chaptertitle\\
The section title is \sectiontitle\\
The subsection title is \subsectiontitle\\
The subsubsection title is \subsubsectiontitle
}
\begin{document}
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\test
\chapter*{Second chapter}
\section*{Second section}
\subsection*{Second subsection}
\subsubsection*{Second subsubsection}
\test
\end{document}
这里\test
只是为了显示在发出命令时控制序列保持正确的值。
补丁必须在加载之前进行hyperref
(如果您使用它)。