[Tex/LaTex] How to Find the Length of a Space in TeX

horizontal alignmentspacing

I have implemented the horrific document

\documentclass{article}
\newcommand{\testspace}[1]{\hspace*{\fill}\(\vert\)\hspace*{\fill}\\
                           \null\space\(\vert\)\hspace*{3.3pt plus #1pt}\break}
\begin{document}
\noindent
\(\vert\)\space\(\vert\)\\
\(\vert\)\hspace{3.3pt}\(\vert\)\\
\testspace{1.665}
\end{document}

This allows me to visually examine each vertical bar to see which ones line up the best, and therefore discover how much stretch an individual control space has. I estimate the width of a space to be about 3.3pt plus 1.665pt, however, I can't shake the feeling that this is awful. Nevertheless, TeX (and I think it is TeX and not LaTeX) will not let me use \the\space. How should I figure out the length of a single space?

Best Answer

The width of normal space depends on current font. There are global metric items in each font called \fontdimen. The \fontdimen2 includes the basic width of normal space, \fontdimen3 includes value atfer plus and \fontdimen4 includes value after minus. The \fontdimen7 includes additional space used when \spacefactor\ge 2000.

Because the \font primitive used in the context \fontdimen<number>\font denotes the current font (and no normal declaration primitive), we can print the values of normal space by the code:

\message{space: \the\fontdimen2\font\space 
   plus \the\fontdimen3\font\space minus \the\fontdimen4\font}

Computer Modern roman at 10 pt gives the following result:

space: 3.33333pt plus 1.66666pt minus 1.11111pt

And you can use \hskip primitive for creating normal space by

\hskip \fontdimen2\font plus\fontdimen3\font minus\fontdimen4\font

but this is only for illustration purposes. Normal people use \spaceor \ (control space).

EDIT: The normal space doesn't generate the space mentioned by \hskip above at all circumstances, because there are many exceptions. If the register \spaceskip is nonzero then \fontdimens are not used but \spaceskip register is used. Moreover, if \xspaceskip is nonzero and the current \spacefactor\ge 2000 then \xspaceskip is used.

There are two differences between \ (control space) and \space (or normal space between words, token of catcode 10):

  • \space is ignored in vertical mode but \ in vertical mode starts horizontal mode.
  • \space respects the current \spacefactor but \ does the same as \space when \spacefactor=1000.

Finally, what does mean that the space respects current \spacefactor. Let f=\spacefactor/1000 and b is basic width of the space, p is plus value and m is minus value. Then p is modified by p:=p*f and m is modified by m:=m/f and b is kept unchanged. But, if \spacefactor\ge2000 then b:=b+\fontdimen7 (or \xspaceskip is used if it is nonzero).

I add an example from my TeXbook naruby, page 105: Suppose the normal space (when \spacefactor=1000) as 10pt plus10pt minus10pt. When \spacefactor=600 then the space 10pt plus 6pt minus16.666pt is inserted, when \spacefactor=1500 then 10pt plus15pt minus6.666 is inserted. Finally when \spacefactor=3000 then 10pt+\fontdimen7 plus 30pt minus 3.333pt is inserted.

Normally, \spacefactor is 1000, but in some languages the \nonfrenchspacing macro sets special \sfcodes to several characters. The \spacefactor is set to the \sfcode value of the last typeset character in horizontal mode. (There is an exception when \spacefactor<1000 and \sfcode>1000. Then new \spacefactor=1000.) For example plain TeX sets \sfcode of period, question and exclamation mark to 3000, comma to 1250, semicolon to 1500 and colon to 2000.