Math typesetting, or rendering LaTeX, in Hugo is not a hard task. We have two popular alternatives. First one is mathjax, I was using it before with jekyll. The other popular library for math typesetting is KaTeX. Since it’s is faster, this time I’ll go with KaTeX.
Implementing these libraries is pretty straightforward. We just need to include them in the <head>
section. Yet, we don’t want them to load for every page, only if it’s required.
$$f(a) = \frac{1}{2\pi i} \oint\frac{f(z)}{za}dz$$
 Create a partial template and use the code below.
/layouts/partials/helpers/katex.html
.
The first line is the CSS, the second one is the javascript file. The third line is the auto render extension. Autorender extension is required to render math elements inside text.


Don’t just copypaste this one. It’s using version 0.12 which is the latest version when I’m typing this blog post. You may want to see if there is an update.
 Include the partial in the head section. This is
/layouts/partials/head.html
for me.


 Use the parameter math in the frontmatter if you want to use the library in that post/page.


 If you want inline equations, you should add the following too.


This will provide inline equations with single dollar signs. $x = {b \pm \sqrt{b^24ac} \over 2a}$
, $x = {b \pm \sqrt{b^24ac} \over 2a}$. By the way, I’m using the default markdown processor of Hugo called goldmark.
Have fun!