A \scantokens
free solution is the following:
\newcommand{\comp}{\begingroup\mathcode`,=\string"8000 \compx}
\def\compx(#1){\begingroup\lccode`~=`,
\lowercase{\endgroup\def~}{\mskip 5mu}#1\endgroup}
which has also the advantage that it's not necessary to have a global definition of an active comma. In this code the definition of the active comma is done only at the last moment; of course it's slightly less efficient, but safer, as it limits risks of conflicts.
Caution. Don't use a comma in the definition of the active comma (in the above code the bit {\mskip 5mu}
); if it's needed, then
\mathchardef\normalcomma=\mathcode`,
issued before tinkering with catcode and mathcode of the comma allows to use \normalcomma
in the definition, with the usual meaning for the comma.
More general implementation
\documentclass[a4paper]{article}
\usepackage{amsmath}
\makeatletter
\@ifundefined{@xp}{\let\@xp\expandafter}{}
\newcommand\DeclareMathActive[2]{%
\ifnum\catcode`#1=12
\DMA@math@active{#1}{#2}%
\else
\ifnum\catcode`#1=11
\DMA@math@active{#1}{#2}%
\else
\ERROR
\fi
\fi}
\def\DMA@math@active#1#2{\@namedef{DMA#1ACTIVE}{#2}%
\@xp\mathchardef\csname DMA#1INACTIVE\endcsname=\the\mathcode`#1 }
\newcommand{\mathactivate}[1]{%
\ifnum\catcode`#1=12
\DMA@math@activate{#1}%
\else
\ifnum\catcode`#1=11
\DMA@math@activate{#1}%
\else
\ERROR
\fi
\fi}
\def\DMA@math@activate#1{%
\mathcode`#1=\string"8000
\begingroup\lccode`\~=`#1
\@xp\let\@xp\@tempa\csname DMA#1ACTIVE\endcsname
\lowercase\@xp{\@xp\endgroup\@xp\def\@xp~\@xp}\@xp{\@tempa}}
\newcommand{\mathinactive}[1]{\@nameuse{DMA#1INACTIVE}}
\DeclareMathActive{,}{\mskip 5mu}
\begin{document}
$\mathactivate{,}1,2,3$
\DeclareMathActive{,}{abc}
$\mathactivate{,}1,2,3\mathinactive{,}$
\begin{align}
a&=\mathactivate{,}1,2,3
\end{align}
\DeclareMathActive{,}{\mathinactive{,}aaa}
$\mathactivate{,}1,2,3$
\end{document}
The \ERROR
command is undefined and should be given a proper meaning in a real world example.
\DeclareMathActive
has two arguments: the character and its requested definition. In the second argument one must not use the same character unadorned, but \mathinactive{<char>}
is allowed as the last example shows.
One can give new definitions for the character, which obey the normal scope rules. However, if the math activation is required in more than one cell in an alignment, it should be either done outside the alignment or in each cell where it's needed.
\usepackage{amsmath}
your code seems to work fine.