For indexing I wanted to write a macro \macroname
that removes leading backslashes from macro names but leaves the names of environments untouched. That is
\macroname{\relax} --> relax
\macroname{itemize} --> itemize
where the -->
is meant to be read as "expands to".
From my understanding, the following should work:
\newcommand\removebs[1]{\if#1\char92\else#1\fi}
\newcommand\macroname[1]{%
\expandafter\removebs\detokenize{#1}}
However, its outcome is
\macroname{\relax} --> \relax % literally
\macroname{itemize} --> itemize
By chance, actually, I found out that if I change the catcode of the backslash to "other" during the definition of the \removebs
macro, it works:
{
\catcode`\|=0
|catcode`|\=12
|global|def|removebs#1{|if#1\|else#1|fi}
}
\newcommand\macroname[1]{%
\expandafter\removebs\detokenize{#1}}
Why is that? From TeX by Topic I'd expect that the comparison made by \if
is what Philipp Lehman calls "category code agnostic".
Thanks for your answers!
\char
and they are different;\char92
is an instruction for printing the character 92 from the currect font.