6

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
  • 1
    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
8

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}
Some verbatim text: \verb*|$%& and some spaces|.
\end{document}

Addition. Expanding on @egreg’s comment, it could be remarked that, with the introduction of T1-encoded font, the arrangement described above has been made more robust than it was formerly (with OT1 encoding): indeed, under the T1 encoding, all the slots of a font that correspond to printable ASCII characters must (or should…) contain a glyph suitable for representing that same character; and even if, strictly speaking, the space character is not a printable character, nonetheless T1-encoded fonts should contain, in slot 32, exactly the glyph for the “visible space”. It should also be remarked, on the other hand, that in general OT1-encoded fonts fail to represent verbatim also other characters besides the space; for example, they also lack suitable glyphs for the {, \, and } characters.

Both these points are illustrated by the following compilable example:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}

\makeatletter

\newcommand*\SetVerbatimFont[4]{%
    \def\verbatim@font{\usefont{#1}{#2}{#3}{#4}}%
}
\newcommand*\ShowVerbatimFont{%
    {\verbatim@font \fontname\font\/}%
}

\makeatother



\begin{document}

\setlength{\parskip}{\bigskipamount}

Some verbatim text in the default verbatim font:\\
\verb*|$%{}\~& and some spaces|\\
(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:

Output of the second code sample

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.

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

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.