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:
-
manually hyphenating
\texttt{Variable\-Name}
which is (a) terrible case-by-case work and (b) might suggest hyphenated variable names to the reader -
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") -
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 withtexdoc source2e
or via CTAN or texdoc.net):So these are the things
\sloppy
does:\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.\hfuzz
and\vfuzz
to0.5pt
. This only affects what warning is shown to you: only lines which are overfull or underfull by more than0.5pt
are warned about. (The default is0.1pt
.)\emergencystretch
to3em
. 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:\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).\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.