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

Xia*_*Mao 4 mathjax

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

但似乎 MathJax.js 在浏览器中生成的 svg 不是有效的 svg。我用一个简单的数学公式进行了测试:$$a^2 + b^2 = c^2$$演示代码)和SVG复制到的jsfiddle,它什么也不显示。

然后我试着写了一个MathJax-node demo,再把svg复制到jsfiddle,看起来不错。这是我的演示代码,它与GitHub 存储库演示几乎相同:

// 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)
  })
});
Run Code Online (Sandbox Code Playgroud)

我还用cloudconvert测试了两个 svg,结果相同。为什么两个 svg 不同?我错过了什么吗?

Pet*_*ger 5

差异是由于特定设置: useGlobalCache

默认情况下, MathJax ( docs ) 将其设置为truemathjax-node ( docs ) 将其设置为false

在服务器上,MathJax 节点没有任何文档上下文并生成自包含的 SVG。在客户端,MathJax 有一个完整的文档上下文,因此可以跨方程重用 SVG 路径。