Fix this simple verbatim macro (plain TeX)

plain-texverbatim

I have written a simple macro to typeset everything enclosed in angle brackets with a special font and to ignore the backslash character.

\catcode`\>=\active
\def>{\endgroup}
\catcode`\<=\active
\def<{\begingroup\tt\obeyspaces\frenchspacing\catcode`\\=12\relax}
\everymath{\catcode`\<=12\catcode`\>=12\relax}
\everydisplay{\catcode`\<=12\catcode`\>=12\relax}

It works just fine when typing direcly but fails when passed into another macro, e.g.,

<\TeX> % correct
\def\identity#1{#1}
\identity{<\TeX>} % incorrect, does expand for some reason

\bye

Is there a way to fix this?

Best Answer

You can use \scantokens (also from e-tex) to retokenize instead of \detokenizeing. Then it does correctly add spaces (consecutive spaces are ignored, you can change this by doing \catcode`\ =\active after \dospecials). And since it retokenizes, you can pass it to a macro.

\catcode`\<=\active
\def<#1>{{\tt\frenchspacing\def\do##1{\catcode`##1=12\relax}\dospecials\scantokens{#1}}}

<\TeX\TeX>
<\TeX \TeX>
\def\identity#1{#1}
\identity{<\TeX>}