Reason for always double-escape $ outside math contexts

Hi there, I saw this from Hugo’s doc about double-escape $ outside math context:

If you add the $...$ delimiter pair to your configuration and JavaScript, you must double-escape the $ when outside of math contexts, regardless of whether mathematical rendering is enabled on the page. For example:

A \\$5 bill _saved_ is a \\$5 bill _earned_.

I find it doesn’t really align with my experiment.

I’ve set up my blog with the recommended settings, with Goldmark passthrough and mathjax loading on demand, and I enabled single $ as delimiter for inline math. So the example string renders into this HTML block:

<p>A \$5 bill <em>saved</em> is a \$5 bill <em>earned</em>.</p>

And if I set math: true in params, after mathjax’s processing the backslash is now gone.

<p>A <span>$</span>5 bill <em>saved</em> is a <span>$</span>5 bill <em>earned</em>.</p>

But when I set math: false, without mathjax’s processing, there’s an extra backslash displayed before $ (it’s just the first HTML block). In this case, remove one of the backslash to make it single-escape in raw string, then it renders correctly.

So I think what’s happening here is that, single backslash would tell Goldmark to escape the $ and not passthrough, while double backslash means not passthrough and also escaping the backslash to expose that to mathjax. Which means, when math rendering is disabled, we should use single-escape instead of double-escape, otherwise there would be an extra backslash.

Then why the doc say we must double-escape regardless of math rendering being enabled? What am I missing here?

(I’m testing these on the Hugo v0.140.2 and the latest v0.147.0.)

(Also, I think sections like <code> doesn’t need $ escaping at all? Mathjax by default ignores code element, and it’s already sort of a passthrough anyways)

You are correct, and I have updated the documentation accordingly. Note that sometimes a single escape is sufficient, but double-escaping is required for things like this:

I will give you \\$2 if you can solve $y = x^2$.

There is no harm in always double-escaping.

Also, as noted in the documentation, the example above will fail if you use KaTeX instead of MathJax.

Thanks for letting us know.

Thanks for the update!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.