Hugo: Add MathJax build support -- post-hugo processing

Created on 1 Jan 2020  路  9Comments  路  Source: gohugoio/hugo

https://github.com/litao91/goldmark-mathjax is available as a supported extension, linked to on their README..

could it be possible to add it as an integration with the Goldmark in Hugo so that you don't need to create a MathJax include html file?

Proposal Stale

Most helpful comment

Please consider this feature. It would be great if one could build server-side math formulas.

All 9 comments

I came across this issue because I'm interested in getting hugo to generate server side math.

The goldmark-mathjax only wraps the math in html tags. With mathjax@3 these tags aren't necessary. You would still have to load MathJax javascript and render in the browser.

From the markdown:

$$
\mathbb{E}(X) = \int x d F(x) =
\left\{ \begin{aligned} \sum_x x f(x) \; & \text{ if } X \text{ is discrete}
\\ \int x f(x) dx \; & \text{ if } X \text{ is continuous }
\end{aligned} \right.
$$

Inline math $\frac{1}{2}$

Default settings would currently output:

<p>$$
\mathbb{E}(X) = \int x d F(x) = \left{ \begin{aligned} \sum_x x f(x) ; &amp; \text{ if } X \text{ is discrete}
\ \int x f(x) dx ; &amp; \text{ if } X \text{ is continuous }
\end{aligned} \right.
$$</p>
<p>Inline math $ \frac{1}{2} $</p>

With the goldmark-mathjax plugin enabled (inline: $, block: $$), you'd see:

<p><span class="math display">$$\mathbb{E}(X) = \int x d F(x) =
\left\{ \begin{aligned} \sum_x x f(x) \; &amp; \text{ if } X \text{ is discrete}
\\ \int x f(x) dx \; &amp; \text{ if } X \text{ is continuous }
\end{aligned} \right.
$$</span></p>
<p>Inline math <span class="math inline">$\frac{1}{2}$</span></p>

Hmmm ok, thanks for the info!! You reminded me that somehow I removed MathJax from my website.

Now I've re-added it via https://github.com/jasikpark/jasik-xyz/commit/334c1805d57d420cbd2b3f4f66ae63b2a349c762

mathjax.html (from the docs)

<script>
    MathJax = {
        tex: {
            inlineMath: [['$', '$'], ['\\(', '\\)']]
        },
        svg: {
            fontCache: 'global'
        }
    };
</script>
<script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script> 

header.html

{{ if .Page.Params.isMath }}
    {{ partial "mathjax.html" . }}
{{ end }}

And adding isMath = "true" to the frontmatter of posts that require MathJax

Actually, could MathJax be added as a go module that processes the html post-build? So that the CSS and images are generated on the build server rather than the client?

Please consider this feature. It would be great if one could build server-side math formulas.

Maybe Hugo Pipes https://gohugo.io/hugo-pipes/ is the right solution?

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

/reopen

@bep I wonder if you'd consider re-opening this - it was closed as stale, despite having significant interest, and no correct workaround. The posted workaround does not provide an answer, as discussed by @hadley here:

https://discourse.gohugo.io/t/use-goldmark-mathjax-extension/25721

For scientific discourse, wrapping every use of math in shortcodes (both inline and block math) isn't really a satisfactory option - for many papers that would more than double the size of the markdown, and making writing it extremely painful.

Many thanks for you consideration, and for creating Hugo, which we're currently considering switch to for https://nbdev.fast.ai/ - however since many of our users (including ourselves!) write a lot of math, this is a blocker for us. (We can try to figure out how to build it with mathjax support ourselves, of course, although golang isn't something we've worked with much before, and we'd also need to setup something to ensure that releases are synced with official releases here.)

See also open PR for this feature: #7435

Fixing this would also resolve this doc issue: https://github.com/gohugoio/hugo/issues/7885

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nikolas picture nikolas  路  3Comments

artelse picture artelse  路  3Comments

ianbrandt picture ianbrandt  路  3Comments

sigma picture sigma  路  3Comments

chrissparksnj picture chrissparksnj  路  3Comments