How to Write URLs and Hyperlinks in LaTeX (url, hyperref, xurl)

Updated Tested on TeX Live 2025 (pdfLaTeX), Oct 2026 5 min read

Quick answer

To write a URL in LaTeX, load hyperref and use \url{https://example.com} for a clickable link in typewriter font, or \href{https://example.com}{link text} to show your own text. Characters like _, % and # need no escaping inside \url. For long URLs that overflow the margin, add \usepackage{xurl} to allow breaks anywhere.

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

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.

Compiled LaTeX sentence ending with the URL https://www.example.com in typewriter font

If you need the formatting and line breaks but not clickable links, for example for print only, load \usepackage{url} instead of hyperref.

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
\href{mailto:[email protected]}{[email protected]} % email link

Compiled LaTeX paragraph with clickable link text created by the href command

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.

CommandPackageWhat it shows
\url{…}url or hyperrefThe URL, clickable with hyperref
\href{url}{text}hyperrefYour text, linked to the URL
\nolinkurl{…}hyperrefThe URL, formatted but not clickable
\href{mailto:…}{…}hyperrefAn email link
\urldef{\cmd}\url{…}urlA 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. Writing my\_page prints 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.

  1. Add \usepackage{xurl} to the preamble, before hyperref.
  2. 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: \sloppy doesn’t help with URLs. It only loosens the spacing between words; it doesn’t add break points inside a URL.

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}

Compiled LaTeX document with a bold, coloured href link to example.com

OptionEffect
colorlinks=trueColours the link text instead of drawing boxes
urlcolor=blueColour for \url and \href links
linkcolor=blackColour for internal links such as \ref and the table of contents
citecolor=…Colour for citation links
hidelinksNo 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.

Compiled LaTeX sentence showing a URL inserted through a command defined with urldef

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
Send emails to \email{[email protected]}.
Check \website{www.example.com} for updates.

Compiled LaTeX text with an email address in roman font and a web address in sans-serif font

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 url field, and a DOI in the doi field.
  • 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 colorlinks or hidelinks.
  • “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.

Frequently asked questions

How do I make a URL clickable in LaTeX?

Load the hyperref package. Every \url and \href then becomes a clickable link in the PDF.

How do I break a long URL in LaTeX?

Add \usepackage{xurl} to the preamble. It lets URLs break between any characters, so long links no longer run into the margin.

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

Use \hypersetup{hidelinks} to keep links clickable but invisible, or \hypersetup{colorlinks=true, urlcolor=blue} to colour the link text instead of boxing it. The boxes never print on paper.

Do I need to escape underscores in \url?

No. Inside \url, type the address exactly as it is; \_ would print a literal backslash and break the link. Only % and # need \% and \# when \url sits inside another command, such as \footnote.

How do I make an email link in LaTeX?

Use \href{mailto:[email protected]}{[email protected]}. Clicking it opens the reader's email program with the address filled in.