**โ ๏ธ Warning:** This post is over a year old, the information may be out of date.

# ๐ Hugo with Math (KaTeX + mmark)

๐ | โฐ 3 minutes

## Intro

I not good with math but sometimes I need to use math notation to explain something.
So I try to find if `Hugo`

have built-in math syntax, but seem it not there. On Jekyll, I can use MathJax but it terribly slow and broke it it take times too long to render the formulas, then I found the alternative for MathJax and it called as **KaTeX**.

As usual, stuff like this are depend on CSS and JavaScript programming, so here we are. There is no need to re-invent too much because KaTeX already prepare the CDN for us to use.

## Partial layout:

Like I told you, KaTeX have CDN ready to use but I still want to host it on Gitlab pages and just use the CDN on local built.

You will get `CORS policy: No 'Access-Control-Allow-Origin' header`

issue appears on your dev-tools console if you don’t use CDN when doing local built on your computer, so I suggest to use CDN for local build and use stored asset when upload / build it on server

First add this `/themes/XYZ/layouts/partials/katex.html`

file, please alter it if you just one to use CDN on both local and remote hosted Hugo.

Let’s inject our partial code to headers file, just put it between `<head>...</head>`

### Matterfront setting

To supports the rendering of mathematical formulas by using KaTeX, we need to define two extra parameter on each post or page that use KaTeX, we need to put `markup: "mmark"`

and `math: true`

parameters

```
---
title: "Hugo with Math (KaTeX + mmark)"
date: 2021-07-14T15:24:14+08:00
markup: "mmark"
math: true
---
```

### Writing $\LaTeX$

To displayed equation like this:

You need to using `<div>`

tags as example below:

```
<div>
\[ \int u \frac{dv}{dx}\, dx=uv-\int \frac{du}{dx}v\,dx \]
</div>
```

Here another example, the ~~famous~~ Matrix ~~reloaded (just kidding)~~ :

As mentioned before, you need to use `<div>`

```
<div>
\[ \begin{pmatrix} a&b\\c&d \end{pmatrix} \quad
\begin{bmatrix} a&b\\c&d \end{bmatrix} \quad
\begin{Bmatrix} a&b\\c&d \end{Bmatrix} \quad
\begin{vmatrix} a&b\\c&d \end{vmatrix} \]
</div>
```

Same goes for aligned equation:

```
<div>
\[\begin{aligned}
x ={}& a+b+c+{} \\
&d+e+f+g
\end{aligned}\]
</div>
```

Another thing is you can type inline equation like example below:

`$E=mc^2$`

which rendered as $E=mc^2$`$\int_{a}^{b} x^2 dx$`

which rendered as $\int_{a}^{b} x^2 dx$`$\int u \frac{dv}{dx}\, dx=uv-\int \frac{du}{dx}v\,dx$`

as $\int u \frac{dv}{dx}, dx=uv-\int \frac{du}{dx}v,dx$`$\mathbf{y} = \mathbf{X}\boldsymbol\beta + \boldsymbol\varepsilon$`

as $\mathbf{y} = \mathbf{X}\boldsymbol\beta + \boldsymbol\varepsilon$

It also capable to generated multi line math formula by using standard $\LaTeX$ line break consisting of 2 backslashes and you must put double dolar sign to make it work correctly.

example:

### Warning

$$\left [ โ \frac{\hbar^2}{2 m} \frac{\partial^2}{\partial x^2} + V \right ] \Psi = i \hbar \frac{\partial}{\partial t} \Psi$$

P/s : Maybe we should use MathJax instead of KaTeX?

Posted by: Robbi Nespu