Links in LaTeX: hyperref, \href, \url and Clickable References

Quick answer

  • Package: \usepackage{hyperref}, near the end of the preamble, but before cleveref or bookmark.
  • Link with your own text: \href{https://example.org}{link text}. Show the address itself: \url{https://example.org}.
  • Link inside the document: \hyperref[sec:intro]{link text}, or \autoref{sec:intro} for “section 1”.
  • Colours instead of boxes: \hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue}; no boxes and no colour: \usepackage[hidelinks]{hyperref}.
  • URL font and line breaks: \urlstyle{same} for the text font, \usepackage{xurl} after hyperref for long URLs.

Start with the complete example, then adjust package order and link appearance for your document class.

LaTeX hyperref Package Order: Near the End, Not Always Last

Load hyperref after most packages, then its extensions. Put options in square brackets or a \hypersetup call:

\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{booktabs}
% ... all other packages ...
\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  linkcolor=blue,
  citecolor=blue,
  urlcolor=blue,
}

Packages loaded after hyperref include cleveref, glossaries, bookmark, hypcap and algorithm (from the algorithms bundle). Check the package and class documentation for exceptions.

LaTeX Links to Websites: \href, \url and \nolinkurl

The three commands for external links differ in what they print and whether it is clickable:

% Print the address and link it
\url{https://www.useoctree.com/learn}

% Link arbitrary text to an address
\href{https://www.useoctree.com/learn}{the Octree learning hub}

% Print the address in URL style, but do not make it a link
\nolinkurl{https://www.useoctree.com/learn}
  • \url{...} typesets the address verbatim in a monospaced font and links it. Special characters such as %, #, ~, & and _ can be typed directly.
  • \href{URL}{text} shows text and links to URL. The text is ordinary LaTeX, so it can contain formatting, math or an image.
  • \nolinkurl{...} is \url without the link, useful for printed documents or for showing an address inside \href: \href{https://example.org}{\nolinkurl{example.org}}.

Without hyperref, the url package alone provides \url (no link, just correct typesetting). hyperref loads url for you.

Links to local files and relative addresses

\href is not limited to the web. A file: address opens another document, and run: asks the viewer to launch a file with its default application. Whether these work depends on the PDF viewer and its security settings, so use them only for documents that stay on one machine or intranet:

\href{file:./appendix.pdf}{the appendix}
\href{run:./analysis.xlsx}{the spreadsheet}

% A target without a scheme is treated as a local file, not a web URL
\href{appendix.pdf}{the appendix}   % same as file:appendix.pdf

For a document you will email or upload, prefer a full https:// address.

Line-Breaking Long URLs

Default URL breaks exclude hyphens and long alphanumeric runs, which can cause overfull lines. Three fixes:

% 1. Allow breaks at hyphens: pass the option to url BEFORE hyperref
\usepackage[hyphens]{url}
\usepackage{hyperref}

% 2. Add extra break points yourself (after loading hyperref)
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}   % also allow breaks after - * ~ ' "
\makeatother

% 3. Let URLs break anywhere: load xurl after hyperref
\usepackage{hyperref}
\usepackage{xurl}

xurl is the simplest modern answer and works with pdfLaTeX, XeLaTeX and LuaLaTeX. The older breakurl package is only needed for the latex → dvips → ps2pdf route, where links otherwise cannot span a line break; do not load it with pdfLaTeX.

URL font

\url uses a typewriter font. Change it for the whole document with \urlstyle:

\urlstyle{same}  % same font as the surrounding text
\urlstyle{tt}    % typewriter (default)
\urlstyle{rm}    % roman
\urlstyle{sf}    % sans-serif

Link to a Section, Figure or Equation: \hyperref, \autoref and \nameref

With hyperref loaded, every \ref and \pageref becomes a link to the corresponding \label. hyperref adds three more commands, and cleveref adds \cref:

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}
\section{Introduction}
\section{Method}\label{sec:method}

See Section~\ref{sec:method}.            % "Section 2" - only the number is a link
See \autoref{sec:method}.               % "section 2" - name + number, all linked
See \nameref{sec:method}.               % "Method" - the section title, linked
See \hyperref[sec:method]{the method}.  % your own link text

See \cref{sec:method}.                  % "section 2"
See \Cref{sec:method}.                  % "Section 2"
\end{document}

\autoref takes its words from macros such as \sectionautorefname and \figureautorefname. The default for sections is lowercase; to get “Section 2” add \renewcommand{\sectionautorefname}{Section} after loading hyperref. cleveref is more flexible (ranges, lists, capitalisation) and is the better choice for a new document.

Anchors for unnumbered sections: \phantomsection

With standard classes, hyperref creates an anchor for \section*, but no section number. Put \addcontentsline after the heading so it uses that target. Use \nameref or custom link text instead of expecting a numbered \ref. For a custom heading that does not create an anchor, insert \phantomsection at the intended destination before adding the contents entry:

\clearpage
\section*{Acknowledgements}\label{sec:ack}
\addcontentsline{toc}{section}{Acknowledgements}
See \hyperref[sec:ack]{the acknowledgements}.

Arbitrary targets: \hypertarget and \hyperlink

To link to a spot that has no counter at all, place a named target and link to it by name:

\hypertarget{data-note}{The raw data were collected in 2025.}
...
As noted \hyperlink{data-note}{earlier}, the data are recent.

Links in the table of contents

In the table of contents only the section title is linked by default. Make the page number clickable too with linktoc=all, or only the page number with linktoc=page:

\hypersetup{linktoc=all}

Link Colours: colorlinks, hidelinks and Removing the Boxes

Out of the box hyperref draws a colored rectangle around every link: red for internal links, green for citations, cyan for URLs. The boxes are PDF annotations – they show up in viewers but are never printed. Still, most people want them gone. Pick one of the following:

% Option A: no boxes, no colors - links are invisible but still clickable
\usepackage[hidelinks]{hyperref}

% Option B: colored link text instead of boxes (this color DOES print)
\usepackage{xcolor}
\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  linkcolor=blue,       % internal links: \ref, ToC, footnotes
  citecolor=teal,       % \cite
  urlcolor=magenta,     % \url and \href
  filecolor=magenta,    % links to local files
}

% Option C: keep boxes but make them invisible
\hypersetup{pdfborder={0 0 0}}

% Option D: black link text both on screen and in print
\hypersetup{colorlinks=true, allcolors=black}

A fourth style, common in theses, is an underline instead of a box or a color: \hypersetup{pdfborderstyle={/S/U/W 1}} draws a one-point line under each link. Like the boxes, it is an annotation and does not print.

allcolors sets every link type at once. For a document that will be printed, the usual choice is colorlinks=true with dark, readable colors, or hidelinks if the links should not be visible on paper at all. Colors can be any xcolor name or definition, e.g. \definecolor{linkblue}{RGB}{0,51,153}.

PDF Metadata and Bookmarks with \hypersetup

hyperref also writes the PDF's document properties and the bookmark panel. Set them once in the preamble:

\hypersetup{
  pdftitle={Fast Hyperlinks in LaTeX},
  pdfauthor={Ada Lovelace},
  pdfsubject={A guide to the hyperref package},
  pdfkeywords={LaTeX, hyperref, PDF},
  pdfpagemode=UseOutlines,   % open with the bookmark panel visible
  bookmarks=true,
  bookmarksopen=true,        % expand the bookmark tree
  bookmarksnumbered=true,    % show section numbers in bookmarks
  unicode=true,              % allow non-ASCII characters in metadata
}

If you would rather reuse \title and \author, load hyperref with the pdfusetitle option. Other useful values for pdfpagemode are UseNone, UseThumbs and FullScreen. The bookmark tree is built from the .out file, so it needs two compilation runs to be complete.

Email Links

An email address is just a URL with the mailto: scheme. Use \href for the link and, if you want the address in the URL font, \nolinkurl for the visible text:

\href{mailto:ada@example.edu}{ada@example.edu}
\href{mailto:ada@example.edu}{\nolinkurl{ada@example.edu}}
\href{mailto:ada@example.edu?subject=Question%20about%20the%20paper}{Email the author}

Links in Section Titles, Captions and Footnotes

Section titles and captions are written to the table of contents and to PDF bookmarks. Bookmarks are plain strings: no math, no formatting, no links. hyperref warns “Token not allowed in a PDF string” and drops the offending tokens. \texorpdfstring gives two versions, one for TeX and one for the bookmark:

\section{Results for \texorpdfstring{$\alpha > 1$}{alpha > 1}}
\section{The \texorpdfstring{\href{https://ctan.org}{CTAN}}{CTAN} archive}

% In a caption, URLs with special characters can break: use a short caption
\caption[Data source]{Data from \protect\url{https://example.org/data}}

% Inside a footnote \url and \href work when the address has no special
% characters (for % # ~ & _ see the next section)
Details are online.\footnote{\url{https://example.org/details}}

Special Characters in URLs: % and #

Typed directly in the body of the document, \url and the first argument of \href both accept %, #, ~, & and _ as they are. The exception is when the command sits inside the argument of another command (a \footnote, a \newcommand definition, a \multicolumn or tabularx cell). There the characters are read by TeX before \url sees them. For \href the fix is simple: hyperref accepts the escaped forms \%, \#, \&, \_ and \~ in the URL argument. For \url in such a place, define the address once with \urldef and use the resulting command:

% Fine in running text:
\url{https://example.org/~ada/report_v2.pdf#page=3}
\href{https://example.org/search?q=latex&lang=en}{search}

% Inside another command's argument, escape in \href:
\newcommand{\homepage}{\href{https://example.org/\~ada/a\_b.html\#top}{home}}
Data at 100\% quality.\footnote{\href{https://example.org/?x=1\&y=2}{source}}

% For \url, pre-define the address with \urldef (in the preamble or body)
\urldef{\datalink}\url{https://example.org/~ada/report_v2.pdf#page=3}
\footnote{See \datalink.}

hyperref with Beamer

Beamer loads hyperref itself. Loading it again with options causes an option clash; instead, pass options through the class or with \hypersetup:

\documentclass[hyperref={colorlinks=true, urlcolor=blue}]{beamer}
% or, in the preamble:
\hypersetup{colorlinks=true, urlcolor=blue}

The same applies to other classes and packages that load hyperref for you. A general escape hatch is \PassOptionsToPackage{hidelinks}{hyperref} placed before \documentclass.

Common Errors

MessageCauseFix
Package cleveref Error: cleveref must be loaded after hyperref!Load order: cleveref came firstMove \usepackage{cleveref} below \usepackage{hyperref}
Links missing or \autoref names wrong after adding a packageA package loaded after hyperref redefined \section, \caption or \footnoteLoad hyperref last (except cleveref, glossaries, bookmark, hypcap, algorithm)
Illegal parameter number in definition of \Hy@tempa# in a \url inside \footnote or another command's argumentUse \href{...\#...}{text} or \urldef (see special characters)
File ended while scanning use of \@footnotetext% in a URL inside a footnote starts a comment and swallows the closing braceEscape it as \% in \href, or \urldef the address
Overfull \hbox, URL sticks into the marginThe URL has no allowed break point\usepackage{xurl} after hyperref
Token not allowed in a PDF string (Unicode): removing `math shift'Math or a command in a section title that goes into the bookmarksWrap it in \texorpdfstring{TeX}{plain text}
destination with the same identifier (name{page.1}) has been already used, duplicate ignoredTwo pages share the same number, e.g. the title page and the first page of the bodyUse \pagenumbering to switch styles (roman front matter, arabic body), or \hypersetup{pageanchor=false} around the unnumbered pages
Option clash for package hyperrefhyperref already loaded by the class or another package with different optionsRemove the options from \usepackage and use \hypersetup, or \PassOptionsToPackage before \documentclass
Rerun to get /PageLabels entry or outlines (.out) file has changedLinks and bookmarks need a second passCompile again
Links jump to the wrong placeMissing anchor, often an unnumbered section or a floatAdd \phantomsection; put \label after \caption
Link text is cut off at a line breakUsing latex + dvipsLoad breakurl after hyperref, or switch to pdfLaTeX

Complete Example

\documentclass{article}
\usepackage[hyphens]{url}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{cleveref}

\hypersetup{
  colorlinks=true,
  linkcolor=blue!60!black,
  citecolor=green!50!black,
  urlcolor=magenta!70!black,
  linktoc=all,
  pdftitle={Hyperlinks in LaTeX},
  pdfauthor={Ada Lovelace},
  pdfkeywords={LaTeX, hyperref},
  bookmarksnumbered=true,
}
\renewcommand{\sectionautorefname}{Section}
\urlstyle{same}

\title{Hyperlinks in LaTeX}
\author{Ada Lovelace\\ \href{mailto:ada@example.edu}{\nolinkurl{ada@example.edu}}}

\begin{document}
\maketitle
\tableofcontents

\section{Introduction}\label{sec:intro}
The \texttt{hyperref} manual lives on
\href{https://ctan.org/pkg/hyperref}{CTAN}; the plain address is
\url{https://ctan.org/pkg/hyperref}. Results follow in \cref{sec:results}.

\section{Results}\label{sec:results}
As promised in \autoref{sec:intro}, see \nameref{sec:intro} and
\hyperref[sec:intro]{the opening section}.

\clearpage
\section*{Acknowledgements}
\label{sec:ack}
\addcontentsline{toc}{section}{Acknowledgements}
Thanks to everyone who reads the manual before asking.
See \hyperref[sec:ack]{the acknowledgements}.
\end{document}

Compile twice, then test the contents and “Results” links in a PDF viewer. Compilation does not check external websites. Unresolved references need consistent labels and the auxiliary files from the previous run.

Quick Reference

TaskCommand
Clickable URL\url{https://...}
Link with custom text\href{https://...}{text}
URL without a link\nolinkurl{https://...}
Email link\href{mailto:a@b.org}{a@b.org}
Link to a label with own text\hyperref[label]{text}
“Section 2” with link\autoref{label} or \Cref{label} (cleveref)
Section title as link\nameref{label}
Named anchor\hypertarget{name}{text} / \hyperlink{name}{text}
Anchor for \section*\phantomsection before \addcontentsline
Remove the colored boxes\usepackage[hidelinks]{hyperref}
Colored link text\hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue}
Clickable page numbers in ToC\hypersetup{linktoc=all}
PDF title and author\hypersetup{pdftitle={...}, pdfauthor={...}}
Break long URLs anywhere\usepackage{xurl} after hyperref
URL in the text font\urlstyle{same}
Math in a bookmarked title\texorpdfstring{$x$}{x}

Frequently Asked Questions

How do you add a link in LaTeX?

Load \usepackage{hyperref} near the end of the preamble, before packages such as cleveref and bookmark that extend it. Then \href{https://example.org}{link text} links any text to an address, and \url{https://example.org} prints the address itself as a clickable link. With hyperref loaded, \ref, \cite and the table of contents become clickable too.

What is the difference between \url and \href in LaTeX?

\url{address} prints the address in a typewriter font and links it. \href{address}{text} prints only the text you give and links that text to the address. Use \url when readers of a printed copy need to see the address, and \href when the link text should read as part of the sentence.

How do I link to a section or figure in LaTeX?

Put \label{sec:intro} after the \section command (or after \caption in a figure), load hyperref, and refer to it with \ref{sec:intro}, \autoref{sec:intro} for "section 1" with the word included in the link, or \hyperref[sec:intro]{any text} to choose the link text yourself. Compile twice so the references resolve.

How do I remove the red boxes around links in LaTeX?

Load hyperref with \usepackage[hidelinks]{hyperref} for clickable links with no boxes and no colour, or set \hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue} to colour the link text instead of drawing boxes. The boxes never appear in print either way; they only show in PDF viewers.

Why does a URL with % or # break inside a footnote?

Inside the argument of another command, such as \footnote, TeX reads % as the start of a comment and # as a macro parameter before \url sees them, giving errors like "Illegal parameter number in definition of \Hy@tempa" or "File ended while scanning use of \@footnotetext". Escape the characters in \href (\%, \#), or define the address in advance with \urldef{\mylink}\url{...} and use \mylink in the footnote.

How do I break a long URL across lines in LaTeX?

Load the xurl package after hyperref, \usepackage{xurl}, and URLs can break after any character. For a lighter touch, \usepackage[hyphens]{url} before hyperref allows breaks at hyphens as well as at slashes and dots.

Next Steps

Free LaTeX Tools

Fix hyperref problems faster with AI

Open the example in Octree, compile it, and use AI chat to ask about package order or a warning from your document. Check the resulting PDF links before sharing it.

Try Octree Free →