3

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

towi
  • 2,500
  • 2
  • 22
  • 29
  • 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 03 '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 04 '14 at 07: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 04 '14 at 07: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 04 '14 at 08:02
  • @tohecz You are correct, of course. Like "vom Typ" to "von dem Typ" -- identical meaning, better linebreak. +1 – towi Sep 04 '14 at 09:10

3 Answers3

7

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.

  • 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 04 '14 at 07:22
  • I found the solution it seems. \sloppy and \fussy in the beginning of the respective paragraphs/items. – towi Sep 04 '14 at 07:33
  • @towi Or \item {\sloppy My bad very bad paragraph.\par} – yo' Sep 04 '14 at 08:08
  • @tohecz I think I tried that but it didn't work. I might be mistaken. – towi Sep 04 '14 at 09: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 04 '14 at 09:23
  • @wipet While I don't think the my original statement is incorrect (LaTeX employs TeX... it is like quibbling that I did not hammer the nail because only a hammer can hammer the nail), I have clarified. – Steven B. Segletes Sep 04 '14 at 09:41
0

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
towi
  • 2,500
  • 2
  • 22
  • 29
0

If I am understanding correctly, you could also use \linebreak to create a new line while enforcing the spacing of the old line. If you use \newline or \\, it will create a new line without aligning the previous line to the margin which I find annoying. This seems a little more manual than you might want.

Trevor
  • 1