Insert hyperlinks in LaTeX (hyperref, href, url)
Load hyperref. \url{address} prints a web address as a clickable link, and \href{address}{text} puts the link on a text of your choice.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{hyperref}Link to a web page
\url prints the address in a monospaced font and may break it at slashes and dots. \href takes the address first and the visible text second.
Complete document:
\documentclass{article}
\usepackage{hyperref}
\begin{document}
The documentation is at \url{https://www.example.org/docs}.
News is posted on \href{https://www.example.org}{the project page}.
\end{document}Link to an email address
Write mailto: before the address in the first argument. A click then opens the mail program.
Contact: \href{mailto:anna@example.org}{anna@example.org}Links inside the document
With hyperref loaded, every \ref, \cite, footnote mark and entry in the table of contents is a link. \hyperref[key]{text} puts the link to a \label on any text.
\section{Method}\label{sec:method}
Text of the section.
\section{Results}
See section~\ref{sec:method} or read
\hyperref[sec:method]{the description of the method}.Special characters in an address
In the running text, write the address as it is: \url and \href accept _, &, ~, # and % without a backslash. Inside the argument of another command, such as \footnote, # and % need a backslash.
\url{https://www.example.org/search?q=a_b&page=2#results}
A note.\footnote{\url{https://www.example.org/a\%20b\#results}}Pitfalls
- Load
hyperrefnear the end of the preamble, after most other packages.cleverefandglossariesare among the few that have to come after it. - By default every link has a coloured frame on screen.
\usepackage[hidelinks]{hyperref}removes the frames, see Change link colours in LaTeX. - A long address without a slash or dot runs into the margin. The
xurlpackage, loaded beforehyperref, lets\urlbreak after any character.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
hyperref - Package
url - How-to Change link colours in LaTeX
- How-to Cross-references in LaTeX
- Error Illegal parameter number in definition of \pair