MathJax is useful for rendering \(\rm \LaTeX\) in the browser, but there is no reason to load the script on pages that don't use it. This post shows how to load the MathJax script only on posts tagged "mathjax" and how to tag posts written in org format. This is mostly a reference for myself, but has turned out to be useful for others as well.
How to tag posts written in org format
Posts written in org format can be tagged by adding #+tags[]:
followed by
space separated tags to the beginning of the post. E.g., to give a post the
three tags "regression", "mathjax", and "R", you would add
#+tags[]: regression mathjax R
to the beginning of the org file, under the title and date.
Conditional footer based on page tags
In Hugo, you can get the tags of a page from the .Pages.tags
variable. Using
the in
function you can check whether the list of tags contains a given
element. Thus, to load MathJax if the page contains the tag "mathjax", add the
following to the footer:
{{ if in .Params.tags "mathjax" }}{{ partial "footer_mathjax.html" }}{{ end }}
where footer_mathjax.html
contains
<script async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
I have tagged this post with "mathjax", so I can write \[ \sum_{n = 1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6} \] or as inline math \(\sum_{n = 1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}\).