[Tex/LaTex] How to relax the horizontal spacing rules for one line

blockboxesspacing

I have a line that happens to end in a non-breakable code-fragment

This is a long line that ends in a \pre{std::ostream}
long word.

When printed the line is too full. The spaces between the words on the line
are as slim as they can be.

I would like to relax the spacing rules for that line so that the spaces will be stretched as needed to bring the pre-word on the next line, even if that space stretch will not look very nice anymore

This   is   a   long   line   that   ends   in   a
\pre{std::ostream} long word.

I did put \hfill between all those words, but that can not be the solution, right?

Update: It seems to make a difference that the problematic line is inside a list item. Is there a "paragraph-less" solution that works on both, paragraphs and list items (and supposedly in many other cases)?

* This is a long line that ends in a \pre{std::ostream}
  long word.

versus

* This   is   a   long   line   that   ends   in   a
  \pre{std::ostream} long word.

When I use sloppypar I get an additional line break.

enter image description here

Best Answer

Whereas \sloppy will loosen the alignment constraints on the whole document, the sloppypar environment can limit the effect of the damage to a localized area.

In my MWE, the \hrulefill shows the extent of the left/right margins.

\documentclass{article}
\def\pre#1{\makebox{#1}}
\begin{document}
\noindent\hrulefill

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxThis is a long line that ends in a 
  \pre{std::ostream}long word.

\begin{sloppypar}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxThis is a long line that ends in a 
  \pre{std::ostream}long word.
\end{sloppypar}
\end{document}

enter image description here

It helps to understand that when TeX (which underlies LaTeX) is justifying a paragraph, it attempts to satisfy a set of weighted constraints and penalties to guide the layout. When the constraints posed by these penalties cannot be met, an overfull or underfull box results, which looks to the user as a margin over- or underrun. This is what is happening when a line is ending in a long unbreakable object (I used a box in my MWE).

By using the sloppy settings, the weights and penalties are revised to make "a" solution more likely. As you point out, the sloppy solution may be more ugly in some ways, but it will largely eliminate the margin overruns that can otherwise result.

Related Question