marked支持数学公式MathJax3

返回
Author Avatar
钢翼
2021-01-18
编程
98

欧拉公式:$e^{i\pi}+1=0$

质能方程:$E=mc^2$

<script>
//引入js前先配置
window.MathJax = {
            tex: {
                inlineMath: [
                    ['$', '$'],
                    ['\\(', '\\)']
                ],
                displayMath: [
                    ['$$', '$$'],
                    ["\\[", "\\]"]
                ]
            },
            svg: {
                fontCache: 'global'
            }
        };
</script>
<!--引入js-->
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

<script>
  //先处理marked,再处理公式
  document.getElementById('yourContent').innerHTML = marked(content)
  //有时js未加载完会报异常。
  MathJax.typesetPromise([document.getElementById('yourContent')]).then(() => {});
</script>

编写公式

When $a \ne 0$, there are two solutions to \\(ax^2 + bx + c = 0\\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$

效果如下:
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$