# The command \string does not print curly braces of its argument

While printing the List of Symbols, I would like, in a draft, to print the definition commands as well. For instance, consider the following commands:

\providecommand{\perm}{\ensuremath{\pi}}
\providecommand{\permel}[1]{\ensuremath{\perm(#1)}}


and the following command for printing symbols within a table:

\providecommand{\symbdef}[2]{\texttt{\color{blue}\string #1} & #1 & #2}


Now, the code:

\section{List of Symbols}
\begin{tabular}{lll}
\symbdef{\perm}{a permutaion} \\
\symbdef{\permel{i}}{the value of \perm for the argument $i$} \\
\end{tabular}


proudeces output: \perm and \permeli in the first column. However I would like to print \permel{i} instead of \permlei here and do not know how.

-

\string only takes one token and prints that, so it only ever sees \permel.

Try

\providecommand{\symbdef}[2]{\texttt{\color{blue}\detokenize{#1}} & #1 & #2}


This will insert explicit spaces after control sequences, which is not semantically wrong, but might not be aesthetically pleasing. In that case, try filtering out the spaces like this:

\providecommand{\symbdef}[2]{\texttt{\color{blue}\expandafter\filterspaceaftercs\detokenize{#1\x}_} & #1 & #2}

\begingroup
\makeatletter
\catcode\|=\z@
\catcode\\=12
|gdef|filterspaceaftercs#1\#2 #3_%
{%
#1%
|ifx|empty#3|empty
|expandafter|@gobble
|else
\#2|expandafter|filterspaceaftercs
|fi
#3_%
}
|endgroup


-
Which will insert spaces after control sequences, of course. –  Joseph Wright Aug 29 '12 at 10:59
@JosephWright As does \show and such, so it's a feature :-) A more elegant solution probably requires verbatim style trickery. –  Stephan Lehmke Aug 29 '12 at 11:05
Yes, I known :-) I was just contrasting it with \string, which does not but where you do need to do a token-by-token conversion (tricky, depending on requirements). –  Joseph Wright Aug 29 '12 at 11:06
@JosephWright ...in which case you'd get the complementary problem with spaces... –  Stephan Lehmke Aug 29 '12 at 11:08
I recoil with dismay and horror at this lamentable plague of macros which do not have expandable versions. — Charles Hermite :P –  percusse Aug 29 '12 at 11:51