1

I would like to add a text with a hyperlink to my document being generated via pyLaTeX. In LaTeX this would be performed by using:

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

I have found the labelref documentation containing the Marker object as well as the Hyperref Object but i cannot seem to get it working.

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)

Running the following code produces the a pdf document without errors. image of the document produced

While what i would expect is that the word "here" is a hyperlink and shown in blue.

4
  • What is the output you actually get? Or error messages, if any? Commented Jul 16, 2019 at 16:16
  • I don't get any errors, the document is generated however, since i am incorrectly using the Hyperref object, the hyperlink is not created. Commented Jul 16, 2019 at 17:56
  • 1
    Running your code, it outputs \hyperref[https://jeltef.github.io/PyLaTeX]{here} to Example_document.tex. That line is trying to reference a label, not a URL. It also mentions in the documentation you linked that the class is meant to reference label/ref parameters. Doesn't look like this package can automatically reference URLs like how you want. Commented Jul 16, 2019 at 18:58
  • Ah, that's disappointing. Thanks for taking a look though! Commented Jul 16, 2019 at 20:29

1 Answer 1

4

Here is how I do it. I created a class that gives you the needed hyperlink functionality. There is also an issue on GitHub where I contributed this as well. 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)
Sign up to request clarification or add additional context in comments.

1 Comment

If you, like me, are looking for a way to link to a section or subsection of your document, you can adapt this like so: def sectionLink(section, text): text = escape_latex(text) return NoEscape(r'\hyperref[' + section + ']{' + text + '}'). The 'section' part is the label you assign via Section(), e.g. something like 'sec:toc'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.