[Tex/LaTex] (Another) problem with url hyphenation in bibliography

biblatexbibtexhyphenationurls

What I am about to ask has already been asked many times, but nothing seems to work for me so far. Basically I am having troubles in getting urls hyphenated correctly in the bibliography.

I modified the command \UrlBreaks so that it allows hyphenation everywhere, but this seems to be ignored in the bibliography, while in the rest of the document it does work.

Here is a MWE:

% !TEX encoding = UTF-8
% !TEX program = xelatex
\documentclass[12pt,english,a4paper]{article}%
\usepackage[backend=bibtex,defernumbers=true,sorting=nty]{biblatex}%
\usepackage[breaklinks,plainpages=false]{hyperref}%
\def\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\do0\do1\do2\do3\do4\do5\do6\do7\do8\do9\do=\do/\do.\do:}
\addbibresource{Biblio.bib}
\begin{document}
See \cite{citation} for further details or visit \url{http://www.joifewoirjecpwoirjcpewrijcpenwrjcpnewrjicpnwerjincpejricpejripcne.com}.
\printbibliography
\end{document}

and the references are in the file Biblio.bib as follows

@book{citation,
author = {Doe, John},
title = {The Book},
url = {http://www.joifewoirjecpwoirjcpewrijcpenwrjcpnewrjicpnwerjincpejricpejripcne.com}
}

Compiling with xelatex (or pdflatex) and bibtex results in the following output:
Compiled with xelatex

A curiosity (hope it helps in finding the bug): the image above is what comes out compiling with xelatex; compiling with pdflatex gives the same output, except for the cyan countour line of the link which, ironically, gets hyphenated:
Compiled with latex

Using \sloppy does something better, but still doesn't make the links hyphenate where I want, i.e. anywhere:
With sloppy option

As you may imagine, \raggedright doesn't solve the issue either.

What I am looking for is some code that allows a good hyphenation of the urls in the bibliography with these constraints (I don't know if they are relevant):

  1. it has to work with xelatex and bibtex (backend=biber option to biblatex doesn't work for me somehow)
  2. package polyglossia must not be required, rather it has to work with babel
  3. compatible with package hyperref
  4. compatible with book document class

Best Answer

biblatex has a number of counters which when set to non-zero positive values less than 10000 allow breaks in urls in the bibliography. The specific counters are

biburlnumpenalty
biburlucpenalty
biburllcpenalty

for breaks after numbers, uppercase letters and lowercase letters, respectively. In your case it suffices to set the lowercase penalty to 9000; lower values encourage breaks more (or rather discourage them less). The setting is done value via \setcounter:

Sample output

\documentclass[12pt,english,a4paper]{article}%
\usepackage[backend=bibtex,defernumbers=true,sorting=nty]{biblatex}%
\usepackage[breaklinks,plainpages=false]{hyperref}%
\def\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\do0\do1\do2\do3\do4\do5\do6\do7\do8\do9\do=\do/\do.\do:}
\setcounter{biburllcpenalty}{9000}
\addbibresource{Biblio.bib}
\begin{document}
See \cite{citation} for further details or visit \url{http://www.joifewoirjecpwoirjcpewrijcpenwrjcpnewrjicpnwerjincpejricpejripcne.com}.
\printbibliography
\end{document}

When choosing values for these penalties it is useful to note that biblatex's defaults are

\UrlBigBreakPenalty   100

for breaks after : and -, and

\UrlBreakPenalty      200

for breaks after most punctuation characters. You want to set the other penalties significantly higher, so that breaks after numbers and letters are of a lower prioriy than those after punctuation marks. If you need to change the two \Url...BreakPenaltys above then it is via the unusual syntax of

\mathchardef\UrlBreakPenalty=200\relax