[Tex/LaTex] How to avoid global \sloppy with many \texttt


I read that using a global \sloppy is bad practice. I have a document with many variables of a source code that I reference as in \texttt{VariableName}. I use a lot of them both in normal paragraphs and sometimes in section titles. By default they are being handled terribly and produce a lot of overshoots on the right margin. So far I came up with the following solutions:

  1. manually hyphenating \texttt{Variable\-Name} which is (a) terrible case-by-case work and (b) might suggest hyphenated variable names to the reader

  2. manually breaking the line right before the variable \\ \texttt{VariableName} which is (a) again terrible case-by-case work and (b) produces empty space on the right margin (line not "full")

  3. using \sloppy globally, apparently bad practice but in fact the only solution that I see which does not require me to go through my whole document and fix on case-by-case basis. This would be especially terrible if I, e.g. change margins in the end and have a bigger/smaller page and have to go through everything manually again. That is IMO not a solution.

Are there any better solutions?

Best Answer

This is how \sloppy is defined (from the LaTeX sources, which you can read with texdoc source2e or via CTAN or texdoc.net):

screenshot of definition of <code>\sloppy</code>

So these are the things \sloppy does:

  • Sets \tolerance to 9999: this is not as bad as the name makes it sound. If you're not inclined to pay attention to underfull/overfull box warnings and fix them, in fact this is never a bad idea: all that a higher \tolerance (but non-infinite, i.e. less than 10000) does is allow TeX to consider worse line breaks, while still trying to generate an optimal paragraph. It is highly unlikely to make the output worse and the worst you can say about it is that it makes TeX work harder, but on today's computers the difference can be measured in milliseconds.
  • Sets \hfuzz and \vfuzz to 0.5pt. This only affects what warning is shown to you: only lines which are overfull or underfull by more than 0.5pt are warned about. (The default is 0.1pt.)
  • Sets \emergencystretch to 3em. This is the most crucial thing for preventing overfull lines IMO (what you called “overshoots on the right margin”). It is additional stretch that TeX adds to each line, after everything else has failed. You can set it as large as you want, if you're willing to accept extra-large spaces between words on such lines. (If you're not going to rewrite text for dealing with overfull boxes, extra-large spaces are definitely better than lines sticking out of the right margin!)

Now that you know and understand what \sloppy does, you can decide for yourself whether or not you'd prefer to apply it globally, rather than go by maxims like “global \sloppy is bad practice”. There are cases where that makes sense, and cases where it doesn't: if you have such hard-to-break lines throughout your document, then it most definitely makes sense to globally take measures that will result in the best output.

Personally, for the case you mentioned (document with many hard-to-justify lines because of variable names in \textt forming unbreakable boxes), this would be my order of preference:

  1. Leave everything at the default settings; manually watch out for and fix overfull and underfull boxes by rewriting text, etc. This degree of polishing you can do if your document is worth the effort and you're sure you've already polished the content enough (which you should always do first). Else, below I assume you're not going to manually fix overfull boxes.
  2. Globally (across all paragraphs of this type) use whichever parameters of \sloppy you think you want: either \sloppy itself, or \tolerance 9999 along with as much \hfuzz as you don't want to get warned about, and as much \emergencystretch as you can tolerate (the higher the better for avoiding overfull lines).
  3. Give up on paragraph justification, and use ragged-right text. Text won't line up at the right margin, but you won't need any awkward spaces or hyphenation.
  4. Use hyphenation inside the \textt variables, globally: there are ways to do this, but as you said it can suggest to the reader that the variable is hyphenated, which I would really avoid.

The choice between (2) and (3) (\sloppy-like, or ragged-right) would depend on the exact document. Of course everything is subjective and a function of your aesthetics.

Related Question