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?

Alan Munn
  • 218,180
Evian
  • 211
  • 1
  • 2

3 Answers3

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.

Henri Menke
  • 109,596
  • 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 01 '18 at 05: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 01 '18 at 08: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 01 '18 at 09: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...) –  Apr 01 '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.
egreg
  • 1,121,712
  • 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 –  Apr 01 '18 at 16:13
  • 1
    @jfbu Did you try \edef\rbracestring{\string}}? Well, \message behaves like \edef. – egreg Apr 01 '18 at 16:15
  • I referred to \def not \edef... but +1 for mentioning your point. –  Apr 01 '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 01 '18 at 16:21
  • well \def\bar{\def\foo{\string}} is legal... –  Apr 01 '18 at 16:25
  • @jfbu But doing \bar would not define \foo to a category code 12 }. – egreg Apr 01 '18 at 16:32
  • @jfbu: See also The TeXbook, exercise 20.17. – GuM Apr 01 '18 at 23:33
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.