# MathJax and KaTeX support

The official docs about math typesetting (MathJax alone sadly) support are a bit appalling for me. Even though the text is quite descriptive regarding the fundamental problem about handling verbatim text inside markdown, it’s only referring to a quite outdated and cumbersome fix, besides it being MathJax specific.

New versions of Jekyll have been adopting the kramdown markdown processor by default, which supports LaTeX handling out of the box. No need to apply any hacks. So, this got me thinking, what does gohugo uses for markdown processing, can it resemble kramdown behavior regarding LaTeX content (no markdown processing inside MathJax and KaTeX tags)?

Second question: is there anything planned to better support this?

Regards

I’ve taken a quick look at the hugo code and have seen imports for two markdown processing libs:

I didn’t go further about how they’re being used but, Mmark seems the promissing one regarding this aspect. From their docs:

• Math support, use $$ as the delimiter. If the math is part of a paragraph it will be displayed inline, if the entire paragraph consists out of math it considered display math. No attempt is made to parse what is between the $$.

Which looks like resembling the Kramdown feature.

Created issue https://github.com/spf13/hugo/issues/1666.

I am not super sure if this is helpful, but here is what I use:

1. I have a (boolean) variable in my content file’s front-matter called includes_math.

2. When this is set to yes, the corresponding code in my footer partial includes whatever is necessary for MathJax. e.g.

    {{ if .Params.includes_math }}
<script type="text/x-mathjax-config">MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\$','\$']]}});</script>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
{{ end }}

1. Finally, as part of the the content, I use <p> tags (yuck) to prevent the markdown renderer from processing the math code. e.g.
<p>
\begin{align}
\mathcal{M}(u_{h}) - \mathcal{M}(u)
& = \mathcal{M}(u_{h} - u)\\
& = a^{*}(z, u_{h} - u)\\
& = a(u_{h} - u, z)\\
& = a(u_{h}, z) - a(u, z)\\
& = a(u_{h}, z) - L(z)\\
& = r(z),
\end{align}
</p>

2 Likes

Thanks for the tip but this is for display math, since <p></p> creates a paragraph, not inline math. All these limitations are covered in the current official docs being referred, and the fixes to circumvent them.

I understand. I was just pointing out the workaround that works for me as of today.

There ought to be a way of making the equivalent of a shortcode where the internal markup is not processed.

The docs do point a fix.