I want to modify the catcode of comma, with the following code
\documentclass{article}
\AtBeginDocument{%
\catcode`\,=\active
\protected\def,{...}%
}
\begin{document}
a,b,c $a,b,c$.
\end{document}
I get the following error message:
! Missing control sequence inserted.
<inserted text>
\inaccessible
l.6 \begin{document}
But with the following code, I get correct result:
\documentclass{article}
\begingroup
\catcode`\,=\active
\protected\gdef,{...}%
\endgroup
\AtBeginDocument{%
\catcode`\,=\active
}
\begin{document}
a,b,c $a,b,c$.
\end{document}
I'm wondering what are the differences between these two ways. Why the first way causes problem?
Edit: Althougn David Carlisle give another related question, but I think this question maybe not the same as that question, since both examples contains
\AtBeginDocument{%
\catcode`\,=\active
}
I could not understand why only one example causes problem.

\verbto not apply to tokens, so do not work in the argument of another command) – David Carlisle May 22 '15 at 09:34\catcode,=\activeinside\AtBeginDocument` in both of the examples. I think it is not the same as your linked answer. – Z.H. May 22 '15 at 10:04\catcode`\,=activeoutside of\AtBeginDocumentas well: the tokenization needs to be correct. – Joseph Wright May 22 '15 at 10:11\AtBeginDocumentI'm not sure what difference you see? – David Carlisle May 22 '15 at 10:11\mbox{\catcode\,=13 zzzz , zzzz}it is a normal, non-active,between thezzz` whatever that code is doing, definition or use. – David Carlisle May 22 '15 at 11:05\specialand a "fake" version of TeX with different handling of\def? – 1010011010 May 22 '15 at 12:25\catcode\,=13\mbox{ zzzz , zzzz}` obviously if you use some unspecified non-tex program it could do whatever you specify, but it would have to be very different from TeX so the question of making some Tex syntax work on that new system may not make sense at all. – David Carlisle May 22 '15 at 12:28\scantokenscommand. – Z.H. May 22 '15 at 13:02