18

Whenever I type a space after a colon (:) in a \texttt{} command I end up with extra space — here is a minimal example:

\documentclass[12pt]{article}

\thispagestyle{empty}

\begin{document}
\section*{Test Space after Colon}  

\texttt{1234 : 8901}\\
\texttt{12345678901}

\vspace{\baselineskip}

\noindent 
The above was produced by 
\begin{verbatim}
\texttt{1234 : 8901}\\
\texttt{12345678901}  
\end{verbatim}
and has extra space after 
the colon (:) 

\vspace{\baselineskip}

\noindent 
\textsf{1234 : 8901}\\
\textsf{12345678901}

\noindent 
The above was produced by 
\begin{verbatim}
\textsf{1234 : 8901}\\
\textsf{12345678901}  
\end{verbatim}
and has no extra space after 
the colon (:) 

\end{document}

The above code produces

Test space after colon

As you can see in the example, the extra space does not appear in the \textsf{} command for example.

Any idea what is going on here?

  • 1
    It seems to come from the punctuation feature just as with ., try adding \frenchspacing in the preamble. Unsure why this hit \texttt in particular and not \textsf – daleif Jun 10 '13 at 12:26
  • 2
    @daleif sorry I didn't see your comment until after I had posted. The effect does hit \textsf but it is less pronounced there/ – David Carlisle Jun 10 '13 at 12:31
15

This is the affect of the default \nonfrenchspacing which adds extra space after sentence/clause ending punctuation. The monospacing of \texttt highlights the effect.

\texttt{\frenchspacing1234 : 8901}\\

produces even spacing as does

\verb|1234 : 8901|\\
  • Thanks for this — I guess I hadn't hit this before since I usually use verbatim, fancyvrb or the listings environments for code. This is covered on pages 72–74 of the TeXbook – Phil Molyneux Jun 10 '13 at 12:47
7

The space after the colon is bigger also in \textsf, but much less than in \texttt. If I try

\sbox0{\textsf{1234 : 8901}}\showthe\wd0
\sbox0{\frenchspacing\textsf{1234 : 8901}}\showthe\wd0

I get

> 59.40216pt.
l.28 \sbox0{\textsf{1234 : 8901}}\showthe\wd0

?
> 58.09662pt.
l.29 ...chspacing\textsf{1234 : 8901}}\showthe\wd0

so an extra 1.3pt is added without frenchspacing. The abnormal spacing in \texttt is due to the font spacing parameters; these are those for cmtt10

(FONTDIMEN
   (SLANT R 0.0)
   (SPACE R 0.524996)
   (STRETCH R 0.0)
   (SHRINK R 0.0)
   (XHEIGHT R 0.430555)
   (QUAD R 1.049991)
   (EXTRASPACE R 0.524996)
   )

while cmss10 has

(FONTDIMEN
   (SLANT R 0.0)
   (SPACE R 0.333334)
   (STRETCH R 0.166667)
   (SHRINK R 0.111112)
   (XHEIGHT R 0.444445)
   (QUAD R 1.000003)
   (EXTRASPACE R 0.111112)
   )

and EXTRASPACE is the culprit here, together with the space factor code associated to the colon, which is 2000.

Now, \ttfamily can be used for emulating a typewriter and space factor works like typists were used to do (at least in the US), leaving additional space after sentence ending periods and colons.

You can redefine \ttfamily to be "non space extending" by adding the following code to your preamble

\makeatletter
\let\ttfamily\relax % avoid a spurious warning
\DeclareRobustCommand\ttfamily
        {\not@math@alphabet\ttfamily\mathtt
         \fontfamily\ttdefault\selectfont\frenchspacing}
\makeatother

The command \texttt uses internally the current definition of \ttfamily, so you don't need to redefine it.

This is simply the kernel definition of \ttfamily, with \frenchspacing added. More simply,

\usepackage{xpatch}
\xapptocmd{\ttfamily}{\frenchspacing}{}{}

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.