How to link to web pages from LaTeX-generated PDF

This has been on my to-do list for a while, but I finally found out how to embed hyperlinks in a PDF file generated from LaTeX.

Short answer: put \usepackage{hyperref} in your header, and when you want to link to a page, use the command \href{URL}{anchor text}. For example,

\documentclass{article}
\usepackage{hyperref}
\begin{document}

Here's a link to \href{http://twitter.com/home}{Twitter}.

\end{document}

For much more detail on links in LaTeX documents, see Patrick Jöckel’s LaTeX-PDF page and the hyperref package documentation. [Update: looks like these links went away.]

22 thoughts on “How to link to web pages from LaTeX-generated PDF

  1. Thanks a lot. This is the simple statement about how to use href I was looking for!
    Much clearer than the ‘short’ documentation of the hyperref package.

  2. Manuel… and others… you have probably forgotten to add the:

    usepackage{hyperref}

    Just place it below the documentclass statement

  3. John,

    Can I translate some of your posts on LaTeX to Portuguese and post them in my own blog, of course referring to the original post? They’re quite straightforward.

    Thanks!

  4. hi,

    I managed to use href to add a weblink to my pdf. however i want the anchor text to be equal to the url (in order to also enable someone who printed the doc get to that web page). so I do:
    href{}{}

    The problem is that the URL is much longer than the textwidth. how can i make the link use multiple lines?

    Thanks in advance!
    greg

  5. Actually I found the solution myself:

    usepackage{url}
    usepackage{hyperref}
    usepackage{breakurl}

    url{http://www.thereallyreallyverylonglink.com/pointing/to/some/interesting/stuff/in/the/web}

    it seems to be important to import the packages in this order!

    HTH
    g

  6. If you use dvipdfm to make your pdf, you’ll need to use the following command:
    usepackage[dvipdfm]{hyperref}
    in place of
    usepackage{hyperref}

    Best!

  7. Thanks for that suggestion. I still feel that it should be possible to address the url wrapping problem within latex. I mean I suspect some of the above packages deliberately avoid breaking at hyphens because it creates ambiguity as to whether the hyphen is meant as a breaking hyphen or is actually a non-breaking hyphen that is part of the original URL.

    The conflicts and overrides generated by successively calling on multiple packages really means you have to rack your brain to figure out the right order to call packages. Ultimately it may not be possible to manipulate the order so all commands provided by these packages work as they are intended to.

    I’m using all of

    usepackage{url}
    usepackage{hyperref}
    usepackage{breakurl}

    Can John or somebody else suggest a Latex solution, for a long URL with many hyphens?

  8. I want to have the effect of an hyperref but I don’t really want to link it to a web site. What I want is to have a box, with the text I provide, open whenever the cursor is on the supposed link. Nothing more. Any idea how I can do that?

  9. In order to allow breaking a url at hyphens, one can load the following packages:

    usepackage[hyphens]{url}
    usepackage[breaklinks,pdftex]{hyperref}

    As you can probably tell, this works with pdflatex. Haven’t tried it with “regular” latex, though.

  10. The default colours are not incredibly pretty; instead of just including hyperref without options, something like this might help:


    usepackage[usenames,dvipsnames]{xcolor}
    usepackage[hidelinks,colorlinks,citecolor=Fuchsia,urlcolor=blue,linkcolor=Cerulean]{hyperref}

  11. Dear John,

    Thank you for the useful pointers on hyper-linking your latex generated PDF’s.

    I am trying to link to certain Wikipedia pages…for example:
    http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U

    I use the following command:
    \href{http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U}{\textcolor{blue}{Wikipedia}}.

    I can’t seem to get it to work…I think because Latex assumes % to be a comment symbol. But Wikipedia uses % in the URL name. So the URL seems to be confusing Latex.

    Any thoughts on a work around to this problem. I’m very new to latex so I greatly appreciate any help you could provide.

    Chris

Comments are closed.