Hugo 默认是不支持显示 LaTeX 风格的数学表达式的,Markdown 中的数学表达式语法在 Hugo 中默认并不会被识别。
在 Hugo 中引入 MathJax 即可解决该问题,MathJax 是一个适用于所有浏览器的 JavaScript 数学显示引擎。
引入方法很简单,在文章的模版的 HTML 的中添加以下内容即可。
<script type="text/javascript" async src="https://cdn.bootcss.com/mathjax/2.7.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML">MathJax.Hub.Config({ tex2jax: { inlineMath: [['$','$'], ['\\(','\\)']], displayMath: [['$$','$$'], ['\[\[','\]\]']], processEscapes: true, processEnvironments: true, skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'], TeX: { equationNumbers: { autoNumber: "AMS" }, extensions: ["AMSmath.js", "AMSsymbols.js"] } }});
MathJax.Hub.Queue(function() { // Fix <code> tags after MathJax finishes running. This is a // hack to overcome a shortcoming of Markdown. Discussion at // https://github.com/mojombo/jekyll/issues/199 var all = MathJax.Hub.getAllJax(), i; for(i = 0; i < all.length; i += 1) { all[i].SourceElement().parentNode.className += ' has-jax'; }});</script>
<style>code.has-jax { font: inherit; font-size: 100%; background: inherit; border: inherit; color: #515151;}</style>
要注意的是需要确保该 HTML 必定被 Hugo 框架加载,我这里是添加到了 layouts/partials/
目录下的 footer.html
文件中,因为我确定该文件必定包含在网站的每个页面中。
实际上单纯引入 MathJax 并不需要如此多行代码,多余的部分是为了解决 Markdown 和 LaTeX 中对于划线 _ 的不同定义带来的问题,详情可以参考 这篇文章 。
好了,开心的在 Hugo 中使用 LaTeX 吧。
编辑于 Jul 14