1

I'd like to get the name of a control sequence token as sequence of character tokens.

With \relax I'd like to get relax.

The preceeding escape character is to be removed from the result of \string\relax.

One can probably do

\def\gobble#1{}%
\expandafter\gobble\string\relax

But do I understand correctly?:

Actually behavior of \string depends on the value of the integer parameter \escapechar.

If \escapechar < 0 or \escapechar > 255/1114111 no escape character is prepended by \string.

If value of \escapechar is 32, then space token (charcode 32, catcode 10) is prepended by \string. Space token cannot be handled as undelimited argument. So we have a case that needs special handling.

In all other cases character token is prepended with character code=value of \escapechar and category code=12/other.

So if you cannot rely on \escapechar=92 you need to do like this:

\lowercase{\def\spacetokengobble} {}%
\def\gobble#1{}
\def\empty{}
\csname\ifnum\escapechar<0 empty\else
          \ifnum\escapechar=32 spacetokengobble\else
            \ifnum\expandafter\ifx\csname XeTeXrevision\endcsname\relax
                  \expandafter\ifx\csname directlua\endcsname\relax
                   255\else 1114111\fi\else 1114111\fi<\escapechar
            empty\else gobble\fi
          \fi
       \fi
       \expandafter
\endcsname\string\relax

Question:

With the value of \escapechar, besides the value 32, are there more special values where removing cannot be done by \gobble?

  • Besides invalid values, I think only 32 requires special attention because \string makes space characters have catcode 10, which is ignored by TeX when grabbing arguments. I suggest you take a look at expl3's \cs_to_str:N: it does precisely what you want – Phelype Oleinik Feb 22 '21 at 13:53
  • only 32 (space) is special but probably no one has ever done that:-) but you can set it to -1 and use \string then there is no need to gobble anything, as you say,or do you really need an expandable construct here? – David Carlisle Feb 22 '21 at 13:54
  • @DavidCarlisle I intend to use it inside \csname..\endcsname to extend a macro name by a prefix before defining the macro. So I think an expandable solution is needed. – JamieLittleJoeHossAdamBen Feb 22 '21 at 14:11
  • 1
    @PhelypeOleinik i started out with TeXbook two months ago. expl3 is inscrutable to me, sorry. – JamieLittleJoeHossAdamBen Feb 22 '21 at 14:13
  • @JamieLittleJoeHossAdamBen "expl3 is inscrutable to me, sorry." I felt the same when I started using expl3. But you get used to the syntax quickly. ;-) – Ulrich Diez Feb 22 '21 at 14:41
  • @JamieLittleJoeHossAdamBen Your approach works fine, so if you wrap it into a definition it should work. The expl3 implementation, _ and : removed for your convenience, is \chardef\czeroint=0 \def\cstostr{\romannumeral\if\string\ \cstostrw\fi\expandafter\cstostrN\string} \def\cstostrN#1{\czeroint} \def\cstostrw#1\cstostrN{-\number\fi\expandafter\czeroint}. Use as \cstostr\macro. – Phelype Oleinik Feb 22 '21 at 15:00
  • if your main intention is to define a macro, then that definition isn't expandable anyway why restrict the logic to being inside \csname?? look how latex2e defines \newcomand and friends, before processing the argument it does \begingroup \escapechar\m@ne... so \string acts in a known way. – David Carlisle Feb 22 '21 at 17:36
  • @JamieLittleJoeHossAdamBen In LuaTeX, there is \csstring primitive which does the same as \string but without any escapechar. The \escapechar with \string is bad concept which brings only problems for macro programmers. If you are using modern TeX engine LuaTeX then you can throw these problems behind your hand and focus to real problems you are solving. The \csstring primitive is menitoned in tex-nuthsell.pdf and in LuaTeX manual, for example. – wipet Feb 24 '21 at 20:00
  • Link to related question https://tex.stackexchange.com/questions/12508/how-to-get-the-string-with/12515#12515 – user202729 Nov 23 '23 at 09:46

0 Answers0