Setting up LaTeX in Hugo

2017-10-26

I want to put equations in my posts. How do you do that in Hugo? It’s easy as $$\prod_{n=1}^{\infty} \left(\frac{2n}{2n-1} \cdot \frac{2n}{2n+1}\right)$$.

Well, a link around an equation in the teaser text screws up the style in the post list page. But that’s a minor issue, and hopefully fixed by switching themes.

I started here and found a hint that using mmark source might be the easiest route. So I followed the link to this post which, after some helpful explanation, linked to the actual changeset used to add KaTeX support.

I’m still figuring out what to do about my theme, so I just modified one file in my test theme (layouts/_default/baseof.html) to try this out. I added four lines - the js and css and includes in <head>, and the js function call just before the end of <body>. Then I created this post in mmark format with a few math blocks, and everything just worked. Update 2017/11/02: updated from Katex v0.6.0 to v0.9.0-alpha1, for \operatorname support.

Inline math works: $$F_M(N, N_R) = {N-1 \choose N_R-1}{M-N+1 \choose N_R}$$, and so does display math:

$$F_M(N, N_R) = {N-1 \choose N_R-1}{M-N+1 \choose N_R}$$

Now I have the bare minimum necessary to start converting old mediawiki posts. I picked a smallish one with a decent amount of math in it: The bell curve integral.

Great! But I’d love for this to work in org-mode…

Update 2020/01/24

Notepadium has built-in support that works great for .org files, for latex block mode, not so much for inline mode.

The only confusion is that the notepadium example page says to “include the partial in your templates like so”, which assumes some familiarity with Hugo. I haven’t used templates at all, so it wasn’t clear where to put this. After some experimentation, seems like it works to call the math.html partial from layouts/partials/header.html (supplements the existing theme’s header).

Some links:

latexhugo

Plots in Hugo

Using Hugo and org-mode for a blog