Creating and Customizing Hyperlinks in LaTeX

Creating and Customizing Hyperlinks in LaTeX

This post may contain affiliate links that allow us to earn a commission at no expense to you. Learn more

Hyperlinks in LaTeX offers many features for creating high-quality professional-looking documents. In the digital era, it is sometimes vital for documents to contain hyperlinks. These could be links to other websites that offer additional information about a particular topic, or they may be hyperlinks that direct to a certain file in your system. Let’s examine how to use hyperlinks in LaTeX properly.

LaTeX uses the hyperref package to cross-reference elements and create hyperlinks. You can include this package in your document preamble by using command \usepackage{hyperref}. The package enables you to add links into your document along with a custom description.

An example of how to use this is shown below:

\documentclass{article}

\usepackage{hyperref}

\begin{document}

Example of Hyperlinks in a document.

\end{document}

Once you have added the hyperref package, you can place hyperlinks in LaTeX in any part of your document by using the \href command. This is demonstrated in the example below:

\documentclass{article}

\usepackage{hyperref}

\begin{document}

Example of Hyperlinks in a document.

Here is the first hyperlink:  \href{https://www.google.com}{Click This}

\end{document}

This should produce an output that resembles the following:

Embedding a URL Into Your Document

LaTeX allows you to insert bare URLs into your document using the command \url. For example:

\begin{document}

Adding a simple URL without any additional description: \url{https://www.google.com}

\end{document}

This should produce an output that resembles the following:

Adding an Email Address in Your Document

Adding an email address to LaTeX works a bit differently than adding a bare URL.  You will need to use the command \href as well as the attribute mailto to do this. This is shown in the following example:

\begin{document}

This is an example of an email address: \href{mailto:[email protected]}{[email protected]}

\end{document}

When compiled, the output should resemble the following:

The email address in the above example will be clickable when compiled. It will direct users to create an email in their default email software or other program. This form of hyperlinks in LaTeX is useful when you need to embed an email address that directs users automatically.


Related Content for Latex


How to Embed Files From Your System

LaTeX also allows you to link files from your system using the url and href commands. This starts by using the command run: at the start of a link string. An example of this is shown below:

\url{run:/path/to/sample/examplefile.ext}

\href{run:/path/to/sample/examplefile.ext}{your text shown}

You can also link documents through relative paths that lead to your current document. An example of this is shown below:

href{run:D:/doc_folder/doc_subfolder/yourexample_file.txt}{Windows, absolute path}

\href{run:doc_subfolder/yourexample_file.txt}{Windows, relative path}

\href{run:./doc_subfolder/yourexample_file.txt}{Windows, relative path, variant}

\href{run:/doc_folder/doc_subfolder/yourexample_file.txt}{Linux, absolute path}

\href{run:./doc_subfolder/yourexample_file.txt}{Linux, relative path}

LaTeX also allows you to create an anchor within your document and a corresponding link for it. You can create hyperlinks in LaTeX with an anchor using the following code:

\hypertarget{label}{target caption}

You can then create the corresponding link for this anchor using the command: \hyperlink{label}{link caption} 

In the above codes, the target caption refers to the caption that will be shown at your target location and the link location.

There are many options for customizing hyperlinks in LaTeX. You can set the hyperlink color and border using the command \hypersetup inthe document preamble. 

For example:

\documentclass{book}

\usepackage{hyperref}

\hypersetup{

    colorlinks=true,

    linkcolor=blue,

    filecolor=magenta,      

    urlcolor=cyan,

    pdftitle={An Example},

    pdfpagemode=FullScreen,

    }

\urlstyle{same}

In the above code, \hypersetup{….} allows us to configure attributes of the link in the document. We then separated each of the parameters that followed using a comma. The syntax for these parameters is denoted as parameter=value.

\colorlinks=true informs LaTeX to make the links colored. The default color produced with this command is red.

\linkcolor=blue informs LaTeX that internal links that were created using cross-referenced elements should be in blue

\filecolor=magenta informs LaTeX that any links to local files should be in magenta.

\urlcolor=cyan informs LaTeX that any links to websites should be in cyan.

\urlstyle{same} informs LaTeX to print the links using a mono-style space font.

In certain cases, you may need to insert links into your document manually. This is possible using the command \label. LaTeX lets you do this because cross-referenced elements are turned into links after hyperref is imported.

For example:

You can directly link a word 

or \hyperlink{thesentence}{a sentence} in your document.

\hypertarget{thesentence}{this sentence}.

This should produce an output that resembles:

As mentioned above, you can create hyperlinks in LaTeX in many different styles. Some of these are shown below:

  • hyperindex: This turns the page numbers in the index entries into clickable hyperlinks. It’s default value is true
  • linktocpage: This turns your page numbers into links instead of the text in the Table of Contents. It’s default value is false.
  • breaklinks: This informs LaTeX to break links into multiple lines.It’s default value is false.
  • colorlinks: This sets the color for the text in links and anchors. These colors will show up in the document’s printed version. It’s default value is false. 
  • linkcolor: This sets the color for normal internal links in the document. It’s default value is red.
  • anchorcolor: This sets the color for your document’s anchor text. It’s default value is black
  • citecolor: This sets the color for your document’s bibliographic citations. It’s default value is green.
  • filecolor: This sets the color for the links that open local files on your system. It’s default value is cyan.
  • urlcolor: This sets the color for your document’s linked URLs. It’s default value is magenta.

Final Thoughts

As you can see, LaTeX allows you to insert hyperlinks into your document in many different ways. You can also customize hyperlinks in LaTeX using the various commands discussed above. Hyperlinks help lead readers to valuable information, so consider using them in your next LaTeX document.

Frequently Asked Questions

Some frequently asked questions related to hyperlinks in LaTeX are shown below:

Q1. How Do You Hyperlink in Latex?

You can add a hyperlink into your LaTeX article using the command \usepackage{hyperref} and then including the command \href{YOUR URL}{TEXT FOR YOUR HYPERLINK}.

Q2. What Is Hyperref Latex?

hypperef is a LaTeX package that allows you to cross-reference elements and create hyperlinks.

Q3. Can We Change the Colour of Hyperlink Using Latex?

You can change the color of hyperlinks in LaTeX using the command  colorlinks=true followed by  
 linkcolor=[color name],
filecolor=[color name],      
urlcolor=[colorname]

Q4. How Do I Make a Clickable Link?

You can make a clickable link using the command \usepackage{hyperref} and then including the command \href{YOUR URL}{TEXT FOR YOUR HYPERLINK}.

Q5. What to do if I still have issues with the LaTex types of lists? 

We find sites like WikiBooks and the StackExchange helpful.

There is more.


Photo of author
Author
Joannah W.
Joannah has been a science publisher with close to 20 years of experience. She wants to help students and researchers stay ahead of the trends and developments in the science community by making science more accessible to everyone.

Leave a Comment