[Tex/LaTex] Good practice on spacing

best practicesspacing

In various places, in passing, I've seen mention of various good practices regarding well-behaved spacing in LaTeX. For example, Dr. Who should really be written Dr.\ Who or Dr.~Who to get the right spacing (Since the full stop is not the end of a sentence.

Similarly with integrals: \int x\, dx should be preferred to \int x dx to properly space out the "dx".

But this is all piecemeal advice for specific circumstances: what are the general principles guiding when to pay attention to how LaTeX is dealing with your spaces?

Best Answer

You want to keep in mind all the irregularities in your text: places where superficially uniform text turns out not to be uniform. You give periods in abbreviations as examples; the reason they are is that a period does not usually go inside a sentence, but in this case, it does (and only a speaker of idiomatic English could know that, not TeX). The differential in an integral is another example: it is the same text as the integrand, but it is not part of the integrand (in this case, I could imagine TeX being written to look out for this, but there are probably good reasons it doesn't).

You should, of course, also look out for constructions which are superficially different but in fact are not. These may center on certain TeX idioms: for example, if you were programming in plain TeX (which you are not, and so you should not actually write this ever) you might do the following:

The following {\it italic text} is not well-spaced.

If you set that, you may see that the word text is a little too close to "is". In the TeXbook, Knuth reminds you to put in an "italic correction". However, now that LaTeX has \textit{...}, which takes care of this, you probably never even learned what an italic correction is. So this is a non-example. The point remains, however, that certain TeX constructions break the flow of the text (in particular, grouping) and you need to pay attention to the typeset result to see if they broke the spacing.

Vertical space can also be an issue, and harder to deal with. Knuth also warns against using tall symbols in the text (like \frac{1}{2} instead of 1/2) because they force the lines apart. Thus, you need to scrutinize all the inline math you write for tall symbols, and consider using displayed equations. Sometimes you can work around this using \smash if you know there is space and TeX doesn't.

Inline math causes another problem with TeX's line breaking algorithm, because it won't break at a lot of places in an equation, commas being the notorious example. Thus, write $a$, $b$, and $c$ rather than $a, b, \text{ and } c$ or even $a, b$, and $c$. Knuth also wants you to put a tie in: and~$c$; I confess that I never use ties. Like manual spacing corrections in equations, they seem like they should be reserved for final polishing (I mean, if Dr. House is in the middle of a line, it's not going to break).

In short, you need to watch for scope changes, mode changes, and changes in "semantic scope", where the last one is totally impossible to communicate to TeX and the other two are still insidious. However, you should not be afraid to "just try it" and see whether you really do have a problem. It is much faster to let TeX do whatever it does (and with TeX, "whatever it does" is sometimes all you can say easily) than to try to anticipate it.