Feeding
\documentclass{article}
\newtheorem{theorem}{Theorem}
\begin{document}
Averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword.
\begin{theorem}
Lots of stuff in several lines. Lots of stuff in several lines. Lots of stuff in several lines.\hfill\rule{1ex}{1ex}%%% a halmos.
\end{theorem}%
A paragraph following the theorem.
\begin{theorem}
Lots of other stuff.\hfill\rule{1ex}{1ex}%%% a halmos.
\end{theorem}%
\end{document}
to pdflatex yields a para, in which the second line is overfull:
To avoid an overfull in the second line, you set \emergencystretch as locally as possible in the first para to a mostly small positive value. However, you also have to later set \emergencystretch to zero to avoid bad effects down the stream. In effect,
\documentclass{article}
\pagestyle{empty}
\newtheorem{theorem}{Theorem}
\begin{document}
\setlength{\emergencystretch}{1.02em}%
Averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword-averylongword.
\begin{theorem}\setlength{\emergencystretch}{0em}%%% this is local
Lots of stuff in several lines. Lots of stuff in several lines. Lots of stuff in several lines.\hfill\rule{1ex}{1ex}%%% a halmos.
\end{theorem}%
\setlength{\emergencystretch}{0em}%%% this is global
A paragraph following the theorem.
\begin{theorem}
Lots of other stuff.\hfill\rule{1ex}{1ex}%%% a halmos.
\end{theorem}%
\end{document}
yields
The output it good, but, clearly, setting the same variable at three different places (which need not be on the same screen of you LaTeX editor) is error-prone. Is there a better way that would NOT introduce additional vertical space or superfluous first-line indents?


sloppyparenvironment. See How to avoid using \sloppy document-wide to fix overfull \hbox problems? for more information. – barbara beeton Jun 25 '22 at 00:22urlpackage, adding hyphenation points, and using thesloppyparenvironment. My non-minimal example has no URLs. There, I do wish to break the “offending” long compound word after one of the hyphens that combines its subwords; hence, I don't need additional hyphenation points. Wrapping the first para intosloppyparintroduces more eager breaking and removes also the hardly visible first-line–overfull problem. Not sure I want it in general. – Jun 25 '22 at 00:52sloppyparis mainly intended to isolate the problem paragraph, you might look at how it's defined, and substitute your preferred value of\emergencystretchfor the equivalent in that definition. Give it a new name, of course. Or define a new environment that uses your value of\emergencystretchand is wrapped in\begingroup ... \par\endgroup. The\paris necessary for the\emergencystretchto be applied properly. – barbara beeton Jun 25 '22 at 01:37