[Tex/LaTex] URL linebreaks W/O \sloppy

line-breakingurls

Hours of reading the different articles on url linebreaking did not reveal a solution for the following problem:

I've got a long url, which I do not intend to short using online services. The url package and pdfLaTeX output is used. The line break occurs at a place, that looks ugly and that I cannot understand, why a tool that fortunately takes proper layout so serious, wraps here.

My MWE is:

\documentclass[10pt,a5paper]{book} % Regular text font size is 10 pt.

\usepackage{lipsum} % load paragraphs of filler text

\setlength{\oddsidemargin}{20mm}
\setlength{\textwidth}{108.5mm}

\usepackage{url}
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\g@addto@macro{\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\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\0\do\.}
\makeatother

\begin{document}

\lipsum[2]

Available: \url{http://www.aaa.bb.cccc.dh/uploads/dfjasdasdfsadfsdfklfjskldfjssdfasdfasdfdfsdf/Psdfsdafresentationskdjf_sdfsad_sdf_sdf_sadfasdf.pdf}

\end{document}

PDF output

As can be seen, the "d" in the second last line is already overlength (compared to the paragraph above), still LaTeX adds the "f" instead of wrapping after the "d".

I fully understand, that the second last line cannot be perfectly justified as there are no spaces that can be enlarged (I've read about a package that enlarges the letters in these cases) – my question is just, why LaTeX makes things even worse with adding the "f" instead of making it the first letter in the last line, and what I can do about that without using the sloppy workaround, which makes the rest look far worse than just wrapping before the "f"?

Best Answer

I would say the best way is to reduce the places where an url can be broken.

Your given MWE reduced to only break the url after character d runs without problems and the wanted result on my MiKTeX 2.9:

\documentclass[10pt,a5paper]{book} % Regular text font size is 10 pt.

\usepackage{lipsum} % load paragraphs of filler text

\setlength{\oddsidemargin}{20mm}
\setlength{\textwidth}{108.5mm}

\usepackage{url}
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\g@addto@macro{\UrlBreaks}{%
\do\/\do\d%
}
\makeatother

\begin{document}

\lipsum[2]

Available: \url{http://www.aaa.bb.cccc.dh/uploads/dfjasdasdfsadfsdfklfjskldfjssdfasdfasdfdfsdf/Psdfsdafresentationskdjf_sdfsad_sdf_sdf_sadfasdf.pdf}

\end{document}

enter image description here