LaTeX is a great tool for printable professional-looking documents, but can be also used to generate PDF files with excellent navigation tools. This article describes how to create hyperlinks in your document, and how to set up LaTeX documents to be viewed with a PDF-reader.
Contents |
Let's start with a minimal working example, by simply importing the hyperref package all cross-referenced elements become hyperlinked.
\documentclass{book} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage{hyperref} \begin{document} \frontmatter \tableofcontents ... \end{document}
The lines in the table of contents become links to the corresponding pages in the document by simply adding in the preamble of the document the line
\usepackage{hyperref}
One must be careful when importing hyperref. Usually, it has to be the last package to be imported, but there might be some exceptions to this rule.
Open an example of the hyperref package in Overleaf
The default formatting for links can be changed so the information in your documents is more clearly presented. Below you can see an example:
\documentclass{book} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage{hyperref} \hypersetup{ colorlinks=true, linkcolor=blue, filecolor=magenta, urlcolor=cyan, } \urlstyle{same} \begin{document} \tableofcontents \chapter{First Chapter} This will be an empty chapter and I will put some text here \begin{equation} \label{eq:1} \sum_{i=0}^{\infty} a_i x^i \end{equation} The equation \ref{eq:1} shows a sum that is divergent. This formula will later be used in the page \pageref{second}. For further references see \href{http://www.sharelatex.com}{Something Linky} or go to the next url: \url{http://www.sharelatex.com} or open the next file \href{run:./file.txt}{File.txt} It's also possible to link directly any word or \hyperlink{thesentence}{any sentence} in your document. \end{document}
This is a complete example, it will be fully explained in the rest of the article. Below is a description of the commands related to the colour and styling of the links.
\hypersetup{ ... }
\colorlinks=true
\linkcolor=blue
\filecolor=magenta
\urlcolor=cyan
\urlstyle{same}
Open an example of the hyperref package in Overleaf
Links to a web address or email can added to a LaTeX file using the \url
command to display the actual link or \href
to use a hidden link and show a word/sentence instead.
For further references see \href{http://www.sharelatex.com}{Something Linky} or go to the next url: \url{http://www.sharelatex.com}
There are two commands in the example that generate a link in the final document:
\href{http://www.sharelatex.com}{Something Linky}
\url{http://www.sharelatex.com}
Open an example of the hyperref package in Overleaf
The commands \href
and \url
presented in the previous section can be used to open local files
For further references see \href{http://www.sharelatex.com}{Something Linky} or go to the next url: \url{http://www.sharelatex.com} or open the next file \href{run:./file.txt}{File.txt}
The command \href{run:./file.txt}{File.txt}
prints the text "File.txt" that links to a local file called "file.txt" located in the current working directory. Notice the text "run:" before the path to the file.
The file path follows the conventions of UNIX systems, using . to refer the current directory and .. for the previous directory.
The command \url{}
can also be used, with the same syntax described for the path, but it's reported to have some problems.
Open an example of the hyperref package in Overleaf
It was mentioned before that all cross-referenced elements become links once hyperref is imported, thus we can use \label
anywhere in the document and refer later those labels to create links. This is not the only manner to insert hyperlinks manually.
It's also possible to link directly any word or \hyperlink{thesentence}{any sentence} in you document. If you read this text, you will get no information. Really? Is there no information? For instance \hypertarget{thesentence}{this sentence}.
There are two commands to create user-defined links.
\hypertarget{thesentence}{this sentence}
\hyperlink{thesentence}{any sentence}
Open an example of the hyperref package in Overleaf
Links in a document are created having in mind a document that will be read in PDF format. The PDF file can be further personalized to add additional information and change the way the PDF viewer displays it. Below an example:
\hypersetup{ colorlinks=true, linkcolor=blue, filecolor=magenta, urlcolor=cyan, pdftitle={Sharelatex Example}, bookmarks=true, pdfpagemode=FullScreen, }
Using the command \hypersetup
, described in the section styles and colours, accepts extra parameters to set up the final PDF file.
pdftitle={Sharelatex Example}
bookmarks=true
pdfpagemode=FullScreen
See the reference guide for a full list of options that can be passed to \hypersetup
.
Open an example of the hyperref package in Overleaf
Linking style options
Option | Default value | Description |
---|---|---|
hyperindex
|
true | Makes the page numbers of index entries into hyperlinks |
linktocpage
|
false | Makes the page numbers instead of the text to be link in the Table of contents. |
breaklinks
|
false | Allows links to be broken into multiple lines. |
colorlinks
|
false | Colours the text for links and anchors, these colours will appear in the printed version |
linkcolor
|
red | Colour for normal internal links |
anchorcolor
|
black | Colour for anchor (target) text |
citecolor
|
green | Colour for bibliographical citations |
filecolor
|
cyan | Colour for links that open local files |
urlcolor
|
magenta | Colour for linked URLs |
frenchlinks
|
false | Use small caps instead of colours for links |
PDF-specific options
Option | Default value | Description |
---|---|---|
bookmarks
|
true | Acrobat bookmarks are written, similar to the table of contents. |
bookmarksopen
|
false | Bookmarks are shown with all sub-trees expanded. |
citebordercolor
|
0 1 0 | Colour of the box around citations in RGB format. |
filebordercolor
|
0 .5 .5 | Colour of the box around links to files in RGB format. |
linkbordercolor
|
1 0 0 | Colour of the box around normal links in RGB format. |
menubordercolor
|
1 0 0 | Colour of the box around menu links in RGB format. |
urlbordercolor
|
0 1 1 | Colour of the box around links to URLs in RGB format. |
pdfpagemode
|
empty | Determines how the file is opened. Possibilities are UseThumbs (Thumbnails), UseOutlines (Bookmarks) and FullScreen. |
pdftitle
|
Sets the document title. | |
pdfauthor
|
Sets the document Author. | |
pdfstartpage
|
1 | Determines on which page the PDF file is opened. |
For more information see