\newcommand doesn't allow overwriting an existing definition. It worked fine in Jupyter Notebook, which uses MathJax, which apparently doesn't actually check this. However, it causes errors in VS Code, which uses KaTeX. It probably also caused errors when converting a notebook to LaTeX and typesetting it with a proper LaTeX implementation. \def overwrites any existing definition and so should work in all cases.