The url package
Typesets web addresses, e-mail addresses and file paths with \url and \path: special characters are typed as they are, and long addresses break across lines.
Loading the package
\usepackage{url}An address typed as normal text causes trouble twice: characters such as _, %, #, & and ~ have a special meaning in LaTeX, and TeX does not break the address at the end of a line. Inside \url{...} these characters are printed as typed, and the address may break after slashes, dots and similar characters, without a hyphen.
The package only typesets the address. With hyperref loaded, which loads url itself, \url also becomes a clickable link.
Main commands
\url{https://example.org/a_b?x=1&y=2} | An address in typewriter type. |
\path{C:\Users\name\file_1.tex} | The same for file paths; with hyperref it does not become a link. |
\urlstyle{same} | Font of the addresses: tt (default), rm, sf or same for the font of the surrounding text. |
\urldef{\shopurl}\url{https://example.org/50%_off} | Stores an address in a command that is safe in headings, captions and footnotes. |
\usepackage[hyphens]{url} | Allows line breaks after hyphens as well. |
\DeclareUrlCommand\email{\urlstyle{sf}} | Defines a command of your own that works like \url with other settings. |
Complete example
\documentclass{article}
\usepackage[hyphens]{url}
\urldef{\shopurl}\url{https://example.org/sale?discount=50%&code=A_1}
\begin{document}
\section{Sources}
The data set is at \url{https://example.org/data/2026/results_final.csv}.
The settings are stored in \path{C:\Users\name\project\config_1.ini}.
An address with a percent sign works in a footnote through a stored
command.\footnote{\shopurl}
\urlstyle{same}
In the text font: \url{https://example.org/~user/index.html}
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- A
%or#inside\urlfails when the\urlstands in the argument of another command such as\footnote,\sectionor\caption: the%starts a comment and the run stops with File ended while scanning use of \@xdblarg. Define the address with\urldefin the preamble and use the new command. - An address without a permitted break point is not broken and causes Overfull \hbox (263.664pt too wide) in paragraph at lines 3--6. The option
hyphensadds break points after hyphens. - When
hyperrefis loaded, pass the option before it:\PassOptionsToPackage{hyphens}{url}ahead of\usepackage{hyperref}, or loadurlwith the option first. - Spaces inside
\urlare ignored unless the optionobeyspacesis set.