0

Over time have seen csname but I never had to learn it until now.

But after reading @egreg's (beautiful!) answer to What exactly do \csname and \endcsname do?, I am not so sure if this is good coding.

Here is a test showing strange bahaviour:

  1. \csname endcsname
  2. \csname endcsname \endcsname
  3. \csname backslash endcsname\endcsname

The first two worked as expected. i.e. they generated errors, but the last one gave no error. I would love to understand what I missed!?

(Certainly, there are pros: you may reuse code and you can tweak space etc.)

  • 2
    I assume you mean csname not cscode ??? – David Carlisle Dec 08 '21 at 14:14
  • \csname..\endcsname can be used nicely for triggering expansion. Besides this \csname..\endcsname can be used for creating control-word-tokens whose names contain characters which are not of category code 11(letter). TeX does tokenize \ab12 as tokens \ab, 1, 2. \csname ab12\endcsname yields a single control-word token \ab12. – Ulrich Diez Dec 08 '21 at 14:43
  • the use of \csname is for generating command tokens eg \begin{...} is more or less defined to be \begingroup\csname #1\endcsname as it needs to convert the environment name to a command. So if you are defining such a "meta" command you may need \csname if you are not, then you do not need it. – David Carlisle Dec 08 '21 at 15:36
  • Try using expl3, most of the time you can get away with :c-type argument. (also try using LuaTeX, you don't need to understand what a control sequence is at all) – user202729 Dec 08 '21 at 16:02

1 Answers1

3
  1. \csname endcsname
  2. \csname endcsname \endcsname
  3. \csname backslash endcsname\endcsname

1 will give a missing \endcsname error if there is a non-character token (after expansion) before a following \endscsname

2 will define the command endcsname<space> to be \relax and then execute it, this is the behaviour for any undefiend command accessed by \csname.

3 will similarly define backslash<space>endcsname to be \relax

The actual question in the title (assuming you meant \csname not cscode) is probably "No.". That is, it is a very specialised command useful in some internal macro definition contexts, but shouldn't be used in a document.

David Carlisle
  • 757,742