There are various approaches, as some of the comments indicate. There are a couple of 'awkward' cases to cover: if the escape character is non-printable, and if the control sequence starts with a space (such as
\csname\space awkward\endcsname). In
expl3, we cover this with a function called
\cs_to_str:N, which in primitives would be written as
and used as
\cs@to@str\foo % => "foo"
\expandafter\cs@to@str\csname\space foo\endcsname % => " foo"
To explain how this works, there are three cases
The escape character is printable and not a space. In this case,
\string\ gives two tokens (the escape character and a space), which have different catcodes. The
\if test is therefore false, and so the second
\string can safely be used, with
\cs@to@str@aux@ii removing the escape character.
The escape character is not printable. In this case,
\string\ yields only one token, and the test continues, expanding
\cs@to@str@aux@i. The insertion of
- forces the test to be false and
\string can be used directly to give the desired output.
The escape character is a space. In this case, the
\if test is true, and
\cs@to@str@aux@i leads to the insertion of
\number turns into
-0. That's not 'terminated', so the
\romannumeral goes looking for an optional space. It finds this in the escape character, which is therefore removed leaving just the control sequence name.
In all three cases, notice that we never have the possibility to remove a leading space from the name itself as we never remove a token that is not the escape character.
As this is all built-in to
expl3, I'd just create a document-level alias:
(you can do this with
xparse, of course).