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

标签 node.js svg mathjax

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

相关文章:

javascript - MathJax 的公式呈现不正确

node.js - AWS Elastic Cache 是否支持 Redis 集群上的 Pub/Sub?

javascript - svg:一个角色的坐标?

javascript - mathjax 与 jquery-popbox 冲突

jquery - 我的 SVG 动画被截断了

java - SVG 到 Java Graphics2D

javascript - 如何将 MathJax 和 Math.js 连接到 slider

jquery - 自动将来自服务器的消息附加到 div 标签

node.js - 错误命令太多 : outbound emails with Haraka

node.js - Mocha 测试执行时间太长