This blog should be a complete guide to render mathematics in Hugo. However, if you have problems reproducing this blog, note that the official documentation is always the best place to start:
First, add following code to hugo.yaml:
| |
Second, create a new file layouts/partials/extend_head.html:
| |
Note that name and path of the created file is based on your theme configuration.
For example, in theme PaperMode (which I use), “extend_head.html” indicates that, to extend the head, I can create a file named
extend_head.htmlin./layouts/partials/(so-called global layouts, without modifying layouts inside the theme).In other words, if your theme does not support this feature, you may need to copy the
head.htmlfrom the theme to global layouts and modify it, or simply modify the theme directly (but rememenber that modifications in git submodules will not be committed to the remote repository).
Next, create a new file layouts/partials/math.html:
| |
Now, you can render both block and inline mathematics in your content files. For example, the following code renders the equation $O=Attention(Q,K,V)=softmax(\frac{QK^T}{\sqrt{d_k}})V$:
| |
Test it locally by running
hugo server -D .to see if the equation rendered correctly.
Finally, there is one more thing to note before deploying your website.
You should always use the newest version of hugo to support all features (in goldmark).
Why? In previous versions, the
passthroughextension is not supported, and consequently,\\inside the equation will be rendered as\instead of a new line.
Based on the official doc, if you are using Cloudflare to deploy your website:
- Go to Workers & Pages -> Your Porject -> Settings -> Variables and Secrets;
- Add a new variable
HUGO_VERSIONwith value0.135.0at least.
This will specify the version of Hugo used by Cloudflare to build your website.