127

There are many way to influence line breaking, and avoiding overfulls on long unbreakable pieces of text. I'm puzzled. We are told that using \sloppy document-wide is not good , that an alternative solution to \sloppy is \emergencystretch , that microtype package can help tune linebreaking etc.

Can someone explain the effect of all the enumerated commands?

My own experiment:

\documentclass[12pt]{article}
\usepackage{showframe}
\begin{document}

\fussy
Long made-up words, fussy: 

abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl

\sloppy
Long made-up words, sloppy: 

abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl

\fussy
Long made-up words, textit, fussy: 

\textit{abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl}

\sloppy
Long made-up words, textit, sloppy: 

\textit{abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl}

\fussy
Here is a line with long unbreakable rule, fussy. \rule{3cm}{.2cm} \rule{3cm}{.2cm}

\sloppy
Here is a line with long unbreakable rule, sloppy. \rule{3cm}{.2cm} \rule{3cm}{.2cm}

\fussy
Extreme unbreakable rule, fussy. \rule{8cm}{.2cm} \rule{8cm}{.2cm}

\sloppy
Extreme unbreakable rule, sloppy. \rule{8cm}{.2cm} \rule{8cm}{.2cm}

\end{document}

As we can see, \sloppy helps avoiding overfulls, but not in extreme cases: enter image description here

0

1 Answer 1

141

\sloppy is a latex macro that does

\def\sloppy{%
  \tolerance 9999%
  \emergencystretch 3em%
  \hfuzz .5\p@
  \vfuzz\hfuzz}

\fussy sets the values back to the latex defaults

\def\fussy{%
  \emergencystretch\z@
  \tolerance 200%
  \hfuzz .1\p@
  \vfuzz\hfuzz}

\tolerance sets the maximum "badness" that tex is allowed to use while setting the paragraph, that is it inserts breakpoints allowing white space to stretch and penalties to be taken, so long as the badness keeps below this threshold. If it can not do that then you get overfull boxes. So different values produce different typeset result.

\emergencystretch (added at TeX3) is used if TeX can not set the paragraph below the \tolerance badness, but rather than make overfull boxes it tries an extra pass "pretending" that every line has an additional \emergencystretch of stretchable glue, this allows the overall badness to be kept below 1000 and stops TeX "giving up" and putting all stretch into one line. So \emergencystretch does not change the setting of "good" paragraphs, it only changes the setting of paragraphs that would have produced over-full boxes. Note that you get warnings about the real badness calculation from TeX even though it retries with \emergencystretch the extra stretch is used to control the typesetting but it is not considered as good for the purposes of logging.

\hfuzz does not affect the typesetting in any way but just stops TeX complaining if the box is is only slightly over-full.

\hbadness not used in \sloppy but used in the final example below` is similar, it does not affect the typesetting but stops TeX warning about underfull boxes if the badness is below the given amount.


The main problem with \sloppy is the setting of \tolerance to 9999 which is almost infinitely bad. That does not distribute the white space evenly but encourages individual lines of the paragraph to stretch the white space to arbitrarily large amounts. This was an acknowledged deficiency in TeX and why in TeX3 the new parameter \emergencystretch was added. Unfortunately when adding \emergencystretch to \sloppy when definining it for LaTeX2e, we didn't reduce the \tolerance setting, but left it at 9999, which is probably good for compatibility with LaTeX2.09, but bad for everything else.

Note final settings show that often you get a better setting with just setting \emergencystretch and not changing \tolerance at all, the setting is the same in both cases, but \hbadness=10000 silences the warnings. Not everyone would want the final setting without being warned about it, but in automatic typesetting contexts where changing the input is not a possibility and you never want over-full boxes, this is a possibility.

enter image description here

\documentclass[12pt]{article}
\usepackage{showframe}
\begin{document}

\def\test#1{{#1

\texttt{\detokenize{#1}}

Long made-up words, sloppy: 

abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl

\textit{abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl}

Long made-up words, textit, sloppy: 

\textit{abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl abcdefghijklabcdefghijkl}

Here is a line with long unbreakable math, sloppy. ${a+b+c+d a+b+c+d}$ ${a+b+c+d a+b+c+d }$


Extreme unbreakable math, sloppy. ${a+b+c+d+e a+b+c+d+e a+b+c+d+e a+b+c+d+e }$ break ${a+b+c+d+e a+b+c+d+e a+b+c+d+e a+b+c+d+e }$


\nobreak\bigskip\hrule\filbreak}}

\test{}


\test{\sloppy}

\test{\setlength\emergencystretch{\hsize}}

\test{\setlength\emergencystretch{\hsize}\hbadness=10000}





\end{document}
6
  • 17
    @Maïeul \sloppy will fix some cases that just setting \emergencystretch won't fix, but just setting \emergencystretch has the big advantage that it has no effect on good paragraphs, it only kicks in if the paragraph would otherwise have failed to be within tolerance. \sloppy changes the settings before the linebreaking is affected so can (and typically does) affect paragraphs that would have worked 9and looked better) with the default settings. Nov 26, 2015 at 12:17
  • 1
    So which function do you guys recommend? Is sloppy a bad thing to use in documents?
    – wonderich
    Jan 28, 2019 at 23:37
  • 2
    @wonderich as I say here generally I'd experiment with setting \emergencystretch before using \sloppy. But there is no single right setting it depends on circumstances. (If there was a clearly best setting for all cases, latex would not have \sloppy and \fussy) Jan 28, 2019 at 23:41
  • 1
    @user if tex can hyphenate the paragraph with badness less than \tolerance then the emergency stretch is not used at all, so setting it never makes things worse if they were not already bad (but still setting it to some values may make things subjectively worse than others). As I say in this answer there is no right value that is best in all cases, otherwise you would not need a user-settable parameter. Jul 12, 2019 at 7:03
  • 2
    Worth mentioning that one can use localized \sloppy with \begin{sloppypar} ...paragraph text... \end{sloppypar}
    – Dinei
    Sep 28, 2020 at 3:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .