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?

  • I would really use @egreg solution in tex.stackexchange.com/questions/324869/… --- if you want a justified text and have long identifiers, you need to hyphenate them. The solution linked performs automatic hyphenation on uppercases and underscores, and use a "centered dot" which is in my opinion perfect for this case.
    – Rmano
    Commented Oct 1, 2017 at 11:54
  • Duplicate of tex.stackexchange.com/questions/44361/… ? Commented Oct 1, 2017 at 16:00
  • 1
    I would typeset these variables using \url of the url package (instead of \texttt), instructing the macro to break at any letter, with this preamble addition: \makeatletter \g@addto@macro{\UrlBreaks}{\do\/do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z}\makeatother Commented Oct 1, 2017 at 16:21
  • @StevenB.Segletes - very clever. Note though that if you also use hyperref, that package will try to turn your \url macros into hyperlinks. Commented Oct 1, 2017 at 18:06
  • 1
    If you like the basic \url approach, but don't want hyperlinks, then perhaps an answer based on this one could achieve what you want: tex.stackexchange.com/questions/219445/line-break-in-texttt/… Commented Oct 1, 2017 at 19:34

4 Answers 4


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.


I would use \sloppy (or use \RaggedRight from the ragged2e package if you don't mind giving up the justified right margin.)

General advice about avoiding over stretched white space is general advice but it's mostly advice about natural language text and if what you are writing isn't natural language then different rules may apply (or there may be no rules and you have to do whatever you think best).

You don't need to do so much manual work. If you accept hyphenated variable names then you can re-enable hyphenation in the tt font, however I wouldn't do that, especially if - is a legal character in variable names in the language being documented.

Similarly you can define a command such that \zz{MyVariable} puts MyVariable in tt and makes a breakpoint before the word, leaving the line short if a break is taken at that point (as if \\ had been used) That's a possibility but if there a lot it's probably better to use \RaggedRight.

Or you can use \sloppy which is probably OK if you want justified paragraphs.


For the specific paragraphs which have a lot of such function names use


it makes more sense than a global \sloppy


Here is a solution based on the hyphenat package:




\newcommand{\terrible}{{\ttfamily MyTerriblyExcruciatinglyLongIdentifierThatsWhatYouGetForWritingJava}}

This document describes a new program for comparing two numbers according to size. One number is referred to as \texttt{a}, the other one as \terrible.

enter image description here

  • 2
    Just for information, TeX won't add hyphens after the 63rd character in a word. For instance it won't honor the discretionary hyphen between Writing and Java.
    – egreg
    Commented Oct 1, 2017 at 16:11

You must log in to answer this question.

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