尝试将简单的内联方程转换为 SVG 不起作用,并在第一次出现 $ 时停止执行。
内联公式:
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}.$$
将上述内联TeX转换为SVG的代码:
var mjAPI = require("MathJax-node/lib/mj-single.js");
var fs = require('fs');
mjAPI.config({
MathJax : {
SVG : {
scale: 120,
font : "STIX-Web",
linebreaks: { automatic: true },
tex2jax: { inlineMath: [['$','$'], ['\(','\)']] }
}
},
displayErrors : true,
displayMessages : false
});
mjAPI.start();
fs.readFile(process.argv[2], 'utf8', function (err, formula) {
if (err) {
return console.log(err);
}
mjAPI.typeset({
math : formula,
format : "inline-TeX",
svg : true,
width: 1,
linebreaks: true
}, function (results) {
if (!results.errors) {
console.log(results.svg)
}
});
});
输出:
只是在 svg 中打印。
编辑。。。
在Peter Krautzberger的帮助下(见下面的评论(,我能够让SVG导出工作。这是代码。
var mjAPI = require("mathjax-node/lib/mj-page.js");
var jsdom = require("jsdom").jsdom;
var document = jsdom("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}.$");
mjAPI.start();
mjAPI.typeset({
html: document.body.innerHTML,
renderer: "SVG",
inputs: ["TeX"]
}, function(result) {
"use strict";
document.body.innerHTML = result.html;
var HTML = document.documentElement.outerHTML.replace(/^(n|s)*/, "");
console.log(result.html);
});
mj-single
只能处理单个方程。对于处理具有多个方程式的文档,您需要使用 mj-page
(它返回一个 HTML 文档而不是单个 svg(。
修改自述文件中的示例,这可能会帮助你入门。
var mjAPI = require("mathjax-node/lib/mj-page.js");
var jsdom = require("jsdom").jsdom;
var fs = require('fs');
var html = fs.readFileSync(process.argv[2])
var document = jsdom(html);
mjAPI.start();
mjAPI.typeset({
html: document.body.innerHTML,
renderer: "SVG",
inputs: ["TeX"]
}, function(result) {
console.log(result.html);
});