据我理解,我对章节标题的定义如下:
\chapter[title in table of contents][title in header line]{title in body}
之后我添加一个标签,例如
\label{Chapter1}
我知道我可以使用类似的东西\nameref(Chapter1)
来显示相应章节的标题。但是,我如何显示当前的章节...所以我给出命令的章节(例如,如果我想在自定义页脚中显示当前章节的名称)请不要提供fancyhdr
解决方案。谢谢!
答案1
没有任何链接的版本(稍后将编辑)
\documentclass{memoir}
\usepackage{xpatch}
\usepackage{hyperref}
\makeatletter
\gdef\@currentchaptername{}%
\xpatchcmd{\@makechapterhead}{%
\chapterheadstart%
}{%
\gdef\@currentchaptername{#1}%
\chapterheadstart%
}{\typeout{Patch success for @makechapterhead}}{\typeout{Patch failure for @makechapterhead}}
\newcommand{\currentchapref}{%
\@currentchaptername%
}
\makeatother
\begin{document}
\tableofcontents
\chapter[title in table of contents][title in header line]{title in body}\label{Chapter1}
\section{ A foo section}
This is \currentchapref.
\chapter[title in table of contents again][title in header line again]{title in body again}\label{Chapter2}
\end{document}
这是带有链接的版本(如果需要)或者使用带星号的宏版本\currentchapref*
来抑制链接:
\documentclass{memoir}
\usepackage{blindtext}
\usepackage{xpatch}
\usepackage{hyperref}
\makeatletter
\gdef\@currentchaptername{}%
\xpatchcmd{\@makechapterhead}{%
\chapterheadstart%
}{%
\@ifundefined{@currentHref}{%
\gdef\@currentchaptername{{}{#1}}%
}{%
% Store the current anchor for the chapter
\edef\@currentchaptername{{\@currentHref}{#1}}%
}%
\chapterheadstart%
}{\typeout{Patch success for @makechapterhead}}{\typeout{Patch failure for @makechapterhead}}
\NewDocumentCommand{\currentchapref}{sm}{%
\IfBooleanTF{#1}{%
\expandafter\@secondoftwo\@currentchaptername% Use only the second argument
}{%
\expandafter\hyperlink\@currentchaptername%
}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter[title in table of contents][title in header line]{title in body}\label{Chapter1}
\section{ A foo section}
\blindtext[10]
This is \currentchapref.
\chapter[title in table of contents again][title in header line again]{title in body again}\label{Chapter2}
\end{document}