8

I read the following sentence in TeX by Topic by Victor Eijkhout, in page 50:

The \string command is executed by the expansion processor, thus it is expanded unless explicitly inhibited.

Does it mean \string is expandable? But I use \meaning\string, and it only turns out \string. So what does it mean?

2
13

To find out whether something is expandable, slap it into an \edef and see what happens. Here we can for example do

\edef\test{\string\relax}
\show\test

Then in the terminal we see

> \test=macro:
->\relax.

Now \string is gone and the definition of \test is now just \12r12e12l12a12x12 where all the tokens have catcode 12 as indicated by the subscript, which is exactly the effect \string should have.

So yes, \string is expandable.


To answer the other part, when you use \show on a control sequence and you get something like

> \string=\string.

that means that this control sequence is a primitive.

4
  • 3
    Unhappily, the terminal or the .log file do not show any category codes. If \string would result in the token \relax it would severely damage its expandability. At least a missing space in the terminal tells that \relax is not the token \relax. – Heiko Oberdiek Apr 1 '18 at 5:13
  • 1
    The fact that \string doesn’t appear in the meaning of \test is by itself a sign that \string is expandable. – egreg Apr 1 '18 at 8:24
  • 1
    @egreg That's the nature of a macro. With \edef\test{\string\string}, the tokens \string appear in the meaning of \test. I think, the more interesting test is, whether the macro survives a \csname ...\endcsname (without calling the closing \endcsname). When it survives, the macro can be used in \label, bookmarks, ... – Heiko Oberdiek Apr 1 '18 at 9:16
  • @HeikoOberdiek a delimited macro could gobble all up to \endcsname, leave empty\endcsname and then do non-expandable things. (this is not quite the same \endcsname ;-) hence my comment...) – user4686 Apr 1 '18 at 16:09
4

With an interactive session.

> tex
This is TeX, Version 3.14159265 (TeX Live 2017) (preloaded format=tex)
**\relax

*\message{\string\relax}
\relax
*\message{\relax}
\relax 
*\bye
No pages of output.
Transcript written on texput.log.

The fact that \message{\string\relax} only prints \relax means that \string is expandable, because unexpandable tokens are written as themselves by a \message, like in the example with \message{\relax}.

By the way, inputting \message{\string} will print nothing on the console, because \string has performed its duty and stringified }, so the text for \message is unfinished. Typing in another } will end the loop and print }.

> tex
This is TeX, Version 3.14159265 (TeX Live 2017) (preloaded format=tex)
**\relax

*\message{\string}

*}
} 
*\bye
No pages of output.
Transcript written on texput.log.
7
  • perhaps add a note that \message does not behave versus braces like would macro definitions or for that matter expansion of user \def-ined macros – user4686 Apr 1 '18 at 16:13
  • 1
    @jfbu Did you try \edef\rbracestring{\string}}? Well, \message behaves like \edef. – egreg Apr 1 '18 at 16:15
  • I referred to \def not \edef... but +1 for mentioning your point. – user4686 Apr 1 '18 at 16:17
  • @jfbu Actually, they behave exactly the same; the point is that in the \edef TeX doesn't see the first } as a group close token, but as a catcode 12 character, because \string has been expanded. Instead, \def\foo{\string}} would be illegal because the first } still has category code 2. – egreg Apr 1 '18 at 16:21
  • well \def\bar{\def\foo{\string}} is legal... – user4686 Apr 1 '18 at 16:25
1

My biased grain of salt is that for expandable coding, the criterion will be more whether \string behaves well in an \if or \ifnum test, or in a \numexpr. Thus,

\ifnum\string1=1

not raising an error is good validation for me, because it means (suggests, rather...) I will be able to use such constructs in expandable coding.

That it expands fully in an \edef is less interesting, because I can not use \edef inside an expandable macro.

The \csname...\endcsname is more in the \ifnum category, from that point of view.

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.