在网页中渲染LaTex公式

概述

MathJax可以实现网页浏览器中的LaTex公式渲染。

引入

可以使用特定的模板形式引入和配置,具体可参考:配置mathjax — MathJax 3.2 文档 (osgeo.cn)。其中代码可以以CDN形式引入:mathjax (v3.2.2) -BootCDN

<script>
MathJax = {
  tex: {
    inlineMath: [['$', '$'], ['\\(', '\\)']]
  }
};
</script>
<script id="MathJax-script" async
  src="https://cdn.bootcdn.net/ajax/libs/mathjax/3.2.2/es5/tex-chtml.js">
</script>

使用测试

以下是比较完整的HTML页面:

<!DOCTYPE html>
<html lang="en">
<head>
...
</head>
<body>
<p>$ a_2 = \sin \theta $</p>

<p>这是一个行内公式\(a_2 = \sin \theta\)。</p>
</body>
</html>

通过在body标签中使用$...$\(...\)形式可以书写行内LaTex公式,浏览器打开后,渲染为:

<p>$ a_2 = \sin \theta $</p>

<p>这是一个行内公式\(a_2 = \sin \theta\)。</p>

\begin{cases}
a_1 = \sin \theta \\ 
a_2 = \cos \theta
\end{cases}

详细配置

参考自:MathJax的基本使用 - 启明星工作室 - 博客园 (cnblogs.com)

MathJax = {
  options: {
    skipHtmlTags: [            //  不会搜索数学的HTML标签
        'script', 'noscript', 'style', 'textarea', 'pre',
        'code', 'annotation', 'annotation-xml'
    ],
    includeHtmlTags: {         //  可以出现在数学中的HTML标签
        br: '\n', wbr: '', '#comment': ''
    },
    ignoreHtmlClass: 'tex2jax_ignore',    //  标记不搜索标签的类
    processHtmlClass: 'tex2jax_process',  //  标记应搜索的标签的类
    compileError: function (doc, math, err) {doc.compileError(math, err)},
    typesetError: function (doc, math, err) {doc.typesetError(math, err)},
    renderActions: {...}
  }
};

总结

MathJax的使用就是这么简单、朴实无华。官方文档写了一堆有的没的,实在是没搞懂能干什么。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

巽星石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值