Icon for parent category 'Writing'

Hyperlinks & URLs

At the time TeX was introduced, hyperlinks and URLs weren’t as mainstream as they are now. That’s why any support for these has to come from a nifty little package called hyperref.

What it Does

Hyperref automatically turns all your \ref commands into hyperlinks within the document. It makes them clickable. And when you do click it, you are brought to the \label to which they refer.

It also automatically creates PDF bookmarks for all your sections and makes the table of contents entries clickable.

URLs

Turning a string into a clickable URL is now easy: use \url{theURL}

1\usepackage{hyperref}
2
3\begin{document}
4    Plain old text url: http://www.google.com
5    \par Fancy, dynamic url: \url{http://www.google.com}
6\end{document}
Code left > output right
Code left > output right

Custom Names

If you want to create a clickable reference with a different name, use

1\hyperref[labelName]{linkText}
Remark

You can use a regular reference within the linkText. But you must use the star variation: \ref* Otherwise, infinitely nested links would occur.

If you want to create a clickable URL with a different name, use

1\href{theURL}{linkText}
1Fancy, dynamic url: \href{http://www.google.com}{Go to Google!}
Code left > output right
Code left > output right

Custom Anchors

It’s also possible to simulate the same system without having to use labels and references. You can create an anchor anywhere within the document with

1\hypertarget{labelName}{caption}

At the place you insert it—the target location—the text caption is displayed. You can leave it empty if you want.

To link to the anchor you just placed, use

1\hyperlink{labelName}{caption}

The text caption is displayed. This time it can’t be empty, otherwise there’d be nothing to click!

1\hypertarget{target1}{I'm a target!} \par
2\vspace{20mm}
3
4% Clicking this moves the top of the page to the spot of the hypertarget
5\hyperlink{target1}{Go to the target!}
Code left > output right
Code left > output right

Local Files and Emails

Very easily, you can use these commands for more than links within the document. For example, you can link to files on the reader’s computer. Or automatically open an email client pointed at your email.

For quick emailing, use

1\href{mailto:yourMail}{yourMail}

For files, use

1\href{run:pathToFile.ext}{caption}

How can I know the files on a reader’s computer? You don’t, of course. But this is useful for manuals of some kind. If a user has installed software, you know the most likely path to its files. It can also simply be useful for yourself, creating quick links to attachments you want to review often.

Continue with this course
Support me and this website!

Want to support me?

Buy one of my projects. You get something nice, I get something nice.

Donate through a popular platform using the link below.

Simply giving feedback or spreading the word is also worth a lot.