MathJax - Beautiful math in all browsers
A JavaScript display engine for mathematics that works in all browsers. No more setup for readers. It just works.
The code of MathJax in Hugo theme
Create a file named mathjax_support.html
in the /layout/partials/
folder. Insert the below code into it, for example.
1 | <script type="text/javascript" async |
Then, include mathjax_support.html
in footer.html
or header.html
. In my case. I chose to put it in header.html
as follows:
1 | {{ if .Params.mathjax}}{{ partial "mathjax_support.html" . }}{{ end }} |
This code means mathjax_support.html
will be include as long as you add mathjax = ture
to your front matter.
Using MathJax in your post
First, add mathjax = ture
to your front matter. It could be like this.
1 | +++ |
Second, write LaTeX code in your post, put inline math into '$','$'
, and display math between '$$','$$'
.
Inline math
Here is inline math: $\sqrt{3x-1}+(1+x)^2$
The result is: $\sqrt{3x-1}+(1+x)^2$
Display math
Here is display math:
1 | $$ |
The result is:
$$
\begin{align}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \newline
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \newline
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \newline
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{align}
$$
Subscript issue with markdown
After enabling MathJax, any math entered between proper markers (see the MathJax documentation) will be processed and typeset in the web page. One issue that comes up, however, with Markdown is that the underscore character (_
) is interpreted by Markdown as a way to wrap text in emph blocks while LaTeX (MathJax) interprets the underscore as a way to create a subscript. This “double speak” of the underscore can result in some unexpected and unwanted behavior.
There are many solutions to the problem, the Hugo official document hints a way to make MathJax work. It revolves a series of complex steps to correctly display LaTeX code. And you have to put inline mathematics between '$ TeX Code $'
, and put display style mathematics between <div>$$TeX Code$$</div>
. This method treats ‘inline math’ with ‘code’, which may get some trouble in your website style.
I think it’s too complex and tedious, my suggestion is to simply use \_
instead of _
in your LaTeX code.
Here is a example: $\sum\_{k=1}^n a\_k$
The result is $\sum_{k=1}^n a_k$.