# Braces in latex get escaped

**URL:** https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742
**Category:** support
**Tags:** typesetting
**Created:** [July 28, 2022, 7:45pm UTC](https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742 "2022-07-28T19:45:38Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dvddddddvd](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dvddddddvd/32/17415_2.png) [@dvddddddvd](https://discourse.gohugo.io/u/dvddddddvd)
#### Post date: [July 28, 2022, 7:45pm UTC](https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742/1 "2022-07-28T19:45:38Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dvddddddvd](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dvddddddvd/32/17415_2.png) [@dvddddddvd](https://discourse.gohugo.io/u/dvddddddvd)
#### Post date: [April 1, 2023, 9:10pm UTC](https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742/2 "2023-04-01T21:10:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![frjo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/frjo/32/18626_2.png) [@frjo](https://discourse.gohugo.io/u/frjo)
#### Post date: [April 2, 2023, 11:29am UTC](https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742/3 "2023-04-02T11:29:21Z")

</div>

I recently got a contribution to my [Zen theme](https://github.com/frjo/hugo-theme-zen) 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](https://katex.org).

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

---

<div class="post-metadata">

### Author: ![dvddddddvd](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dvddddddvd/32/17415_2.png) [@dvddddddvd](https://discourse.gohugo.io/u/dvddddddvd)
#### Post date: [September 30, 2023, 3:43am UTC](https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742/4 "2023-09-30T03:43:47Z")

</div>

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

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

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

```

---

<div class="post-metadata">

### Author: ![razon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/razon/32/14882_2.png) [@razon](https://discourse.gohugo.io/u/razon)
#### Post date: [September 30, 2023, 6:35am UTC](https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742/5 "2023-09-30T06:35:59Z")

</div>

Not sure what causes the problem without seeing your codes, the result of the [KaTeX module](https://hugomods.com/en/docs/content/katex/) seems work as expected.

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

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

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

````

![image](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/e/5/e542938f8b3237556f8fd5cbab7b91197b859be1.png)

---

<div class="post-metadata">

### Author: ![dvddddddvd](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dvddddddvd/32/17415_2.png) [@dvddddddvd](https://discourse.gohugo.io/u/dvddddddvd)
#### Post date: [September 30, 2023, 7:22am UTC](https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742/6 "2023-09-30T07:22:54Z")

</div>

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:

```auto
{{ $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`.

---

<div class="post-metadata">

### Author: ![razon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/razon/32/14882_2.png) [@razon](https://discourse.gohugo.io/u/razon)
#### Post date: [September 30, 2023, 7:37am UTC](https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742/7 "2023-09-30T07:37:57Z")

</div>

> [@dvddddddvd](#):
>
> I’m having some progress by manually doing some transforms on the `.RawContent`

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.

---

<div class="post-metadata">

### Author: ![dvddddddvd](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dvddddddvd/32/17415_2.png) [@dvddddddvd](https://discourse.gohugo.io/u/dvddddddvd)
#### Post date: [November 26, 2023, 12:27pm UTC](https://discourse.gohugo.io/t/braces-in-latex-get-escaped/39742/8 "2023-11-26T12:27:56Z")

</div>

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