URLs are full of characters that mean something to LaTeX, such as _, %, # and ~. The url and hyperref packages read them literally, so you rarely need to escape anything.
What you’ll learn:
- Write a URL with
\url - Add link text with
\href - Break long URLs
- Change link colours
- Reuse and restyle URLs
Write a URL with \url
Use this to show the address itself, as in references and footnotes. Load hyperref, which loads the url package for you and makes the link clickable.
\documentclass{article}\usepackage{hyperref}
\begin{document}
You can visit the following link for more information: \url{https://www.example.com}.
\end{document}Result: the URL prints in a typewriter font and is clickable in the PDF.

If you need the formatting and line breaks but not clickable links, for example for print only, load \usepackage{url} instead of hyperref.
Add your own link text with \href
Use this when the reader should see words, not the address. \href comes with hyperref:
\href{https://www.example.com}{Visit example.com}\href{https://www.example.com}{\textbf{Visit our site}} % bold link text
Result: the link text prints in the normal font and opens the URL when clicked. Keep the address visible as well if the document will be printed.
| Command | Package | What it shows |
|---|---|---|
\url{…} | url or hyperref | The URL, clickable with hyperref |
\href{url}{text} | hyperref | Your text, linked to the URL |
\nolinkurl{…} | hyperref | The URL, formatted but not clickable |
\href{mailto:…}{…} | hyperref | An email link |
\urldef{\cmd}\url{…} | url | A reusable command holding the URL |
Special characters in URLs
Use this when a URL contains _, %, #, & or ~. Inside \url and the first argument of \href, type the URL exactly as it is:
\url{https://www.example.com/my_page?id=10&sort=asc#results}Warning: Don’t escape characters inside
\url. Writingmy\_pageprints a literal backslash,my\_page, and the link breaks.
The exception is a URL inside another command’s argument, such as \footnote or \section. There, % and # must be written as \% and \#; \url still prints them correctly:
\footnote{See \url{https://www.example.com/a_b\%20c\#part}.}For many such URLs, define them once with \urldef (below); the defined command works safely inside footnotes.
Break long URLs with xurl
Use this when a long URL runs into the margin. The url package breaks only at certain characters, such as / and ., never between letters, so a long URL often can’t break where it needs to.
- Add
\usepackage{xurl}to the preamble, before hyperref. - Recompile.
\usepackage{xurl}\usepackage{hyperref}Result: URLs can break between any characters, so they stay inside the text block. The link still works, because the break only affects the printed text.
Other options, if you want more control:
\PassOptionsToPackage{hyphens}{url}before loading hyperref also allows breaks at hyphens.\def\UrlBreaks{…}and\Urlmuskip(url package) fine-tune break points and spacing.
Note:
\sloppydoesn’t help with URLs. It only loosens the spacing between words; it doesn’t add break points inside a URL.
Change link colours and remove boxes
Use this when the default red and cyan boxes around links look wrong. hyperref draws them in the PDF viewer only; they never print. Set link styles with options or \hypersetup:
\usepackage[colorlinks=true, urlcolor=blue, linkcolor=black]{hyperref}% or, later in the preamble:\hypersetup{hidelinks}
| Option | Effect |
|---|---|
colorlinks=true | Colours the link text instead of drawing boxes |
urlcolor=blue | Colour for \url and \href links |
linkcolor=black | Colour for internal links such as \ref and the table of contents |
citecolor=… | Colour for citation links |
hidelinks | No boxes and no colour; links stay clickable |
Load hyperref near the end of the preamble; most packages expect it last. cleveref is the main exception and goes after it, which matters if you use clickable cross-references with \autoref or \cref. Overleaf’s hyperlinks guide covers the remaining options.
Reuse a URL with \urldef
Use this for a URL you cite many times. Define it once in the preamble:
\urldef{\mysite}\url{https://www.mergedocs.pro}Then write \mysite wherever you need it. If the address changes, update one line.

Change the URL font with \DeclareUrlCommand
Use this to give some URL-like text, such as email addresses, a different style. \DeclareUrlCommand from the url package creates a new \url-like command:
\usepackage{url}\DeclareUrlCommand\email{\urlstyle{rm}} % roman font\DeclareUrlCommand\website{\urlstyle{sf}} % sans-serif font
Check \website{www.example.com} for updates.
To change the URL style everywhere, put \urlstyle{same} in the preamble so URLs use the surrounding text font. To change the document’s typewriter font itself, see how to change fonts in a LaTeX document.
URLs in a bibliography
Use your bibliography’s own fields rather than typing \url into a note:
- With biblatex, put the address in the
urlfield, and a DOI in thedoifield. - Prefer DOIs or publisher permalinks to shortened links; short links break if the service shuts down, and many style guides discourage them.
Common errors
- A backslash appears in the printed URL: you escaped a character inside
\url. Remove the backslash. - “Illegal parameter number” or a cut-off URL in a footnote: a
#or%inside a command’s argument. Write\#and\%, or use\urldef. - “Overfull \hbox” at a URL: add
\usepackage{xurl}. - Red or cyan boxes around links: use
colorlinksorhidelinks. - “Option clash for package url”: you loaded url with options after hyperref, which had already loaded it. Load url (or xurl) before hyperref, or use
\PassOptionsToPackage{hyphens}{url}before hyperref.
For code that contains URLs, see how to insert code listings with indentation. The xurl package on CTAN has its short documentation. More tutorials are in the LaTeX guides.
If the document needs to go to Word users, links in \href and \url generally survive when you convert LaTeX to Word with Pandoc.