gpt4 book ai didi

node.js - MathJax-node 为内联 TeX 生成 SVG 输出

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:32 24 4
gpt4 key购买 nike

尝试将简单的内联方程转​​换为 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 中打印 When

已编辑...

在 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)。

修改 the readme 中的示例,这可能会帮助您入门。

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);
});

关于node.js - MathJax-node 为内联 TeX 生成 SVG 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42240973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com