1

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

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

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

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 文档。生成的文件的图像

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

4

1 回答 1

4

这是我的做法。我创建了一个类,它为您提供所需的超链接功能。在 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)
于 2019-09-30T14:15:07.747 回答