I'm struggling with hyphenation of long URLs (using command \url{}).
Line breaking works fine in cases when URL is just part of line which doesn't fit to text area width. That means URL is hyphenated correctly if line starts with some text before URL - or if hyphenated URL is followed by other text which is also hyphenated.
Problem is in parts of URL which are at the line alone
first line of very long URL
middle lines of very long hyphenated URL
At screenshot there is visible that both cases overflowed the text area - case 1 is clearly visible, but also case 2 overflowed by few pixels.
In real document overflowed area is variable depending on contents which makes sometimes collisions with objects on the right side (images, ...).
How to hyphenate URLs correctly to avoid such overflows?
ideal solution is perfectly aligned block
but acceptable is also solution with underflowed line instead of overflowed (earlier line break instead of late)
I guess problem is mainly caused by non-proportional font and is fixed if other proportional text is present at the same line, but how to handle alone parts of long URLs?
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage[unicode]{hyperref}
\def\UrlBreaks{\do{a}} % Force correct hyphenation for this example
\usepackage{pdfpages}
\usepackage{needspace}
\begin{document}
Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text.\newline
\url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}\newline
Some text before. \url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after.
\end{document}
Shorter MWE
\documentclass[a4paper]{book}
\usepackage{hyperref}
\def\UrlBreaks{\do{a}}
\begin{document}
Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text.\newline
\url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}\newline
Some text before. \url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after.
\end{document}
Edit 18-12-23: Additional issue
I've tried suggested solution:
\def\do#1{\appto\UrlSpecials{\do#1{\mathchar`#1 \mskip 0mu plus 1mu\penalty100}}}%
\do\a
and initially everything was perfect. But I wasn't happy for long time :-) After I'd replaced the dummy URL with the real, document couldn't be even compiled - Number too big error, see details in screenshot.
Problem is only with combination "suggested solution" and "real URL". Old method and real URL is OK (only with wrong breaks, which was reason for this question) and suggested solution with dummy URL works also fine. It has definitely something to do with special characters in URL.
Full code:
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage[unicode]{hyperref}
% Old method - no error, but issues with breaks
%\def\UrlBreaks{\do\-\do\/\do\0\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\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}
% New method - error for real URL (previous example with "aaa" works)
\def\do#1{\appto\UrlSpecials{\do#1{\mathchar`#1 \mskip 0mu plus 1mu\penalty100}}}
\do\-\do\/\do\0\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\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{pdfpages}
\begin{document}
Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text.\newline
%\url{http://aaa.bbb.ccc/njrrjjriokf56opiyfl5ojiorij6m9h9r87vjvjytrjyiotryhjiothjvoi8jr8o7j8hohj8oj88jt6hj8ohjot6trhiothj8hj8ohitnhh8tr7t786ht76nvh6gtrhgoght6onbvgnhyg8o7hvntnvotr696yjvhopr76r}\newline
\url{http://www.portafontium.cz/iipimage/30063568/karlovy-vary-65\_0240-t?x=-45\&y=345\&w=927\&h=383}\newline
\end{document}





