I know that latex sometimes allows overfull hboxes as an alternative to having to stretch beyond the stretch settings or as an alternative to having ragged right sides.
However, in my specific case, LaTeX sometimes permits overflows that should really not be acceptable. I have provided a minimal reproducing example below.
To be clear, I am not looking for a specific solution to this case. I know I can use \sloppy, \raggedright or some combination of the emergencystretch and tolerance settings. I'm looking for a general solution to tell latex that overflows longer than X is unacceptable in any case. I want LaTeX to automatically find a better solution if the overflow would be bigger than X. To me, a ragged right side would be perfectly acceptable as an alternative to such big overflows.
What are the settings to tweak in LaTeX to prevent these problems?
The reproducing example:
\documentclass{article}
\usepackage{listings}
\newcommand{\lstinlinestyle}{\ttfamily}
\newcommand\codeinline{\lstinline[basicstyle={\lstinlinestyle}]}
\begin{document}
Lorem Ipsum is simply dummy text of the printing and typesetting \codeinline{Textthatcannotbehyphenated}.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
\end{document}
I would prefer the output to look like this without manual intervention:
Edit: Do I have to care about bad boxes? does not answer my question, because it only mentions \sloppy and \emergencystretch as the solutions to the problem. This does not always resolve the problem because sometimes the amount of stretch necessary to create a good line break will result in a strange-looking paragraph. In such cases a ragged right side will look much better.
Edit2:
While testing the solution, I noticed that I use \texttt and that it overflows as well. Here is a reproducing example:
\documentclass{article}
\begin{document}
Lorem Ipsum is simply dummy text of the printing and typesetting \texttt{Textthatcannotbehyphenated}.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
\end{document}
So, I'm still looking for a more general solution than the one provided in the current answer.
Edit3: This question is much related to what I want to do as well: Wrap line instead of overfull hbox? but it was never answered to satisfaction.
Edit 5: No satisfactory solution has been found yet. If you have any ideas, please let me know! I'm considering to make a LuaLaTeX package that uses raggedright as a fallback in case a paragraph has a certain badness score.



