我想知道是否有一个功能可以通过引用我知道在本章某处定义的标签来给我该章节的名称?
例如:
\chapter{What's it's name?}
\label{chap:whatsItsName}
\section{Party}
\label{sec:party}
\subsection{Going down}
\label{subSec:goingDown}
[...]
\chapter{Second chapter}
[...]
Like said in Section \ref{subSec:goingDown} in the Chapter \chapterNameRef{subSec:goingDown} [...]
其结果可能是这样的:
章节 它的名字是什么?
派对
下降
[...]
第二章
[...]
就像在章节“它的名字是什么?”第 1.1.1 节中所说的那样[...]
我知道 nameref 可以给我该章节的名称,如果我引用该章节的标签,但我想知道是否可以引用小节的标签并获取章节它位于。
答案1
这是一个初步的解决方案——它chapter
仅适用于。
\documentclass{book}
\usepackage{refcount}
\usepackage{xpatch}
\usepackage{xparse}
\xpatchcmd{\@chapter}{\refstepcounter{chapter}}{\refstepcounter{chapter}\label{chapautolabel:\number\value{chapter}}}{}{} % Provide an automated label
\usepackage{nameref}
\ExplSyntaxOn
\seq_new:N \l_sternandy_ref_seq
\NewDocumentCommand{\chapnameref}{O{chapautolabel}m}{%
\tl_set:Nx \l_tmpa_tl {\getrefnumber{#2}}
\seq_set_split:NnV \l_sternandy_ref_seq {.} {\l_tmpa_tl} % Works only if the standard numbering scheme is used
\nameref{#1:\seq_item:Nn \l_sternandy_ref_seq {1}}
}
\ExplSyntaxOff
\begin{document}
\chapter{What's it's name?}
\label{chap:whatsItsName}
\section{Party}
\label{sec:party}
\subsection{Going down}
\label{subSec:goingDown}
\chapter{Second chapter}
Like said in Section \ref{subSec:goingDown} in the Chapter \chapnameref{subSec:goingDown}
\end{document}
答案2
是的,你可以存储此信息以供日后检索zref
:
\documentclass{book}
\usepackage{xparse,zref}
\let\oldlabel\label
\makeatletter
\zref@newprop{chaptertitle}{\chaptertitle}
\renewcommand{\label}[1]{%
\oldlabel{#1}%
\zref@labelbyprops{#1*}{chaptertitle}%
}
\let\oldchapter\chapter
\newcommand{\chaptertitle}{}% This will store the chapter title
\RenewDocumentCommand{\chapter}{s o m}{%
\renewcommand{\chaptertitle}{#3}% Store chapter title
\IfBooleanTF{#1}
{% \chapter*
\oldchapter{#3}% \chapter*[.]{..}
}
{% \chapter
\IfNoValueTF{#2}
{\oldchapter{#3}}% \chapter{..}
{\oldchapter[#2]{#3}}% \chapter[.]{..}
}
}
\newcommand{\chapterNameRef}[1]{%
\zref@extract{#1*}{chaptertitle}% Extract chapter title
\zref@refused{#1*}% Reference has been used
}
\makeatother
\begin{document}
\chapter{What's it's name?}\label{chap:whatsItsName}
\section{Party}\label{sec:party}
\subsection{Going down}\label{subSec:goingDown}
\chapter{Second chapter}
Like said in Section \ref{subSec:goingDown} in the Chapter \chapterNameRef{subSec:goingDown}\ldots
\end{document}
将其扩展到其他部门单位也很容易。