Consider the following MWE:
\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\begin{document}
\lstinline |asdf|asdf asdfasdf
\verb |asdf|asdf asdfasdf
\end{document}
My understanding of what is to expect here has always been the following (let \cmd stand for either \verb or \lstinline in the following):
- When TeX first tokenized
\cmd |, it gobbles the space following it, leaving only the token\cmdin its "mouth" (and|behind it in the input stream). - It then expands
\cmd, which leads to a series of category code changes, basically making every otherwise special characterother, followed by some macro that looks at the next token (in this case,|). - This macro then grabs everything up to the next occurrence of that token (being tokenized then), applies some formatting and changes the category codes back.
Notably, the space following \cmd is gobbled during that control sequence's tokenization, i.e. before any category codes are changed.
With this understanding, I would expect both of the lines above to typeset
asdfasdf asdfasdf
But I get the following output:
\lstinline behaves as expected, but \verb somehow knows about the space following it.
How?? To my knowledge, there shouldn't ever have been a space token behind the \verb token.



\verb, it is done and complete, before ever reaching the|. – Steven B. Segletes Oct 09 '20 at 14:17\expandafter\verb |asdf|asdf asdfasdf. Presumably, in this case, the\expandafterwill gobble the spaces in search of the next token, so that\verbsubsequently no longer finds the space. – Steven B. Segletes Oct 09 '20 at 14:18\verbis safe.\expandafter\verb {asdf{would cause trouble. (Of course, not being able to just not type the space is not really a common problem, so this is more of an academic question.) – schtandard Oct 09 '20 at 14:24\expandafterwill cause the next token to be tokenized, I guess, which locks in its catcode. Since\verbis a game of catcodes, setting the catcode of{before\verbsees it would, logically, cause problems. – Steven B. Segletes Oct 09 '20 at 14:31\csname verb\endcsname |asdf|asdf asdfasdfalso will not gobble the space after\endcsname, unless I add an\expandafterbefore the\endcsname. – Steven B. Segletes Oct 09 '20 at 14:36\expandaftertrick include},%, and active characters, such as~. – Steven B. Segletes Oct 09 '20 at 14:42\tracingallwith\verb |asdf| %, you'll see that\@sverbgrabs one argument which is an explicit space token, probably coming from the space character following\verb. Why this space character hasn't been discarded when\verbwas tokenized, I don't know.\@sverbmakes the grabbed token\let-equivalent to\verb@egroup, which yields an\egroupmatching... – frougon Oct 09 '20 at 14:54\bgroupin\verb. Tokens in-between are simply processed as catcode-12 tokens, except space tokens which are active in this context (this is due to the use of\@vobeyspacesby\@verbhere, or by\@sverbwhen coming from\verb*). – frougon Oct 09 '20 at 15:04\expandafterwill gobble the spaces in search of the next token”: no, the\expandafteractually hits the space token, not expanding it, but freezing its catcode 10, then the\@ifstarin\verbwill ignore (catcode 10) space tokens as usual – Phelype Oleinik Oct 09 '20 at 16:03