I sometimes have long words that are unbreakable and end up sticking out into the right margin. I realize that there's nothing magical to be done about this and the only real solution is to break them or rewrite the text.
However, I'm trying to change the last-resort behavior so it breaks the line before the long word and sets it at its natural spacing, while justifying the rest of the paragraph.
I already found an answer on tex.sx that defines a command on a per-word basis, but this doesn't change the default behavior. Also, the \lword command from the linked answer causes the preceding line to be stretched slightly and inserts line breaks where they're not strictly necessary. I've attempted to fix these issues in my \Lword command.
Ideally, I'd also like the [draft] option to highlight these emergency line breaks.
Here are some examples of what I've tried so far:
\documentclass[draft]{article}
\usepackage{showframe}
\usepackage{ragged2e}
\usepackage{color}
% https://tex.stackexchange.com/a/62347/99343
\newcommand*\lword[1]{%
\leavevmode
\nobreak
\hskip0pt plus\linewidth
\penalty50
\hskip0pt plus-\linewidth
\nobreak
\textcolor{red}{#1}%
}
% same as above, but with higher penalty and infinite stetch
\newcommand*\Lword[1]{%
\leavevmode
\nobreak
\hskip0pt plus 1fil
\penalty9999
\hskip0pt plus -1fil
\nobreak
\textcolor{blue}{#1}%
}
\newcommand*\test[4]{{%
#2%
\strut\llap{\smash{\begin{minipage}[t]{8em}\RaggedLeft #1\end{minipage} }}%
Aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa
#3{bbbbbbbbbbbbbbbbbbbbbbbbbbbbb}
aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa
aaaa aaaa aaaa aaaa aaaa aaaa aaaaa
#4{bbbbbbbbbbbbbbbbbbbbbbbbbbbbb}
aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa.
\par
}}
\setlength\parindent{0pt}
\setlength\parskip{1ex}
\begin{document}
\test{default rendering} {}{}{}
\test{manually inserted linebreak (desired result)}{}{\\}{}
\test{lword macro} {}{\lword}{\lword}
\test{Lword macro (desired result)} {}{\Lword}{\Lword}
\test{sloppy} {\sloppy}{}{}
\test{emergencystretch} {\emergencystretch10em}{}{}
\test{RaggedRight} {\RaggedRight}{}{}
\test{sloppy and RaggedRight} {\sloppy\RaggedRight}{}{}
\end{document}
Here's my actual question:
- Is it possible to get this line breaking behavior without wrapping potential problem words in an extra command, and if so, how?
- Failing that, do my changes to
\lwordhave any possible side effects?

bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb? At least in english, this is impossile. – Johannes_B Mar 14 '16 at 11:28\emergencystretchand\sloppy, but these commands never produce a short line in the middle of a paragraph. I consider a short line to be less disrupting than an extremely stretched one. – wrtlprnft Mar 14 '16 at 12:21\url: If there is a line break opportunity near the beginning of the word, I would need the\Lwordmagic to happen there, too. Maybe I should edit my question... – wrtlprnft Mar 14 '16 at 12:29\url{http://www.somelongdomainnamethatdoesnotwrap.com/}where the first period is too early and the second too late, but breaking at the first period would still reduce the shortness of the line. If I effectively do\Lword{\url{...}}, TeX will not break there. I guess I'll look ifhyperrefcan be sufficiently customized for this. – wrtlprnft Mar 14 '16 at 12:39\Lword{\url{...}}should allow breaking anywhere within the url or breaking short and moving the whole url over. – David Carlisle Mar 14 '16 at 12:42