Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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"?

share|improve this question

1 Answer 1

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

share|improve this answer
    
Thanks Kurt, it looks though, that this is a rather unreliable workaround. Say, I got two other urls in my document, which I need to break after "f" and "s", so I include only those three into the \UrlBreaks command (such as \g@addto@macro{\UrlBreaks}{\do\d\do\f\do\s}), it fails again. –  Ben Aug 4 '13 at 20:12
    
Well @Ben, I would put the urls into a bib entry, use \RaggedRight (package ragged2e) for the bibliography. Then you can play with the necceccary \dos ... If I remember correctly, there was an question/answer for this. Must look for it ... –  Kurt Aug 4 '13 at 21:23
    
Yes, I guess you are referring to your post here. The thing is just: If I use your suggestion with \usepackage[newcommands,newparameters]{ragged2e}, then the whole paragraph is ragged right (there is more text before the "Available"), which looks awful. So, why does LaTeX not break after the "d", even if it is explicitly told that this is possible and even though the hbox is already overfull? –  Ben Aug 5 '13 at 9:41
    
@Ben My point is to move the url in the bibliography. A bibliography should use left justifing. If you have several urls you have to play with the order of the \dos. Lacking an example with more than one url I can only guess, but changing the \do\d\do\f\do\s to \do\s\do\d\do\s or something else could do the trick. It is only a workaround ... To solve problems like this it is in my opinion the best to use not a left-right-justifing ... –  Kurt Aug 5 '13 at 10:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.