4

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?

2 Answers 2

3

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>}
5

catcode changes affect how characters from a file are tokenized, but they have no effect on tokens that have already been made.

In your example #1 is the single token \TeX.

This is why you can not use \verb in arguments in latex.

If you are not restricted to classic tex you can use \detokenize so this works in pdftex but needs etex for dvi version.

enter image description here

\catcode`\<=\active
\def<#1>{\begingroup\tt\obeyspaces\frenchspacing\detokenize{#1}\endgroup}
\everymath{\catcode`\<=12\relax}
\everydisplay{\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

Note you get a space here after \TeX as character tokens, including a final space are re-constructed from the csname.

3
  • I am using XeTeX so I have indeed access to \detokenize, than you! The only issue is this final space. I tried unskip but it won't work for <\TeX\TeX> for example
    – user292932
    Mar 20, 2023 at 0:41
  • 2
    @John that is sort of not fixable. You could do the catcode changes as well, then a top level <.....> would work as you had it but one in an argument would behave differently. That tends to be confusing. or as here you always tokenize then detokenize. You can clean up a bit, eg an \unskip before the \endgroup would remove the trailing space, but < > would then make no output, not a space. Mar 20, 2023 at 0:49
  • So it seems to be as good as I can get, thank you for your help with that
    – user292932
    Mar 20, 2023 at 1:07

You must log in to answer this question.