使用 hyperref 进行交叉引用的最佳实践

使用 hyperref 进行交叉引用的最佳实践

我想引用文档的某些部分。目前我看到有几种选择。单击交叉引用时,应该可以找到文档的正确部分。

第一个选项是使用标签,如上所述维基百科。但我认为这不会产生可点击的链接。

我认为第二种选择更好:hyperref 包,我已经在使用。我在手动的

我怎样才能以最简单的方式链接各个部分?

我不想为每个部分添加一个,\label我认为这可以自动完成。目录中的条目是链接的,因此我推测某些标签已经定义。此外,该过程应该能够阻止重命名和在文档中移动该部分。这可能吗,还是每次更改后我都必须检查我的文档两次?

答案1

使用标签和 hyperref 可以生成可点击的链接,至少对我来说是这样。下面是一个简短的例子:

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\tableofcontents

\section{Fermat’s Principle}
    \label{sec:fermat}
    Fermat’s principle states that the path light takes from one point 
    to another is not necessarily the one with the smallest distance,
    but rather the path which can be traversed in the shortest time.
\section{Geometrical Optics}
    Geometrical optics is an approximation for light propagation in 
    cases where the wavelength is very small compared to the
    structures with which the light interacts. Snell’s Law, describing 
    refraction, can be derived from Fermat’s principle 
    (see section~\ref{sec:fermat}).    
\end{document}

在此处输入图片描述 当然,这可以阻止移动和重命名该部分,只要您将标签与部分一起移动并且不重命名它即可。

答案2

您可以使用命令\hyperlink{chapter.2}{LinkText}\hyperlink{section.2.1}{LinkText}(如用于 pdf 的目录)。使用这些命令,您可以将绝对(!)链接到第 2 章或第 2.1 节。如果您移动章节,链接的目标不会移动。我认为您应该使用标签引用机制来满足您的需求。

答案3

hyperref包提供命令\autoref,这可能是进行交叉引用的最干净的方式hyperref

您仍需要按照@Psirus 的回答,但不是通过类型来引用它们,

section~\ref{sec:fermat}

你可以简单地这样做:

\autoref{sec:fermat}

优点:

  • 自动检测标签类型(部分、小节、图形、表格……)
  • 整个标签(包括类型的文本)均可点击

定制

如果您对生成的标签文本仍然不满意,您可以随意更改它们,如下所示:

\def\subsectionautorefname{FooSubSection}

如果您只想获得一次完全不同的文本,您也可以使用以下\hyperref命令:

\hyperref[sec:fermat]{Fermat's cool stuff}

有关的

为了完整起见,还有cleverref包,它提供了类似的命令,但似乎更具可配置性。

相关内容