为什么 MathJax.js 生成的 svg 与 MathJax-node 生成的 svg 不同

Why is svg generated by MathJax.js different than the svg generated by MathJax-node

我正在编写一个将 html 保存到 onenote 的网络应用程序。为了保存数学公式,我打算通过MathJax.js把数学公式转成svg,然后再把svg转成png,因为onenoteapi支持的html/css有限

但是 MathJax.js 在浏览器中生成的 svg 似乎不是有效的 svg。我用一个简单的数学公式测试了它:$$a^2 + b^2 = c^2$$ (demo code) and copy the svg to jsfiddle 但它什么都不显示。

然后我尝试编写一个 MathJax-node 演示并将 svg 复制到 jsfiddle again, it looks good. Here is my demo code, it's almost the same as the GitHub repo demo:

// a simple TeX-input example
const fs = require('fs')
var mjAPI = require("mathjax-node");
mjAPI.config({
  MathJax: {
    // traditional MathJax configuration
  }
});
mjAPI.start();

var yourMath = String.raw`a^2 + b^2 = c^2`

mjAPI.typeset({
  math: yourMath,
  format: "TeX", // or "inline-TeX", "MathML"
  svg: true,      // or svg:true, or html:true
}, function (data) {
  if (!data.errors) {console.log(data.svg)}
  // will produce:
  // <math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  //   <mi>E</mi>
  //   <mo>=</mo>
  //   <mi>m</mi>
  //   <msup>
  //     <mi>c</mi>
  //     <mn>2</mn>
  //   </msup>
  // </math>

  fs.writeFile('math.txt', data.svg, (error) => {
      console.log(error)
  })
});

我也用cloudconvert测试了两个svg,结果是一样的。为什么两个svg不同?我错过了什么吗?

差异是由于特定的设置:useGlobalCache

默认情况下,MathJax (docs) sets this to true while mathjax-node (docs) 将其设置为 false

在服务器上 MathJax-node 没有任何文档上下文并生成独立的 SVG。在客户端,MathJax 具有完整的文档上下文,因此可以跨方程重用 SVG 路径。