11

How does this exactly work?

\expandafter\string\csname\endcsname

I don't know what I would expect to come from there, but definitely not \csname\endcsname_ where _ is a space.

I thought \csname<name of cs>\endcsname would expand in one expansion to the control sequence <name of cs>. Now, I thought \string would print the next token, and, if it's a control sequence print \escapechar<name of cs>, but I'm getting something different. Where's the problem?

11

The main purpose of \string\zzzz is to write something to a file that when read back in produces a command token with name zzzz. The most general way to get the command token with name the empty string is to use \csname\endcsname so TeX defines \string to produce that if the following token is a command token with name the empty string.

Another way to get the command with empty name is to use \ at the end of the line, this normally produces \^^M, as \endlinechar is normally 13 but if \endlinechar is negative or greater than 255 then no character is added to the end of the line, and in that case the same token as \csname\endcsname is created. This representation isn't suitable as the output for \string though as it only works at the end of the line, and with a non standard setting of \endlinechar.

  • My problem wasn't with the space. But as you say “that when read back in produces a command token with name…” okey, didn't now that was the behaviour of \string. In any case, why no space after \csname? – Manuel Feb 13 '15 at 10:02
  • 3
    @Manuel It's just the way it is. It is hard coded into TeX the program that it produces \csname\endcsname other things would have been possible but only one man can answer exactly why that exact form was chosen. Just an arbitrary choice really. – David Carlisle Feb 13 '15 at 10:04
  • 1
    So \csname csname\string\endcsname\endcsname equals \csname\endcsname as far as \string or \detokenize is concerned. – Manuel Feb 13 '15 at 10:10
  • @Manuel in the latest tex release, yes, assuming mormal catcode and escapechar settings – David Carlisle Feb 13 '15 at 10:19
  • 1
    @Manuel Indeed: \showtokens\expandafter{\detokenize\expandafter{\csname csname\string\endcsname\endcsname}}\showtokens\expandafter{\csname\endcsname} – Joseph Wright Feb 13 '15 at 15:06

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.