The hyperref package
Turns cross-references, citations, the table of contents and URLs into clickable links and writes bookmarks and metadata into the PDF; \href, \url and \hypersetup are its main commands.
Loading the package
\usepackage{hyperref}Loading the package is enough to make every \ref, \cite, footnote mark and entry of the table of contents a link inside the PDF. The section headings also appear as bookmarks in the side panel of the PDF viewer.
By default a link is marked with a coloured frame that is visible on screen. colorlinks=true colours the link text instead, and hidelinks removes every marking while the links keep working.
All settings go into \hypersetup{...} or into the package options. This includes the document properties pdftitle, pdfauthor and pdfkeywords.
Main commands
\href{https://example.org}{link text} | Link to a web address with a text of your choice; mailto: addresses work too. |
\url{https://example.org} | Prints the address itself and links it. |
\hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue, citecolor=blue} | Coloured link text instead of frames, with one colour each for internal links, URLs and citations. |
\usepackage[hidelinks]{hyperref} | Links without frame and without colour, for documents that will be printed. |
\autoref{sec:intro} | Reference with the kind of object in front, for example "section 1", all of it a link. |
\nameref{sec:intro} | Prints the title of the referenced section instead of its number. |
\hyperref[sec:intro]{the introduction} | Link to a label with a text of your choice. |
\texorpdfstring{$\alpha$}{alpha} | In a heading: the first argument is typeset, the second one goes into the bookmark. |
Complete example
\documentclass{article}
\usepackage{hyperref}
\hypersetup{
colorlinks=true, linkcolor=blue, urlcolor=blue,
pdftitle={Links in a PDF}, pdfauthor={A. Author}
}
\begin{document}
\tableofcontents
\section{Introduction}\label{sec:intro}
The project page is \url{https://example.org/project}, and the
\href{https://example.org/manual.pdf}{manual} is a PDF file.
\section{\texorpdfstring{$\alpha$}{Alpha} decay}
See \autoref{sec:intro}, called \nameref{sec:intro}, or jump to
\hyperref[sec:intro]{the first section}.
\end{document}Compiled with pdfLaTeX from TeX Live 2023. Tools run in this order: pdflatex → pdflatex.
Pitfalls
- Load
hyperrefnear the end of the preamble, because it redefines commands of many other packages. A few packages have to come after it, among themcleveref,glossariesandbookmark. - Math or formatting commands in a heading give the warning Token not allowed in a PDF string (Unicode), because a bookmark is plain text. Give the bookmark its own text with
\texorpdfstring. - When two pages have the same page number, for example a title page followed by a reset page counter, pdfTeX reports destination with the same identifier (name{page.1}) has been already used, duplicate ignored.
- Without a colour package only the basic colour names such as
red,blueandmagentaexist. For a name liketealor a mix likeblue!60!black, loadxcolor. - The bookmarks are read from the
.outfile of the previous run, so they are complete after the second run.
Related pages
- How-to Insert hyperlinks in LaTeX
- How-to Change link colours in LaTeX
- How-to Set PDF metadata in LaTeX
- Package
cleveref - Error Token not allowed in a PDF string (Unicode)
- How-to Set up a thesis in LaTeX
- How-to References with automatic names in LaTeX
- How-to Refer to a page number in LaTeX
- How-to Create a minimal document in LaTeX
- How-to Create a table of contents in LaTeX