Here's a line of erroneous code.
\documentclass{standalone}
\begin{document}
\frac{a}{M}
\end{document}
As we expect, the compiler throws a "missing $" error.
! Missing $ inserted.
<inserted text>
$
l.91 \frac{a}{M}
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
My question is: how are the missing $ inserted?
I expect that the system would insert $ at both the start and the end of line, so that the compiler would give
However, the compiler actually gave me this.
I dont't understand why the first token list {a} of the \frac command was interpreted as text.
When I try math mode only commands, the first token list would be interpreted in math mode. For example, frac{\sqrt2}{M} gives this.



\fracexpands to\begingroup #1\endgroup \over #2. Thea(in#1) doesn't need math-mode so it doesn't trigger the error but\overdoes →a $\over M $. But with\sqrtthis gets triggered twice: at the\sqrtand then at the\over. Compare\begingroup \sqrt{2} \endgroup avs\sqrt{2} a. But I'm sure there is more to say about whatever TeX is doing deep down. – Qrrbrbirlbel Oct 25 '22 at 21:20ais legal in text mode, but\sqrt{2}isn't. – egreg Oct 25 '22 at 21:51