The KaTeX library allow you to enable TeX support to your hugo website. The library supports all major browsers. TeX syntax allows us to write mathematical formulas and scientific symbols easily.
$$ \int_a^b f(x) dx $$
Installing
To install, we just need to import the library files from some CDN to our Hugo website. One way to do this is using the Hugo partials which allow us to import small HTML context.
To start, lets create a new partial template file to hold the code needed to add the library.
Create a new partial file in hugo_root/layouts/tex.html.
For the KaTeX library we just need to include one .js file and one .css file.
However, we also need the auto-render Katex extension which is responsible for scanning our markdown content looking for delimitiers to tell what content is a TeX content that Katex should render. The auto-render extension can be included with another single .js file.
One last thing is configuring the auto-render extension using javascript inside <script> tags.
You can configure what are the delimiters that the extension should look for.
The latex.html file
The full content of latex.html is the following:
| |
Importing the partial to the theme
Now we just need to import the partial in your PaperMod theme.
Find the hugo-root/themes/PaperMod/layouts/partials/head.html template file, then import the created partial.
| |
Testing
You should now be able to use TeX! Trying adding the following matrix structure to your markdown:
| |
And you should see the following renderized TeX:
$$ \begin{bmatrix} x & y \\ z & w \end{bmatrix} $$
Inline TeX
You can use inline TeX like this \(
\begin{bmatrix}
a & b \newline
c & d
\end{bmatrix}
\). The only difference is the delimiters, instead of using $$, just use \\( and \\).
New Line
The Katex documentation says that hard line breaks are \\ and \newline.
However, note that we had to use \\\ instead of \\ to split lines while avoiding conflict with Hugo parser.
Documentation
You can check more examples of supported TeXt functions at https://katex.org/docs/supported.