I recently got a contribution to my Zen theme that implements KaTeX. Tested your example and it seems to work just fine. At least it renders the same way as it does on https://katex.org.
If you post a link to your repo we can check what the difference are.
Thanks for the response. This would not work for my setup, which instead just imports KaTeX directly - that supports my usage of $...$ and $$...$$ for any equations.
I’m having some progress by manually doing some transforms on the .RawContent. Something like:
{{ $content := .RawContent }}
<!-- this fixes issues with \\ not working correctly -->
{{ $content = $content | replaceRE "\\\\\\\\" "\\\\\\\\" }}
<!-- this fixes earlier issues with braces -->
{{ $content = $content | replaceRE "\\\\{" "\\\\{" }}
{{ $content = $content | replaceRE "\\\\}" "\\\\}" }}
<!-- this fixes issues with _ preceding a { or a \ -->
{{ $content = $content | replaceRE "\\_\\{" "\\_{" }}
{{ $content = $content | replaceRE "\\_\\\\" "\\_\\" }}
{{ $content | .Page.RenderString | safeHTML }}