Introduction
With the help of hyperlinks, you can make your items/text/images clickable in LaTeX. It is very easy and straight forward to use and setup hyperlinks in LaTeX document.
It is generally useful when the output format of your LaTeX code is PDF so that one can click on an item and simply follow it. This feature is achieved using a package named hyperref. It is very easy and straight forward to use and set up hyperlinks in LaTeX document.
How to add/set hyperlinks in LaTeX
Steps to make clickable links are as follows:
-
Include hyperref package into your document preamble using the command \usepackage{hyperref}. hyperref package will enable you to add links with the description of your choice as well as to set URLs into your LaTeX document.
1 2 3 4 5 6
\documentclass{article} \usepackage{hyperref} \begin{document} Testing Hyperlinks in this document. \end{document}
-
After adding the package hyperref, you can add or set the hyperlinks anywhere in your document by using the command \href. Look at the below example to understand the usage:
1 2 3 4 5 6 7
\documentclass{article} \usepackage{hyperref} \begin{document} Testing Hyperlinks in this document. This is my first hyper link: \href{http://www.resurchify.com}{Click Here} \end{document}
The above example will produce the following output:
Note: You might see a colored box surrounding your hyperlinked text. This color box is visible only on your computer (Just to highlight that it is a hyperlink). However, it will not appear in your printed copy of the document. So, just ignore it.
Embed/Include a bare URL
In order to include the bare/plain URL into your LaTeX document you need to use the \url command. Check the below example:
1 2 3 4 5 | \begin{document} Adding bare/plain URLs without any additional description: \url{http://www.resurchify.com} \end{document} |
The above example will produce the following output:
Adding email address
In order to add an email address in your LaTeX document, you need to use \href command along with the mailto attribute. Look at the below example to understand:
1 2 3 4 5 | \begin{document} This is my email address: \href{mailto:[email protected]}{[email protected]} \end{document} |
Whenever the user clicks on the email id (Here, [email protected]), it will be automatically opened in the default email software or program.
Embedding files from your system
In LaTeX, files can also be linked with the help of the url or href command. To do that, simply add the string run: at the beginning of the link string. For example,
\url{run:/path/to/sample/file.ext}
\href{run:/path/to/sample/file.ext}{text displayed}
Another way of doing this is to use relative paths to link documents near the location of your current document. To execute this, use the standard Unix-like notation (./ is the current directory, ../is the previous directory, etc.)
For example,
href{run:D:/doc_folder/doc_subfolder/sample_file.txt}{Windows, absolute path}
\href{run:doc_subfolder/sample_file.txt}{Windows, relative path}
\href{run:./doc_subfolder/sample_file.txt}{Windows, relative path, variant}
\href{run:/doc_folder/doc_subfolder/sample_file.txt}{Linux, absolute path}
\href{run:./doc_subfolder/sample_file.txt}{Linux, relative path}
Hyperlink and Hypertarget
We can also create an anchor in the document and the link for it (with or without the caption). We use the following command to create an anchor:
\hypertarget{label}{target caption}
and create a link using the following command:
\hyperlink{label}{link caption}
where the target caption and link caption is the caption or text that will be displayed at the target location and link location respectively.
Changing Default setting - Customization
Generally, default settings like color, border are fine for the users. However, if you wish to change these default settings like hyperlink color or border etc, you have to use \hypersetup command in your document preamble.
The command can be used as follows:
\hypersetup{
In the field of the option, more than one option can also be passed separated by commas. The syntax of the option has to be: "variable_name=new_value"
In case, you pass the options to the package during its loading, the command looks like this: \usepackage[
Changing LaTeX Hyperlink Color
Generally, default color is OK. However, if you wish to change the default settings of hyperlink color, you have to use \hypersetup command in your document preamble.
Summary
- Include the hyperref package in the document preamble.
- Make use of \href{Url} command for adding a hyperlink without its description.
- Make use of \href{Url}{Description} command for adding hyperlink or Url along with its description.
- Append your email id with mailto:, if you want default email software should open up when the user clicks on it.
- For changing the color of hyperlink or any default setting, you can use \hypersetup package.