# Line breaks and the URL package

Long URLs need to be broken at the end of a line. Here's a topic about that. That topic suggests adding the hyphens option to the URL package, and also to define

\renewcommand{\UrlBreaks}{\do\/\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z}


I've done both, but it still doesn't work. Below is a copy of an example. Any ideas?

\documentclass[10pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[swedish,english]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage[hyphens]{url}
\usepackage{hyperref}
\renewcommand{\UrlBreaks}{\do\/\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z}

\usepackage{fixmath}
\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{color}
\usepackage[small,font=it]{caption}
\usepackage{amssymb}
\usepackage{icomma}
\usepackage{verbatim}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\footrulewidth}{0pt}

\setlength{\parindent}{0pt}
\setlength{\parskip}{2ex}

\begin{document}

This is a test. Kurzweil says technology increases exponentially (\url{http://www.kurzweilai.net/the-law-of-accelerating-returns}).

\end{document}


EDIT: I am using latex and then dvipdf to compile.

-
It works fine for me without any special considerations. It splits at http://|split|www.----- – Sean Allred Mar 6 '13 at 19:30
I use latex and then dvipdf if that matters. I cannot understand why it would work for some and not others. – Calle Mar 6 '13 at 20:03
oh goodness. yep, that's it. ... that is weird. I would put your build pattern in the question, since it seems to be relevant. – Sean Allred Mar 6 '13 at 20:10
dvips/ps2pdf produces the same result. It may be that pdflatex is the only one that works here. The DVI also is messed up; so there has got to be some rationale for this behavior (although it looks like a lucky bug in pdflatex). – Sean Allred Mar 6 '13 at 20:13
Yes, I got it to work with pdflatex. Thank you very much, if you want to post that as a solution I could accept it. Odd though, I only ever noticed the difference when dealing with images before. – Calle Mar 6 '13 at 20:17

I'm not sure what is causing the problem in the first place, but pdflatex gives the desired output without any fuss whatsoever. The issue seems to be reproducible only with a latex dvipdf or latex dvips ps2pdf build pattern. The DVI produced by latex exhibits this issue (viewed under xdvi), so it is no wonder that the trailing build processes fail.

A better solution must yet exist, but using pdflatex in the first place circumvents the issue for the most part. (The hyphenation patterns leave much to be desired.)

latex output: (DVI screenshot viewed under xvdi; the red line is the page edge)

pdflatex output:

-

I finally found a solution, that might be of interest to all the ones having problems with displaying long URLs in LaTeX: I had a little chat with Vilar (the maintainer of the breakurl-package) and he was able to extend the breaking possibilities of his package they way I suggested.

Therefore the new option anythingbreaks in the newest version does the links look good as they keep in range of the textborder.

See for yourself:

Completely without:

Standard behavior:

Anythingbreaks:

Look also at my question here: http://tex.stackexchange.com/a/108001/28200

-

One can fix this by loading the package breakurl after hyperref (which should also be loaded later in the preamble):

...
\usepackage{hyperref}
\renewcommand{\UrlBreaks}{\do\/\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\d\o\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z}
\usepackage{breakurl}


With this addition, on the given test file, the .dvi breaks right after http://www., as it does in the .pdf made with dvipdf.

-

This isn't an automated solution, but I currently use a macro that manually breaks URLs up:

\documentclass{memoir}
\usepackage{lipsum}
\usepackage{hyperref}

\newcommand*{\brokenurl}[2]{\href{#1#2}{\texttt{#1}}\par\nopagebreak\href{#1#2}{\texttt{#2}}}
\newcommand*{\brokenurlwithoutpar}[2]{\href{#1#2}{\texttt{#1}}\\*\href{#1#2}{\texttt{#2}}}

\begin{document}

Kurzweil says technology increases exponentially (\url{http://www.kurzweilai.net/the-law-of-accelerating-returns}).

Kurzweil says technology increases exponentially (\brokenurl{http://www.kurzweilai}{.net/the-law-of-accelerating-returns}).

Kurzweil says technology increases exponentially (\brokenurlwithoutpar{http://www.kurzweilai}{.net/the-law-of-accelerating-returns}).

\lipsum[1]

\end{document}


This "solution" of mine is an occasional last resort, when nothing else helps. Yes, it's obvious, it's not deep, and it doesn't use any tricks - but every so often it's the only thing that seems to work. Note the difference between \brokenurl (with \par\nopagebreak) and \brokenurlwithoutpar (with \\*); when you set something like \parindent0pt, the two behave nearly the same (it's in fact hard to construct an example where their right-alignment behavior is different). Now it occurs to me that defining the macro with \par\nopagebreak\noindent instead of just \par\nopagebreak would avoid any need for \parindent0pt, but then, depending on the context you're in (say, a bibliography), you might intentionally want an indented continuation of a broken URL, to make it clear that the item is a continuation of the previous line; it depends very much on the formatting and style.

Note that my method works smoothly even if you break within words in arbitrary places (eg, \brokenurl{http://www.kurz}{weilai.net/...}).

-