简体   繁体   English

LaTeX 中的狮身人面像交叉引用

[英]Sphinx cross-reference in LaTeX

I have the following reference identified in file A:我在文件 A 中确定了以下参考:

.. _my-label:

and I reference it in file B:我在文件 B 中引用它:

this is a reference to file A :ref:`my-label`

This generates a cross-reference as expected when outputting HTML.这会在输出 HTML 时按预期生成交叉引用。 However, when outputting LaTeX, it does not and I have the classical warning:但是,当输出 LaTeX 时,它没有,我有经典警告:

LaTeX Warning: Hyper reference `my-label:my-label' on page XX undefined on input line YY.

Is there a LaTeX trick like double compilation or something similar that I am not doing correctly?是否有 LaTeX 技巧,如双重编译或类似的东西,我没有正确执行?

I encountered the same issue.我遇到了同样的问题。 HTML compiled without errors for me, but LaTeX compilation did throw the hyperref errors you described. HTML 编译对我来说没有错误,但是hyperref编译确实抛出了你描述的超引用错误。 It seems to me that, for some obscure reason, Sphinx does not create the labels that hyperref tries to reference.在我看来,出于某种模糊的原因,Sphinx 并没有创建hyperref试图引用的标签。


I came up with the following solution: since I do not know how to include the missing labels, I will just make it so that LaTeX does not look for them anymore.我想出了以下解决方案:因为我不知道如何包含缺少的标签,所以我将这样做,以便 LaTeX 不再寻找它们。 In detail, I am doing this by overwriting the \hyperref command.详细地说,我通过覆盖\hyperref命令来做到这一点。

I included the following code in my conf.py file:我在我的conf.py文件中包含了以下代码:

latex_elements = {
    'preamble': r'''
\renewcommand{\hyperref}[2][]{#2}
'''
    }

This includes the \renewcommand{... in the preamble of the LaTeX document created by Sphinx.这包括由 Sphinx 创建的 LaTeX 文档的序言中的\renewcommand{... It will overwrite the \hyperref command so that it won't try to insert a link, but just print the link text.它将覆盖\hyperref命令,因此它不会尝试插入链接,而只是打印链接文本。

Obviously, with this solution, the reference that caused the errors will not appear as hyperlinks in your PDF document, but at least it is compiling without errors.显然,使用此解决方案,导致错误的引用不会在您的 PDF 文档中显示为超链接,但至少它正在编译没有错误。


Note笔记

What I described worked perfectly fine for my use case, however, it is described in the Hyperref manual that the \hyperref command can be invoked in two different ways ( \hyperref{URL}{category}{name}{text} and \hyperref[label]{text} ).我所描述的对我的用例来说非常好,但是,在Hyperref 手册中描述了\hyperref命令可以以两种不同的方式调用( \hyperref{URL}{category}{name}{text}\hyperref[label]{text} )。 I am only overwriting the second one, as that seems to be the one that Sphinx is using for cross references.我只覆盖了第二个,因为那似乎是 Sphinx 用于交叉引用的那个。 However, not accounting for the first one when overwriting the command might lead to issues in some cases.但是,在覆盖命令时不考虑第一个可能会在某些情况下导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM