relates to What are the valid names as labels? .
\newcommand{\keyword}[1]{\textbf{#1}\label{KW:#1}\pageref{KW:#1}}
works just great for almost any text...until one wants \keyword{S\&P~500}. The problem is that label does not allow a \ to be part of the label, but it is the only way to typeset the & character in the text. Is there an \xlabel command that is a little more forgiving, i.e., first stripping or mangling any invalid characters from #1, after expanding it once of course? (I tried changing the catcode of either & or \\ in the macro, but couldn't figure it out. I can't assign to a variable and include it for the same reason that it would contain a \ again.)
&is valid in a\label; on the contrary, control sequences should be avoided. – egreg Nov 03 '21 at 21:07