Automatically inserted spaces

A blank you type in a LaTeX source is not a character but a measurement. In 10-point Computer Modern the space between two words is exactly 3.33252 pt — and so, when you measure them, are ~ and \ (backslash-space). In other places, however many blanks you type, not one of them survives: that is why \LaTeX is comes out as “LaTeXis”. This page covers the measured widths of the spaces LaTeX inserts automatically, the commands that put back a space that vanished, and how to choose between ~, \hspace and \quad when you place one yourself.

How wide is a LaTeX space, in points?

Measure a 10-point article with T1 Computer Modern and the interword space comes out at 3.33252 pt, with 1.66626 pt of stretch and 1.11084 pt of shrink. Those three numbers belong to the font itself; TeX calls them \fontdimen2, \fontdimen3 and \fontdimen4. So a LaTeX space is not a width but a stretchable width — what TeX calls glue. Justified lines come out flush precisely because that stretch and shrink is rationed out line by line, something that would be impossible if a space were a character.

The spaces you place yourself, by contrast, include several that do not stretch at all. \, (a thin space) is 1.6663 pt, \enspace is 4.99878 pt, \quad is 9.99756 pt and \qquad is 19.99512 pt. All four are defined in em: \quad is exactly 1 em, \qquad 2 em, \, one sixth of an em and \enspace half an em. Because the em follows the font size, the same \quad grows to 11.74713 pt inside \large, whereas \hspace{10pt} stays 10 pt wherever you write it. Em-based lengths when the gap should follow the text; absolute lengths when it must match a figure or a rule — that is the whole of the choice.

CommandMeasured at 10ptDefinition and behaviour
(ordinary blank)3.33252 pt\fontdimen2; glue, with 1.66626 pt stretch and 1.11084 pt shrink
\ 3.33252 ptcontrol space; the interword width, but immune to the sentence-space rule
~3.33252 pt\ with \nobreak in front: same width, but no line break there
\,1.6663 pta thin space of one sixth of an em; same as \thinspace; rigid
\negthinspace−1.66631 ptthe inverse of \,; used to pull two things together
\enspace4.99878 pthalf an em; handy around list labels
\quad9.99756 ptexactly 1 em; grows to 11.74713 pt inside \large
\qquad19.99512 pt2 em; used to separate formulas or examples
\hspaceas specified\hspace{10pt} is always 10 pt — but it is discarded at the start of a line
\hspace*as specifiedthe version that survives at the start of a line; no break there either

\LaTeX\ is and \LaTeX{} is are not the same width

A blank typed straight after a command made only of letters is consumed as the signal that the command name has ended, and not one point of it reaches the page. Measured, \LaTeX is is 32.37791 pt wide and \LaTeX\ is is 35.71043 pt — a difference of exactly 3.33252 pt, one interword space. This is the first thing people hit when they search for “LaTeX space disappears”, and running pdftotext over the PDF really does give you LaTeXis run together. Typing more blanks, or folding the source onto the next line, changes nothing.

There are three fixes, and \LaTeX\ is, \LaTeX{} is and {\LaTeX} is all measure 35.71043 pt. But “all three are the same” holds only under a condition. After a period they part company: etc.\ Next measures 39.99023 pt while etc.{} Next measures 41.10107 pt, a difference of 1.11084 pt — exactly the extra width added at a sentence end (\fontdimen7). The reason is that \ is a space that never receives that extra, whereas an ordinary blank typed after {} is widened as a sentence space. So \ does not merely restore a lost space, it also cancels sentence treatment — which is why it is the fix for abbreviations such as Fig.\ 5. The heuristic behind sentence spacing itself — \sfcode, \@ and \frenchspacing — is covered on the punctuation page.

latex
% widths measured with \sbox0{...}\typeout{\the\wd0}
% pdfLaTeX, TeX Live 2024, 10pt article, T1 Computer Modern
\LaTeX is       % 32.37791pt -> "LaTeXis"  (the blank is eaten)
\LaTeX\ is      % 35.71043pt
\LaTeX{} is     % 35.71043pt
{\LaTeX} is     % 35.71043pt

% ...but after a period the two fixes are NOT the same:
etc.\ Next      % 39.99023pt  (control space: no sentence space)
etc.{} Next     % 41.10107pt  (+1.11084pt = \fontdimen7)

Why xspace is as often a trap as a fix

The xspace package lets you end your own command with \xspace so that a space is added “unless the next character is punctuation”, sparing you from typing \LaTeX{} every time. But the test is nothing more than a lookup in a list of exceptions. The list in xspace.sty as shipped with TeX Live 2024 (maintained by the LaTeX Project, part of the tools bundle) holds comma, period, apostrophe, slash, question mark, semicolon, colon, exclamation mark, tilde, hyphen and the closing round bracket, plus \ , \/, the braces, \space, \footnote and \footnotemark. In front of anything not on that list, \xspace inserts a space.

The closing square bracket ] is not on the list. So if you define \newcommand{\eg}{e.g.\xspace} and write [\eg], the output is [e.g. ] — a space right before the bracket. Worse, since the preceding character is a period, the space inserted is a sentence-width one: measured 22.2168 pt against 17.77344 pt without \xspace, a difference of 4.44336 pt. The same happens before \eg\% (giving e.g. %), before \eg\@., and before a straight double quote. And the failure runs the other way too: writing \eg{} for safety makes the brace match an exception, so no space is inserted at all and \eg{}x comes out as e.g.x.

latex
\usepackage{xspace}
\newcommand{\eg}{e.g.\xspace}

\eg foo    % "e.g. foo"   correct
\eg, foo   % "e.g., foo"  correct (comma is an exception)
[\eg]      % "[e.g. ]"    WRONG -- ] is not an exception
\eg\%      % "e.g. %"     WRONG
\eg{}x     % "e.g.x"      WRONG -- the brace matches an exception

% You can patch the list, but every new case needs another patch:
\xspaceaddexceptions{]}

You can extend the list with \xspaceaddexceptions{]}, but every addition leaves you counting what you have forgotten next. That is why experienced authors tend to skip \xspace altogether: they define \newcommand{\eg}{e.g.} plainly and write \eg{} foo at the call site, or they abandon the abbreviation macro and just type e.g. directly. \xspace genuinely pays off only where the macro is called often and you can promise that only ordinary words follow it — a product or project name, say.

Where the tie ~ is mandatory: the space that never breaks

~ is a space of the same width as an interword space that will not let a line break there. There is no special kind of blank behind it: the definition in latex.ltx reads \DeclareRobustCommand{\nobreakspace}{\leavevmode\nobreak\ }a plain \ with \nobreak in front of it. Measured, x~y therefore gives the same 3.33252 pt gap as x\ y, and like \ it declines the sentence-end extra (x.~y is 16.6626 pt against 17.77344 pt for a plain x. y). Everything people look for under “LaTeX tilde line break” comes down to that one \nobreak.

The effect is easiest to see in a narrow column. Set “We show in Figure 5 that it holds.” in a 3 cm parbox and the line ends on Figure, leaving 5 to open the next one. Change it to Figure~5 and TeX hyphenates Fig-ure instead rather than separate the two. So ~ does not merely forbid a break: it makes TeX look for a different set of breaks. The places where it is genuinely mandatory are those where a number, symbol or abbreviation stranded at the start of a line stops making sense — Figure~5, Section~2, Eq.~(3), Chapter~12, Dr.~Smith, Knuth~(1984), initials as in D.~E. Knuth, and a value with its unit as in 10~kg. With cross-references and citations the tie belongs in front of \ref and \cite: Figure~\ref{fig:main}, see~\cite{knuth1984}.

latex
% in a 3cm column, the plain space breaks the reference in half:
We show in Figure 5 that it holds.   % -> "...in Figure / 5 that..."
We show in Figure~5 that it holds.   % -> "...in Fig-/ure 5 that..."

See Section~\ref{sec:method} and Figure~\ref{fig:main}.
As shown by Knuth~\cite{texbook}, a mass of 10~kg suffices.
D.~E. Knuth, Dr.~Smith, Eq.~(3), Chapter~12.

Two cautions. First, ~ only refuses a line break; it does not stop hyphenation. That is exactly what happened above when Fig-ure split, and if you need the word itself kept whole you want \mbox{...}. Second, ~ is a character LaTeX makes active, so outside verbatim you cannot simply type it to get a tilde in a URL or a file path. To print the tilde character in running text, use \textasciitilde.

The difference between \hspace and \hspace*

In the middle of a line the two behave identically. The difference appears when the line breaks at that very gap. TeX discards glue that lands immediately after a break, so \hspace{1.5cm} vanishes entirely if the break falls there. Measured: set alpha beta gamma\hspace{1.5cm}epsilon zeta in a 3 cm column and TeX breaks at the gap, so the 1.5 cm appears nowhere. Change it to \hspace*{1.5cm} and the break does not happen there — epsilon is hyphenated instead and the 1.5 cm gap stays on the page.

In practice, choose the starred form when the gap is part of the layout: a signature field on a title page, a fixed step before or after a heading, or a line where \hspace works with \hfill to push elements to opposite margins. When you only want a little air for readability, the unstarred form is right, and its disappearing at a line end is a feature rather than a bug. Negative values work too — \hspace{-1em} measurably pulls the text back by exactly one em.

\ignorespaces and \unskip: killing the space an environment leaks

\ignorespaces skips every blank that follows it, while \unskip removes one glue item already placed before it. The first eats input, the second trims output — remember it that way and the two never get confused. Measured: a b is 13.8855 pt and a \unskip b is 10.55298 pt, shorter by exactly one interword space. a\ignorespaces b, with two blanks, also comes to 10.55298 pt.

They earn their keep around environments you define yourself. Define \newenvironment{note}{\textbf{Note:}}{\textbf{.}} and write \begin{note} yes \end{note} tail, and the blanks after \begin{note} and before \end{note} both reach the page: the whole thing measures 70.92018 pt. Add \ignorespaces at the end of the opening code and \unskip at the start of the closing code and it measures 63.1443 pt — not one scaled point different from writing \textbf{Note:}yes\textbf{.} tail by hand. The text pulled out of the PDF changes from Note: yes . tail to Note:yes. tail. Adding those two words to an environment definition pays off especially in collaborative manuscripts, where one author breaks the line after \begin and another does not, yet the output no longer wobbles.

latex
% leaks a space on both sides -- measured 70.92018pt
\newenvironment{naive}{\textbf{Note:}}{\textbf{.}}

% tight on both sides -- measured 63.1443pt, identical to hand-set text
\newenvironment{tidy}{\textbf{Note:}\ignorespaces}{\unskip\textbf{.}}

\begin{tidy} yes \end{tidy} tail

% \ignorespacesafterend also swallows the blank AFTER \end{...};
% use it only when the environment must butt against what follows.
  • Put the ties in first. Type Figure~\ref{...}, Section~2, 10~kg and Dr.~Smith with the tie already there; hunting them down later is expensive.
  • Remember that \ does two jobs: it restores a lost space and cancels sentence treatment. The second job is what makes Fig.\ 5 work.
  • Avoid \xspace by default. If you do use it, test at least a closing square bracket, a \% and a quotation mark before shipping.
  • \hspace* for gaps that belong to the layout, \hspace for gaps that only aid reading. Only the starred form survives at the start of a line.
  • Build \ignorespaces and \unskip into your environments on day one. Put them in when you define the environment and no stray space appears as the manuscript grows.