【问题标题】:Python: PyLaTeX hyperlink to a URLPython:到 URL 的 PyLaTeX 超链接
【发布时间】:2019-07-16 14:20:11
【问题描述】:

我想在通过 pyLaTeX 生成的文档中添加带有超链接的文本。在 LaTeX 中,这将通过以下方式执行:

\href{http://www.sharelatex.com}{Something Linky}

我找到了包含 Marker 对象和 Hyperref 对象的 labelref documentation,但我似乎无法让它工作。

from pylatex import Document, Hyperref

doc = Document()
doc.append("Example text a link should go ")
# i was hoping the hyperlink would work like i'm showing below
doc.append(Hyperref("https://jeltef.github.io/PyLaTeX", "here"))
doc.generate_pdf("Example_document", clean_tex=True)

运行以下代码会生成一个没有错误的 pdf 文档。 image of the document produced

虽然我希望“这里”这个词是一个超链接,并以蓝色显示。

【问题讨论】:

  • 你实际得到的输出是什么?或错误消息(如果有)?
  • 我没有收到任何错误,但是生成了文档,因为我错误地使用了 Hyperref 对象,所以没有创建超链接。
  • 运行您的代码,它将\hyperref[https://jeltef.github.io/PyLaTeX]{here} 输出到Example_document.tex。该行试图引用 label,而不是 URL。它还在documentation you linked 中提到该类旨在引用标签/引用参数。看起来这个包不能像你想要的那样自动引用 URL。
  • 啊,真令人失望。感谢您的观看!

标签: python latex pylatex


【解决方案1】:

这是我的做法。我创建了一个类,它为您提供所需的超链接功能。在 GitHub 上也有一个问题,我也贡献了这个。 https://github.com/JelteF/PyLaTeX/issues/264

    from pylatex import Document, Hyperref, Package
    from pylatex.utils import escape_latex, NoEscape

    def hyperlink(url,text):
        text = escape_latex(text)
        return NoEscape(r'\href{' + url + '}{' + text + '}')

    doc = Document()
    doc.packages.append(Package('hyperref'))
    doc.append("Example text a link should go ")
    # i was hoping the hyperlink would work like i'm showing below
    doc.append(hyperlink("https://jeltef.github.io/PyLaTeX", "here"))
    doc.generate_pdf("Example_document", clean_tex=True)

【讨论】:

  • 如果您和我一样,正在寻找链接到文档的某个部分或子部分的方法,您可以这样调整:def sectionLink(section, text): text = escape_latex(text) return NoEscape(r'\hyperref[' + section + ']{' + text + '}')。 “部分”部分是您通过 Section() 分配的标签,例如类似'sec:toc'.
猜你喜欢
  • 1970-01-01
  • 2012-11-20
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 2011-12-05
  • 1970-01-01
相关资源
最近更新 更多