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.


* 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

  • 2
    Issue \linebreak before \pre{std::ostream} would also do the trick. See Underscore makes text go past end of line into margins. – Werner Sep 3 '14 at 20:05
  • 1
    Can't you just re-word the text? Ad an adjective on an appropriate place, or sort-of a filler word? Or remove a word? That would help, too. – yo' Sep 4 '14 at 7:52
  • @tohecz Only as a last resort. a) I want to know a solution for the future, b) it is not big a overfull box that that last resort is needed I thought c) I am past orthography proof reading and do not want to introduce any new ones ;-) – towi Sep 4 '14 at 7:59
  • @towi (as a math journal typesetter) Sometimes I modify the text to get rid of bad line just before the proofs (i.e., before sending the article to the authors for a final check). However, I only do small stuff like adding or removing "that" in appropriate places etc. I think you could re-consider the work-flow and allow such occasional minor changes. But you ought to know what's better, I only shoot some ideas ;) – yo' Sep 4 '14 at 8:02
  • @tohecz You are correct, of course. Like "vom Typ" to "von dem Typ" -- identical meaning, better linebreak. +1 – towi Sep 4 '14 at 9:10

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.


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

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

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.

  • Alas, my problematic line is inside a list-item. When I use sloppypar inside that I get an extra line break (but with sloppy white spacing at least). Sorry, did not know that that was important. I updated the question – towi Sep 4 '14 at 7:22
  • I found the solution it seems. \sloppy and \fussy in the beginning of the respective paragraphs/items. – towi Sep 4 '14 at 7:33
  • @towi Or \item {\sloppy My bad very bad paragraph.\par} – yo' Sep 4 '14 at 8:08
  • @tohecz I think I tried that but it didn't work. I might be mistaken. – towi Sep 4 '14 at 9:09
  • 1
    "It helps to understand that when LaTeX is justifying a paragraph..." -> "It helps to understand that when TeX is justifying a paragraph...". LaTeX is only macro package which doesn't justify paragraph and doesn't calculate with penalties, demerits, badness etc. in the way described above. This is work for TeX. – wipet Sep 4 '14 at 9:23

To summarize the answer from @steven-b-segletes and @werner I want to share my results with everyone.

Here is what the different solutions look like:

enter image description here

At the end of each paragraph I noted what that paragraph was set with:

  • original
  • \linebreak
  • \begin{sloppypar}
  • \sloppy and \fussy
  • back to original

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.