# Good practice on spacing

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?

-
Look at Will's post, Correct punctuation spaces‌​. He doesn't address formulae there, but maybe he'll be along here later. –  Charles Stewart Nov 17 '10 at 11:36
@Charles Stewart So \@ is a "semantic" way of telling TeX where to end a sentence or not, while \  is a syntactic explicit spacing command, right? That's helpful... –  Seamus Nov 17 '10 at 11:47
yes, that's right. You should consider \@. a "sentence-ending period" and .\@ an "abbreviation period" . –  Will Robertson Nov 18 '10 at 8:07
AFAIK, those corrections are needed when you're using \nonfrenchspacing — which is default. However, most modern typesetters don’t use additional space between sentences. This is, as Bringhurst points out in The elements of typographic style, rather Victorian. Personally, I always use \frenchspacing and don't have to care about this kind of correction, although I do use ties and everything. –  rberaldo May 10 '11 at 20:46

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.

-
I think the correct approach would be to write Dr.~House the very first time, without worrying or checking whether or not it will occur at the end of a line. No matter what happens afterwards in the preceding text, it will still not break across a line. –  Niel de Beaudrap Nov 17 '10 at 14:36
@Niel: That would be better, yes. Likewise writing a, b, and~c or any of the other picky things Knuth suggests using ties for. But it's an extra processing step in my brain that turns out to be a waste of time as I revise in-place umpteen times. I feel that this belongs to the editing phase rather than the composition phase, and usually, there are more important things to do there when I get to it. So I never get around to inserting ties. –  Ryan Reich Nov 17 '10 at 15:21
I think it's most helpful because it's very easy to go through, add the ties in the editing phase, and then make a minor wording change that causes new bad breaks and so you have to go back and redo the ties. Better to just train yourself to do it right the first time. The most common place I see this messed up is before a citation. If one always write ~\cite{...} then this is never a problem. –  TH. Dec 18 '10 at 10:31
Apart from line breaking, there's another reason to write Dr.~House, that is… spacing. TeX automatically lengthens spaces after a period, that is, spacing between sentences is slightly longer than between words. This should be avoided (using ~ or \ ) when a period does not end a sentence. –  fudo Jul 5 '11 at 14:24
@fudo: ah, I like that reason. And the tie is one character shorter than the escaped space, too. –  Ryan Reich Jul 5 '11 at 17:19

Here is a short summary of my spacing policy.

• For inline text: simply use non-breaking spaces anywhere that you would never want the line to break. Think about doing so any time you are doing something unusual in your text, such as  Dr.~Who ,  Figure~\ref{fig:a} , and ad-hoc inline lists such as  (a)~this, or (b)~that .

• Almost all macros eat spaces after them. So if in doubt, force spaces after any macro, \like\ this. If you define macros such as

\newcommand\etc{\emph{etc.}}


then being mindful of this rule will, as a side effect, properly deal with the spaces following such italicized abbreviations.

The additional ideas below may not be "best practices", but they are practices that I have converged upon due to the demands of having to read and write my own work over and over again, and wanting things to be as easy to read as possible.

• Sometimes, in mathematical contexts, you know in advance that you will need more space because a certain piece of math is always written in that way. In such cases, you should think of defining a macro to take care of it. For instance, for use in integrals, it would be good policy to define a macro for the differential symbol d, as follows:

\makeatletter
\renewcommand\d[1]{\mspace{6mu}\mathrm{d}#1\@ifnextchar\d{\mspace{-3mu}}{}}
\makeatother


(This assumes you don't need to use the Icelandic character 'eth', which is the original meaning of  \d .) This macro adds spacing on the left of a differential form, but less if what preceded it was another differential. You can then write

$\cos(\theta)\cos(\phi) \d\theta \d\phi$


and get an output which is more approximately what you would want by default.

• I always look at the typeset version of my mathematics to ensure that it is easy to scan visually, and add space where I think it is necessary to help break the mathematics into easily parsed blocks of "tokens". So I make liberal use of the \, , \; , \! , and \quad macros.

-
I like your \d. –  Ryan Reich Nov 17 '10 at 12:25
There is also the xspace package which helps with spacing after macros. Also, which spacing commands are bigger and which smaller? –  Seamus Nov 17 '10 at 12:57
@Seamus: the order is \!, \,, \:, \;, \quad, and \qquad. In units of mu (= 1/18 em, determined from the surrounding font size), their widths are -3, 3, 4, 5, 18, and 36. –  Niel de Beaudrap Nov 17 '10 at 13:09
@Niel: The best definition for the differential in integrals and in differential forms in general is \mathop{}\!d (use \mathrm{d} if you prefer). This adds the correct spacing in all cases that I know. –  egreg May 10 '11 at 23:40
@Mk12: is there any advantage to writing it \like{} this, however? Writing it \this\ way is one character shorter and involves fewer keystrokes. As to cleveref.sty, that's a pretty good argument, though in the past I've had interoperation problems with hyperref.sty, which means arguably you should use the \autoref feature of the latter package anyway. –  Niel de Beaudrap May 28 '13 at 15:01

A tricky issue and an idea:

1. Will noted that in the when TeX typesets "(etc.)", TeX puts extra space after the close parenthesis, so treating it as the end of a sentence, not the end of an abbreviation. See his Correct punctuation spaces;
2. An SO thread about how to implement the macros Will suggested at the end of his item, Correct way to define macros \etc \ie in latex, showed up lots of tricky issues implementation them. The accepted answer, by Jukka, said, pace Will, that the right thing is just to learn all the workarounds for TeX's foibles.

My stab at implementing a \latinabbrev macro-defining macro convinced me, after much time, that it's hard to get the definition right, but it is possible, and the corner cases are so tricky that you can't expect most LaTeX users to understand them well enough to always do the right thing in their writing.

-

Just about the integrals: Personally, I wouldn't consider \int x\ dx good practice; the space is too large. I always use \int x\,dx.

-
Actually, that's right. I was reading an article about this on my phone, and the commas were too small for me to see comfortably the first time... –  Seamus Nov 17 '10 at 12:55
I edited the question to reflect this point. –  Seamus Nov 17 '10 at 22:38
.. I stumbled on this old question, shouldn't mathematical operators such as d be in upright roman? –  Yiannis Lazarides Mar 12 '11 at 20:06
@saltypen: I actually don't like them upright. The mathematics community is divided on this question. I use upright only for stuff like $x_{\max}$, where it is clear that the "max" is text. This is not the case with "d". –  Hendrik Vogt Mar 12 '11 at 20:20

Actually, when \nonfrenchspacing is in use, it's most correct (as I understand it) to put an \@ between every uppercase letter and sentence punctuation mark, not just sentence-ending punctuation marks. For instance, ideally you should type They use three languages at NASA\@: C\@; MATLAB\@, the best; and Pascal.. Under \nonfrenchspacing, colons, semicolons, and commas all have space factor codes greater than 1000, so without the \@'s in my example you wouldn't get perfect spacing after "NASA:", "C;", or "MATLAB,". (You don't get extra space after semicolons or commas, but spaces after those punctuation marks stretch more and shrink less than a normal interword space.)

Still, all those \@'s can make a TeX file quite hard to read. So I tend not to bother with \@'s between uppercase letters and commas or semicolons. Lamport's LaTeX 2e manual doesn't say to add them there, and the TeXbook only implies that something goes "wrong" in those situations (see page 76), so I assume that this is a reasonable compromise.

-