# set tolerance within a span command only

URLs (and similar, e.g., long class names or folder names) can be annoying to typeset well. This gets even worse when the document is automatically generated, and you cannot "just" insert manual breaks and adjustments.

If I set tolerance very high, margins are okay. But outside of problematic URLs, I would prefer to have the default tolerance apply.

I'd like to do {\tolerance 10000 \url{...}} (i.e. allow "ragged" whitespace at the end of the URL lines only) but tolerance does not take effect until the end of the paragraph, always affects the entire paragraph; not just the URL. I want the tolerance change to only affect the URL; at most the current paragraph, but then revert back to the default value (without having to insert begingroup / endgroup; as the document is automatically generated).

In the example below (first is the default behavior, second is with tolerance=10000), the breaks of the remaining text below are the same. But that supposedly isn't always so; and with lower tolerance in the first line. The third version is what I'd like to get (I manually inserted a \hfill\\ into the URL there). IMHO, the third version is best, as it does not overrun the margin, but also does not overly stretch the first line (the second version is too stretched IMHO); I consider the ragged lines to be tolerable because of the bad URL.

\documentclass{article}
\usepackage[a4paper, margin=2cm, showframe]{geometry}
\textwidth=6cm
\parindent=0pt
\usepackage{url}

\begin{document}
\hrule
I'd like to have acceptable breaks with a really ugly URL:
\url{http://theseareincrediblylong.theseareincrediblylong.theseareincrediblylong.theseareincrediblylong}
that doesn't break nicely and easily overflows,
yet my usual text is justified with my default tolerance.

%%% This is what I want it to look like,
%%% but I want the "tolerance" to apply to the URL only.
\tolerance=10000
\hrule
I'd like to have acceptable breaks with a really ugly URL:
\url{http://theseareincrediblylong.theseareincrediblylong.theseareincrediblylong.theseareincrediblylong}
that doesn't break nicely and easily overflows,
yet my usual text is justified with my default tolerance.
\end{document}

• Personally, I prefer to just let the URLs break wherever they need to, as I think that looks better and they aren't meant to be read as words, anyway. However, maybe you want \sloppypar? You can't do this except at the paragraph level (or larger unit). – cfr Aug 27 '18 at 1:13
• sloppypar has more the opposite effect (more stretching, in order to still fill the line - I'd like the usual amount of streching, and then rather use raggedleft if it cannot be justified nicely); plus it needs to be wrapped around the paragraph (like raggedleft). I'm looking for something easy to script without having to determine the extends of a paragraph myself. – Erich Schubert Aug 27 '18 at 13:15
• As I say, I'd just have the URLs break wherever, but I realise that's not to everyone's taste. – cfr Aug 27 '18 at 14:02
• As cfr mentions many things that could be done would have to be done at a paragraph level, so would necessarily apply to the text surrounding the URL as well. Since the effect of these tricks is usually at least somewhat detrimental to normal text I would avoid them if possible. \emergencystretch might be an acceptable option: tex.stackexchange.com/q/241343/35864 because it does not affect 'good' lines. So if this is only about URLs you might be better of trying to treat URLs differently. For example you could let them break anywhere as cfr suggests. – moewe Aug 28 '18 at 18:48
• Your name sounds German so maybe the thread golatex.de/ueberlaufende-url-t21025.html in the German golatex forum can give you some ideas. – moewe Aug 28 '18 at 18:49