Getting Started
Text Formatting
Mathematical Expressions
Document Structure
Page Layout
Errors & Troubleshooting
Hyperlinks in LaTeX: \href, \url and the hyperref Package
Load hyperref, then \href{url}{text} for a link with custom text and \url{...} to print the address itself. That covers 90% of link needs. The other 10% — load order, colors, URLs that blow past the margin — is where hyperref bites people, so this page covers those too.
The basics
\url is more than a clickable \texttt: it switches off LaTeX's special characters inside its argument, so underscores, tildes, # and & pass through untouched. Never write a raw URL in body text without it.
The load-order rule
hyperref loads last. It redefines the cross-referencing machinery of nearly every other package, so it has to see their definitions first. This single rule fixes a remarkable share of "my references broke" posts. There is a short list of real exceptions that load after hyperref:
cleveref— always after hyperref, or\creflinks will not workglossaries— needs hyperref in place to make glossary entries clickable
Fixing the colored boxes
Out of the box, hyperref draws a colored border around every link: red for internal references, green for citations, magenta for URLs. The borders do not print, but on screen they look like a mistake. Two sane configurations:
Journals mostly want hidelinks. For a thesis read on screen, colorlinks=true with everything set to one restrained color beats the default red/green/magenta carnival.
Underscores, percent signs and other landmines
Inside \url, special characters just work — underscores, tildes, ampersands, percent signs, all of it. The first argument of \href is almost as robust, with one exception: % starts a comment there and must be escaped.
A URL pasted into \texttt or plain text instead of \url is the classic source of Missing $ inserted — the underscore gets read as a subscript command.
Breaking long URLs at the margin
By default \url only breaks after certain characters (/, ., - and a few more). One long path segment and the URL sails into the margin. The fix is the xurl package, which allows a break after any character:
Use xurl. The \UrlBreaks route works but you end up listing the alphabet by hand, which is exactly what xurl already does for you. This matters most in bibliographies and two-column layouts, where the line is short and URLs are long.
Internal links: \ref, \autoref and \cref
Once hyperref is loaded, every \ref, \cite, footnote and table-of-contents entry becomes a link automatically. You write nothing extra. The upgrades worth knowing:
| Command | Output | Comes from |
|---|---|---|
\ref{fig:x} | 3 (number only — you type "Figure") | LaTeX core |
\autoref{fig:x} | Figure 3 (name is part of the link) | hyperref |
\cref{fig:x,fig:y} | Figures 3 and 4 (sorts and compresses lists) | cleveref |
\cref is the strongest of the three once a document has more than a handful of references — it handles plurals, ranges, and mixed types without you retyping "Figure" and "Table" everywhere. Just remember: cleveref loads after hyperref.
PDF metadata and mailto links
The PDF title your reader's viewer shows in its tab is not your \title — it has to be set through \hypersetup:
Set pdftitle for anything you distribute. A PDF whose tab reads the filename or, worse, a leftover template title looks careless, and search indexes read this field.
"Destination with the same identifier" warnings
This warning means two link targets collided. Two causes account for nearly all of them:
- A duplicate
\label— two labels with the same name. Rename one; done. - Unnumbered sectioning: several
\section*commands all anchor to the same counter value, e.g.(page.1).
For the unnumbered-section case, drop a \phantomsection before each starred section (and before its \addcontentsline if you add one), which mints a fresh anchor:
The warning is harmless in the sense that the PDF still compiles — but the links jump to the wrong place, which readers do notice.
Frequently asked questions
How do I add a clickable link in LaTeX?
Load hyperref with \usepackage{hyperref}, then use \href{https://example.com}{link text} for a link with custom text, or \url{https://example.com} to print the address itself as a clickable link.
Why do my LaTeX links have ugly colored boxes around them?
That is hyperref's default: links get a colored border that shows on screen but not in print. Load the package as \usepackage[colorlinks=true]{hyperref} to color the link text instead, or \usepackage[hidelinks]{hyperref} to make links visually invisible while keeping them clickable.
Why does hyperref have to be loaded last?
hyperref redefines large parts of LaTeX's cross-referencing internals, so it must see every other package's definitions first. The main exceptions load after it: cleveref and glossaries both need hyperref already in place. If your links point to the wrong pages or \ref breaks, package order is the first thing to check.
How do I stop a long URL from running into the margin?
Add \usepackage{xurl} after hyperref. It lets \url break at any character, which fixes almost every overfull line caused by a URL. Without xurl, URLs only break at a limited set of characters like / and ., which is often not enough in a narrow column or a bibliography.
Hyperlinks in LaTeX: \href, \url and the hyperref Package
Load hyperref, then \href{url}{text} for a link with custom text and \url{...} to print the address itself. That covers 90% of link needs. The other 10% — load order, colors, URLs that blow past the margin — is where hyperref bites people, so this page covers those too.
The basics
\url is more than a clickable \texttt: it switches off LaTeX's special characters inside its argument, so underscores, tildes, # and & pass through untouched. Never write a raw URL in body text without it.
The load-order rule
hyperref loads last. It redefines the cross-referencing machinery of nearly every other package, so it has to see their definitions first. This single rule fixes a remarkable share of "my references broke" posts. There is a short list of real exceptions that load after hyperref:
cleveref— always after hyperref, or\creflinks will not workglossaries— needs hyperref in place to make glossary entries clickable
Fixing the colored boxes
Out of the box, hyperref draws a colored border around every link: red for internal references, green for citations, magenta for URLs. The borders do not print, but on screen they look like a mistake. Two sane configurations:
Journals mostly want hidelinks. For a thesis read on screen, colorlinks=true with everything set to one restrained color beats the default red/green/magenta carnival.
Underscores, percent signs and other landmines
Inside \url, special characters just work — underscores, tildes, ampersands, percent signs, all of it. The first argument of \href is almost as robust, with one exception: % starts a comment there and must be escaped.
A URL pasted into \texttt or plain text instead of \url is the classic source of Missing $ inserted — the underscore gets read as a subscript command.
Breaking long URLs at the margin
By default \url only breaks after certain characters (/, ., - and a few more). One long path segment and the URL sails into the margin. The fix is the xurl package, which allows a break after any character:
Use xurl. The \UrlBreaks route works but you end up listing the alphabet by hand, which is exactly what xurl already does for you. This matters most in bibliographies and two-column layouts, where the line is short and URLs are long.
Internal links: \ref, \autoref and \cref
Once hyperref is loaded, every \ref, \cite, footnote and table-of-contents entry becomes a link automatically. You write nothing extra. The upgrades worth knowing:
| Command | Output | Comes from |
|---|---|---|
\ref{fig:x} | 3 (number only — you type "Figure") | LaTeX core |
\autoref{fig:x} | Figure 3 (name is part of the link) | hyperref |
\cref{fig:x,fig:y} | Figures 3 and 4 (sorts and compresses lists) | cleveref |
\cref is the strongest of the three once a document has more than a handful of references — it handles plurals, ranges, and mixed types without you retyping "Figure" and "Table" everywhere. Just remember: cleveref loads after hyperref.
PDF metadata and mailto links
The PDF title your reader's viewer shows in its tab is not your \title — it has to be set through \hypersetup:
Set pdftitle for anything you distribute. A PDF whose tab reads the filename or, worse, a leftover template title looks careless, and search indexes read this field.
"Destination with the same identifier" warnings
This warning means two link targets collided. Two causes account for nearly all of them:
- A duplicate
\label— two labels with the same name. Rename one; done. - Unnumbered sectioning: several
\section*commands all anchor to the same counter value, e.g.(page.1).
For the unnumbered-section case, drop a \phantomsection before each starred section (and before its \addcontentsline if you add one), which mints a fresh anchor:
The warning is harmless in the sense that the PDF still compiles — but the links jump to the wrong place, which readers do notice.
Frequently asked questions
How do I add a clickable link in LaTeX?
Load hyperref with \usepackage{hyperref}, then use \href{https://example.com}{link text} for a link with custom text, or \url{https://example.com} to print the address itself as a clickable link.
Why do my LaTeX links have ugly colored boxes around them?
That is hyperref's default: links get a colored border that shows on screen but not in print. Load the package as \usepackage[colorlinks=true]{hyperref} to color the link text instead, or \usepackage[hidelinks]{hyperref} to make links visually invisible while keeping them clickable.
Why does hyperref have to be loaded last?
hyperref redefines large parts of LaTeX's cross-referencing internals, so it must see every other package's definitions first. The main exceptions load after it: cleveref and glossaries both need hyperref already in place. If your links point to the wrong pages or \ref breaks, package order is the first thing to check.
How do I stop a long URL from running into the margin?
Add \usepackage{xurl} after hyperref. It lets \url break at any character, which fixes almost every overfull line caused by a URL. Without xurl, URLs only break at a limited set of characters like / and ., which is often not enough in a narrow column or a bibliography.
Getting Started
Text Formatting
Mathematical Expressions
Document Structure
Page Layout
Errors & Troubleshooting
