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_modulesinto theassetsandstaticfolders was a common approach. However, starting with v0.162.0, Hugo rejects symlinked entries inresources.Getfor security reasons (CVE-2026-50135), so module mounts are now the correct approach.
First, install katex via npm:
npm install --save katexThen, add module mounts to your hugo.yaml to mount the katex dist directory into assets/vendor/katex and the fonts into static/css/fonts:
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/fontsImportant: When you define
module.mounts, all default mounts are replaced — so you must explicitly re-mountassetsandstaticas shown above.
Now create themes/your-theme/layouts/partials/katex.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:
<!-- Include KaTeX if enabled in post-->
{{ if .Params.katex }}
{{ partial "katex.html" . }}
{{ end }}Then, in your post .md file, set
katex: truein the frontmatter to enable KaTeX rendering.
Now you can add a formula such as
$$\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}$$