# How does \verb* set the visible space?

I'm trying to understand how \verb* works under the hood, especially where the mapping for the visible space character comes into play. Let's have a look at the definitions first:

\verb:
macro:->\relax \ifmmode \hbox \else \leavevmode \null \fi \bgroup \verb@eol@error
\let \do \@makeother \dospecials \verbatim@font \@noligs
\@ifstar \@sverb \@verb

\@verb:
macro:->\@vobeyspaces \frenchspacing \@sverb

\@vobeyspaces:
macro:->\catcode \ \active \let  \@xobeysp

\@xobeysp:
\long macro:->\leavevmode \nobreak \


So far, so clear. First the usual catcode setup for special characters, font setting etc. When there is no star, \@vobeyspaces is called,   is made active and an explicit space token is inserted for each space character. Then comes \@sverb:

\@sverb:
macro:#1->\catcode #1\active \lccode \~#1
\gdef \verb@balance@group {\verb@egroup \@latex@error {\noexpand \verb illegal in command argument}\@ehc }
\aftergroup \verb@balance@group \lowercase {\let ~\verb@egroup }


However, in the starred case \@sverb is called directly. I would expect a similar command being called here that makes all space characters map to the symbol for visible space tokens. But that is not the case. All the things done in \@sverb only seem to be related to finding the end of the verbatim text and some extra error handling.

Where in all these definitions does the actual mapping to the visible space character occur?

• In the teletype font, the symbol for the visible space is found in the ASCII slot for the space… – GuM Aug 28 '18 at 7:41
• an explicit space token is inserted for each space character.: perhaps more precisely space character is tokenized to active space token, the latter being defined to expand to (essentially) the "control space". – user4686 Aug 28 '18 at 8:17

First of all, note that \dospecials includes the space character, so the \catcode of the space character is turned into 12 (“other”) by the \verb macro. Second, do not forget that, in the cmtt font, the symbol for the visible space is found in slot 32, that is, precisely in the slot allotted to the space character. So, exactly as you don’t need to do anything special in order to make the \verb command typeset an “A” character when an A is found in the input, in the same way no special provision is needed in order to make a space character in the input to be typeset as a “visible space” symbol. You have to arrange things in some special way if you want spaces in the input to be translated into genuine spaces in the printout.

Of course, if a different font is used for verbatim output, it must contain a similar symbol in the same slot, otherwise the printout won’t look right: see, for example, the output of the following (compilable) code snippet:

\documentclass{article}

\makeatletter
\def\verbatim@font{\normalfont}
\makeatother

\begin{document}
(it looks right).

\SetVerbatimFont{OT1}{cmr}{m}{n}
Some verbatim text in \ShowVerbatimFont:\\
\verb*|$%{}\~& and some spaces|\\ (it looks wrong). \SetVerbatimFont{OT1}{cmr}{bx}{n} Some verbatim text in \ShowVerbatimFont:\\ \verb*|$%{}\~& and some spaces|\\
(it looks wrong).

\SetVerbatimFont{T1}{cmtt}{m}{n}
Some verbatim text in \ShowVerbatimFont:\\
\verb*|$%{}\~& and some spaces|\\ (it looks right). \SetVerbatimFont{T1}{cmr}{m}{n} Some verbatim text in \ShowVerbatimFont:\\ \verb*|$%{}\~& and some spaces|\\
(it looks right).

\SetVerbatimFont{T1}{cmr}{m}{it}
Some verbatim text in \ShowVerbatimFont:\\
\verb*|$%{}\~& and some spaces|\\ (it looks right). \SetVerbatimFont{T1}{pcr}{m}{n} Some verbatim text in \ShowVerbatimFont:\\ \verb*|$%{}\~& and some spaces|\\
(it looks right).

\SetVerbatimFont{T1}{ptm}{m}{n}
Some verbatim text in \ShowVerbatimFont:\\
\verb*|\$%{}\~& and some spaces|\\
(it looks right).

\end{document}


This is the output:

Note that the second and third paragraphs illustrate the case of two fonts that are inadequate for verbatim typesetting, showing that they not only misrepresent space characters, but also {, }, \, and ~. On the other hand, the last two paragraphs use PostScript fonts (Courier and Times) with T1 encoding, and you can see that the output is correct.

• On the other hand, with \usepackage[T1]{fontenc} the output would be “correct”. – egreg Aug 28 '18 at 11:11