LaTeX hyperlink: add clickable links with hyperref
Load the hyperref package, then use \href for text that links to a URL and \url to print the URL itself. Both become clickable in the PDF.
\documentclass{article}
\usepackage{hyperref}
\begin{document}
Visit \href{https://www.latextemplates.com}{LaTeX Templates} or
\url{https://www.latextemplates.com/what-is-latex}.
\end{document}\href{url}{text} showstext. \url{url} shows the address in monospace. hyperref ships with every common TeX distribution.
By default hyperref draws a coloured box around each link. The boxes appear in most PDF viewers but do not print. The next section changes that.
Link colours: colorlinks and hidelinks
\usepackage[colorlinks=true, linkcolor=blue, urlcolor=blue, citecolor=blue]{hyperref}
\hypersetup{pdftitle={My Document}, pdfauthor={A. Name}}colorlinks=truecolours the link text instead of boxing it.linkcoloris for internal references,citecolorfor citations andurlcolorfor external URLs.hidelinksremoves boxes and colours: the links still work, but nothing marks them. It suits theses and anything printed.\hypersetup{...}takes the same options after loading, plus PDF metadata such aspdftitleandpdfauthor.
Internal links
Label the target, then refer to it. hyperref makes the reference clickable.
\section{Introduction}\label{sec:intro}
Back to \autoref{sec:intro}, or to \hyperref[sec:intro]{the opening section}, or
\hyperlink{target}{jump to a target}.
\hypertarget{target}{A named target.}\refbecomes a link automatically oncehyperrefis loaded.\autoref{sec:intro}adds the type word: "section 1".\hyperref[label]{text}turns your own text into a link to the label.\hypertargetand\hyperlinklink to a spot with no label or number.
For an email link, use\href{mailto:a.name@example.com}{a.name@example.com}.
Load order
Load hyperref after nearly every other package: it redefines many internals and breaks when others redefine them afterwards. The exceptions load after it, notably cleveref, which stops withcleveref must be loaded after hyperref! otherwise.
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[hidelinks]{hyperref} % after nearly everything else
\usepackage{cleveref} % the exception: after hyperrefLong URLs and special characters
A long \url may stick out past the margin because LaTeX only breaks it at a few characters. The xurl package allows a break almost anywhere.
\usepackage[hidelinks]{hyperref}
\usepackage{xurl}
\url{https://www.example.com/a/very/long/path/that/would/otherwise/run/past/the/margin/index.html}
\href{https://example.com/page?a=1&b=2\%20x}{Special characters in an href}Inside \href and \url, #,_, & and ~ can be typed as they are. A % must be written \%.
Common mistakes
- Loading
hyperrefearly. It is the usual cause of odd errors after adding another package. Move it to the end of the preamble. - Coloured boxes around links. They do not print. Use
hidelinksto remove them in viewers too, orcolorlinksto colour the text instead. ??instead of a reference. Run LaTeX twice: references resolve on the second pass.
Templates that use this
Links to a portfolio, LinkedIn or email belong in a CV: seeJake's resume or theCV and resume templates, and thecover letter templates to match.
Related
Citations become links the same way; see theLaTeX bibliography guide. Footnotes can hold URLs; see LaTeX footnotes. If you are choosing an engine, seepdfLaTeX vs XeLaTeX vs LuaLaTeX. For complete documents to adapt, browse thethesis templates.