Python:PyLaTeX 超链接到 URL

标签 python latex pylatex

我想添加一个带有超链接的文本到通过 pyLaTeX 生成的文档。在 LaTeX 中,这可以通过使用以下命令来执行:

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

我找到了 labelref documentation包含标记对象以及 Hyperref 对象,但我似乎无法让它工作。

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

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

最佳答案

这是我的做法。我创建了一个类,为您提供所需的超链接功能。 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)

关于Python:PyLaTeX 超链接到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57059457/

相关文章:

python - 在 Windows 10 上安装 PyPotrace

Python 在星号之间多次提取多个字符串

python - 如何在 python 中正确地打乱列表

python - 将静态项目上传到谷歌应用引擎

r - 章节标题从页面中间开始

fonts - 下载报告时使用 RStudio 的 Shiny 额外字体 (LaTeX)

latex - 目录链接指向错误的页面

python - 如何在 pylatex 中添加页面背景图像?