How to include KaTeX 0.16 in Hugo (LaTeX rendering)

This post shows how to include the latest version of KaTeX (0.16) in Hugo for rendering LaTeX math. We don’t include it from a CDN mostly for GDPR reasons, but also because it’s faster to load it from the local server.

We will include katex.min.js, auto-render.min.js, katex.min.css from the assets folder and fonts from the static folder (so Hugo adds the entire directory).

Note: In Hugo versions before v0.162.0, symlinking files from node_modules into the assets and static folders was a common approach. However, starting with v0.162.0, Hugo rejects symlinked entries in resources.Get for security reasons (CVE-2026-50135), so module mounts are now the correct approach.

First, install katex via npm:

npm_install_katex.sh
npm install --save katex

Then, add module mounts to your hugo.yaml to mount the katex dist directory into assets/vendor/katex and the fonts into static/css/fonts:

hugo.yaml
module:
  mounts:
    - source: assets
      target: assets
    - source: static
      target: static
    - source: node_modules/katex/dist
      target: assets/vendor/katex
    - source: node_modules/katex/dist/fonts
      target: static/css/fonts

Important: When you define module.mounts, all default mounts are replaced — so you must explicitly re-mount assets and static as shown above.

Now create themes/your-theme/layouts/partials/katex.html:

katex_hugo_partial.html
<!-- Include library -->
<link rel="stylesheet" href="{{ (resources.Get "vendor/katex/katex.min.css").RelPermalink }}" />
<script defer src="{{ (resources.Get "vendor/katex/katex.min.js").RelPermalink }}"></script>
<!-- Include auto-render extension & render all KaTeX formulae !-->
<script defer src="{{ (resources.Get "vendor/katex/contrib/auto-render.min.js").RelPermalink }}" onload="renderMathInElement(document.body);"></script>

In themes/your-theme/layouts/partials/footer.html, conditionally include the partial:

katex_footer_partial.html
<!-- Include KaTeX if enabled in post-->
{{ if .Params.katex }}
    {{ partial "katex.html" . }}
{{ end }}

Then, in your post .md file, set

katex_frontmatter.yaml
katex: true

in the frontmatter to enable KaTeX rendering.

Now you can add a formula such as

katex_example_formula.tex
$$\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}$$

to your post to see it rendered by KaTeX:

$$\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}$$
Check out similar posts by category: Hugo