Line Breaking – Forcing Linebreaks in \url in LaTeX

line-breakingurls

This question led to a new package:
xurl

I wish to typeset some relatively long URLs in a piece of text, and when I use \url{..}, the resulting text does not respect the margin boundaries that govern the main text body, instead going all the way to the edge of the paper before wrapping around.

For reference, my preamble looks like this:

\documentclass[12pt]{article}
\usepackage{mathptmx}
\usepackage{fullpage}
\usepackage[pdftex]{hyperref}

and example of the url I'm including is:

\begin{itemize}
\item Some text

\url{very-long-url}
...
\end{itemize}

I'm compiling using pdflatex, although I doubt this makes a difference.

Best Answer

(converting a previous comment to an answer)

A quick google search (alas, too quick) reveals one solution. Use the [hyphens] option with the url package:

\PassOptionsToPackage{hyphens}{url}\usepackage{hyperref}

Some recommendations also include inserting \sloppy in case latex is trying too hard to align things.