1. Introduction

In this tutorial, we’ll show how to insert clickable hyperlinks into a LaTeX text using package hyperref. We’ll cover internal links to other parts of our document (e.g., sections and footnotes) and external links, which lead to web resources.

2. The hyperref Package

We’ll now review the hyperref package, the options it needs, and its main commands.

To use it, we need to include it and set the formatting options (about which we’ll talk later):

\usepackage[colorlinks=true, urlcolor=blue, linkcolor=red]{hyperref}

It offers three commands we can use to insert links: \href, \url, and \hyperref, which we detail in the following sections.

3. The Command \href

We use \href to create a link to an outside resource. Its syntax is:


When LaTeX renders this command, it will place clickable TEXT within the document. Clicking on TEXT will take us to the resource URL points. For example:

We know of several popular news websites. These include \href{www.cnn.com}{CNN}, \href{www.bbc.com}{BBC}, and \href{news.google.com}{Google News}.

The output is:


Since the URLs point to websites, clicking the links will open the corresponding web pages in our browser.

We can also insert links to local files. Let’s suppose the directory with the current document contains file sample.txt. We could access it using the run option:

There's a file ./sample.txt which contains some interesting information.
Let's open it by clicking \href{run:./sample.txt}{here}.

When we compile this code, we get:

Rendered by QuickLaTeX.com

When we click on the colored link, we will see the target file. That may not happen if our viewer has security controls. Furthermore, the \url command is known to malfunction in some cases.

Instead of ./sample.txt, which is a file in the current directory, we can also specify a file with respect to its parent directory, or give its absolute path. In the case of Windows, we can also specify the drive:

\href{run:C:/Users/my_name/sample.txt}{Absolute Windows path}

We note that a forward slash is used in Windows, unlike normal Windows conventions.

4. The Command \url

We use the \url command to just list the URL alone. In this case, we explicitly see the URL, and clicking on it takes us to the corresponding website. In the following example, we also include an ordinary footnote to demonstrate that the link is shown in red (as dictated by the linkcolor=red option to hyperref).

We know of several popular news websites\footnote{There are, of course, hundreds of news websites that we have omitted in this simple example.}.
These include \url{www.cnn.com}, \url{www.bbc.com} and \url{news.google.com}.

After compiling, we see that the URLs are explicitly shown in the text:


Clicking on a URL takes us to the corresponding website. Clicking on the red footnote symbol “1” moves us to the corresponding footnote text.

5. The Command \hyperref

\hyperref[LABEL]{TEXT} creates a link, shown as TEXT,  that points to our document’s element (e.g., section or figure) whose label is LABEL. Clicking on TEXT takes us to it.

That is useful when we want internal links. For example:

\section{Introduction} \label{sec:Intro}

This is the introductory section. It refers to another section, \hyperref[sec:AC]{alternating currents}

\section{Alternating Currents} \label{sec:AC} This section refers to the previous section, \hyperref[sec:Intro]{Introduction}).

\section{Forces} \label{sec:Forces}

This section refers \hyperref[sec:Intro]{to an earlier Section \ref*{sec:Intro}}.

Here’s the output:


The link texts point to the corresponding sections within the document.

6. Appearance

In previous examples, the links appeared as text of various colors. We can control links’ appearance by setting the options when including the package. Here are the most commonly used options:


Now, let’s take a look at a few examples.

6.1. Examples

If we set colorlinks=false, the ensuing output is obtained:

We know of several popular news websites. These include \href{www.cnn.com}{CNN},
\href{www.bbc.com}{BBC} and \href{news.google.com}{Google News}.
hyperExample1 5

If we alternatively use:


we get the following, in which the default colors are used:


We can change this as desired. For instance:

\usepackage[colorlinks=true, urlcolor=cyan, filecolor=green]{hyperref}
We know of several popular news websites.  These include \href{www.cnn.com}{CNN},
\href{www.bbc.com}{BBC} and \href{news.google.com}{Google News}.
Some interesting information is contained \href{run:./sample.txt}{here}.

That yields the following output:


Here is a more elaborate example, where we’ve used the xcolor package to access a wider range of colors:

\usepackage[colorlinks=true, urlcolor=Bittersweet, filecolor=Fuchsia, linkcolor=JungleGreen]{hyperref}
We know of several popular news websites.  These include \href{www.cnn.com}{CNN},
\href{www.bbc.com}{BBC} and \href{news.google.com}{Google News}\footnote{We
changed the color of internal links to JungleGreen.}.
Some interesting information is contained \href{run:./sample.txt}{here}.

The end result in this case is:


7. Conclusion

In this article, we showed how to incorporate hyperlinks into a LaTeX document with the hyperref package. Its command \href takes us to external resources, while \hyperref allows us to navigate to different parts of our document.

Comments are closed on this article!