同时获取所有形式的名称引用

同时获取所有形式的名称引用

是否有可能(在同一个文档中)获取章节或部分标题的各种形式,类似于包nameref所做的(可能与加载不冲突hyperref),例如:

下面的最小工作示例让我获得My moderately long chapter titleMy moderately long section title,但我想以类似的方式获得My short chapter titleMy very long, rich and complex chapter title(和对应物)。section

\documentclass[11pt]{book}

% nameref is imported by hyperref
\usepackage{hyperref} % Extensive support for hypertext in LaTeX

\begin{document}

\chapter[My moderately long chapter title]{My very long, rich and complex chapter title}
\chaptermark{My short chapter title}
\label{chapter_title}

Some text.

\section[My moderately long section title]{My very long, rich and complex section title}
\label{section_title}

Some text.
\nameref{chapter_title}, \nameref{section_title}.

\end{document}

有办法实现这个吗?理想情况下,应该是这样的:

  • \longnameref{chapter_title}->My very long, rich and complex chapter title
  • \middlenameref{chapter_title}->My moderately long chapter title
  • \shortnameref{chapter_title}->My short chapter title

以及各自的section对应方。

答案1

这是代码的修改版本https://tex.stackexchange.com/a/388510/103046

我使用了非常强大的zref包并定义了名为 的新标签属性shorttitlemarktitlelongtitle使用 和 之后的短章节标题、章节标记标题或长标题\refstepcounter进行\label应用。

\chaptermark宏进行了轻微的修改,以定义在使用时 \@currentmarktitle扩展和存储哪些内容。\zlabel

\shortnameref\longnameref首先\middlenameref检查标签是否定义,如果是,则自动添加超链接。

\documentclass[11pt]{book}
\usepackage{xpatch}

\usepackage[user,hyperref]{zref}

\makeatletter

% Empty dummy macros
\providecommand{\@currentshorttitle}{}
\providecommand{\@currentlongtitle}{}
\providecommand{\@currentmarktitle}{}

\zref@newprop{shorttitle}{\@currentshorttitle}
\zref@newprop{longtitle}{\@currentlongtitle}
\zref@newprop{marktitle}{\@currentmarktitle}
\zref@addprops{main}{shorttitle,longtitle,marktitle}


\NewDocumentCommand{\shortnameref}{m}{%
  \zref@ifrefundefined{#1}{%
  }{%
    \hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{shorttitle}}%
  }%
}

\NewDocumentCommand{\longnameref}{m}{%
  \zref@ifrefundefined{#1}{%
  }{%
    \hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{longtitle}}%
  }%
}

\NewDocumentCommand{\middlenameref}{m}{%
  \zref@ifrefundefined{#1}{%
  }{%
    \hyperlink{\zref@extract{#1}{anchor}}{\zref@extract{#1}{marktitle}}%
  }%
}


\xpretocmd{\chaptermark}{\def\@currentmarktitle{#1}}{}{}% Let \chaptermark define \@currentmarktitle{}

\xpatchcmd{\@chapter}{%
  \refstepcounter{chapter}%
}{%
  \def\@currentshorttitle{#1}%
  \def\@currentlongtitle{#2}%
  \refstepcounter{chapter}%
}{\typeout{Success}}{}

\xpatchcmd{\@sect}{%
  \refstepcounter{#1}%
}{%
  \def\@currentmarktitle{}%
  \def\@currentshorttitle{#7}%
  \def\@currentlongtitle{#8}%
  \refstepcounter{#1}%
}{\typeout{Success again}}{}



\usepackage{hyperref} % Extensive support for hypertext in LaTeX

\AtBeginDocument{
\let\latex@@label\label
\RenewDocumentCommand{\label}{m}{%
  \latex@@label{#1}%
  \zlabel{#1}%
}
}
\makeatother



\begin{document}

\chapter[My moderately long chapter title]{My very long, rich and complex chapter title}
\chaptermark{My short chapter title}
\label{chapter_title}

Some text.

\section[My moderately long section title]{My very long, rich and complex section title}
\label{section_title}

\subsection[My moderately long subsection title]{My very long, rich and complex subsection title} \label{subsection_title}


Some text.

\clearpage
\tiny
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{llll}
  chapter & \longnameref{chapter_title} & \shortnameref{chapter_title} & \middlenameref{chapter_title}\tabularnewline
  section & \longnameref{section_title} & \shortnameref{section_title} & \tabularnewline
  subsection & \longnameref{subsection_title} & \shortnameref{subsection_title} & \tabularnewline
\end{tabular}

\end{document}

在此处输入图片描述

相关内容