Braces in latex get escaped

I’m using a simple template with KaTeX and the following fails:
$$\mathcal I := { X\subset E : \text{the vectors in $X$ are linearly independent} }.$$

I suspect this is due to Hugo automatically replacing { → {, and I check this by testing $a_{b}$.

How can I go around this, so that my LaTeX code also works on other markdown parsers that do not do this replacement?

Going to bump this. To summarize, \{ is being replaced with {, probably due to HTML escaping.

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

If you post a link to your repo we can check what the difference are.

I’ve tested it on your repo: it has the exact same issue. You can try the following examples:

$$\{\text{this should have braces!}\}$$

$${A}_B C_{D}$$

Not sure what causes the problem without seeing your codes, the result of the KaTeX module seems work as expected.

\mathcal I := { X\subset E : \text{the vectors in $X$ are linearly independent} }.

\{\text{this should have braces!}\}

{{< katex >}}
{A}_B C_{D}
{{< /katex >}}


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 }}

and then calling this partial in single.html.

Perhaps this is the cause, you can check the generated HTML source code (CTRL+U on Chrome) to diagnose. KaTeX works with the .Content properly.

Yes, but what I am trying to do is to avoid the use of a custom shortcode. Anyway, this approach worked out fine.