6

I have a simple question: does it make any difference whether at some point I have two spaces instead of one in the source code, say after a period? That is, will PdfTeX ever output different things with two codes that, everything else being equal, differ in this:

end of. Sentence

vs.

end of.  Sentence

I am not asking about the difference between having \frenchspacing or not, I am really just interested in how multiple spaces in the source code are interpreted.

  • 2
    As far as I know, the answer is no. In other word: hello world and hello world gives the same output. – user156344 Feb 14 at 3:28
  • 1
    Edit of above comment: "hello_world" and "hello_ _ _ _world" are the same, except they are put in a "source-code" environment, like \verb|...|, lstlisting or minted. – user156344 Feb 14 at 3:45
  • No, but one should use Mr.~Smith or {Mr.} Jones when only one space is wanted. Okay, it probably isn't precisely a 2/1 ratio, but it is smaller. – John Kormylo Feb 14 at 4:43
  • @JohnKormylo {Mr.} doesn't affect space factor – David Carlisle Feb 14 at 10:01
  • @DavidCarlisle - Mr.{} Jones? Mr.\space Jones? – John Kormylo Feb 14 at 16:26
8

No. Multiple consecutive spaces are gobbled into a single space within the code, unless they are hard coded (like using ~ or \ - a control space - or via \hspace, or ...).

The setting of it might differ, however, depending on the other elements within the line of text. This is because the inter-word spacing can shrink/stretch as the paragraph setting is optimised. Here are some examples that hopefully illustrates this:

enter image description here

\documentclass{article}

\begin{document}

% Same end-of-sentence/period
This is text. Some more text.

This is text.  Some more text.

This is text.                    Some more text.

\hrulefill

% Regular space
This is text.\ Some more text.

This is text.~Some more text.

\hrulefill

\medskip

% Stretched inter-word space
\parbox{120pt}{This is text. Some more text.}

\medskip

\parbox{120pt}{This is text.\ Some more text.}

\end{document}
  • 4
    You might want to be ultra specific and say "multiple consecutive spaces". If something "invisible" (e.g. an \index entry) comes between two spaces in the input stream, each space is handled separately, and the output can look quite ragged. – barbara beeton Feb 14 at 4:40
6

The answer is: it depends.

In the normal TeX state, consecutive spaces in input are reduced to a single space token.1 See the referenced thread for more information about tokens and how TeX forms them from the input file. In what follows I'll distinguish between space character (SC) and space token (ST): a SC is what's found in the input file, a ST is the TeX internal object that might be produced by a SC during tokenization.

A consequence of the tokenization rules is that consecutive SCs in input are either reduced to a single ST or disappear altogether (leading SCs in a line). Thus inputs such as

end of. Sentence
end of.  Sentence
end    of.      Sentence

will give the same output, provided they are read in under the normal state, that is, when the SC has category code 10.2

Caveat. In an input such as x {} y, there are no consecutive SCs. This would be tokenized, under normal settings, as

x11 ST10 {1 }2 ST10 y11

In some contexts the SC is given a different category code, which of course change what happens during tokenization: the rules about reduction only hold when the SC has category code 10.

In principle one could assign the SC any of the sixteen category codes; if it is assigned category code 11 or 12, the effect of a SC would be to print the glyph in position 32 of the current font. The following simple plain TeX example

\catcode` =12 abc def  ghi\bye

produces

enter image description here

because the cmr10 font contains, in slot 32, the slash for producing ł, the Polish “suppressed l” (in inner circles the glyph is called lslashslash). Exercise: why doesn't the space after 12 yield an lslashslash?

More sensible changes are giving the SC category code 13 (active) or 9 (ignored). The former is the case of \obeyspaces, which is included in the settings for verbatim output. Basically \obeyspaces consists in declaring the SC to have category code 13 and so when a SC is encountered, it will behave like a macro and expand according to the current definition for it. The standard definition for the active SC is to yield a ST10

% latex.ltx, line 116:
\def\space{ }

% latex.ltx, line 558:
\def\obeyspaces{\catcode`\ \active}
{\obeyspaces\global\let =\space}

(it is the same in plain TeX). Note that after tokenization has been performed, no more reduction is done: if TeX is fed with two consecutive STs, it will use both. This is why an input such as

\verb+x y+
\verb+x  y+
\verb+x   y+

will result in one, two or three spaces, respectively, between “x” and “y”.

Category code 9 (ignored) for the SC is used for expl3 code. See a TUGboat paper of mine3

References

  1. What is a token?

  2. What are category codes?

  3. Recollections of a spurious space catcher

4

Yes.

\documentclass{article}
\begin{document}

\verb+abc abc+

\verb+abc  abc+


\obeyspaces
xx xx  xxx

\end{document}

enter image description here

2

There are already two good answers, one saying yes and the other no.

Here's the thing: Normally, multiple spaces are merged into a single space at a rather early stage in TeX's reading process. More precisely, on reading, any string of consecutive characters with character code 10 is coalesced into a single space token.

\documentclass{article}
\begin{document}
abba {\catcode`b=10 abba} abba
\end{document}

enter image description here

The only way around this is to change the category code of the space character. This is done, e.g., by \verb and \obeyspaces, as seen in Ulrike Fischer's answer.

Note, however, as Barbara Beeton says in a comment on Werner's answer, that anything coming between two space characters will defeat this mechanism, even if it produces no output of its own. Many LaTeX commands do extra work to detect when this happens, to compensate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.