【问题标题】:MathJax-node to generate SVG output for inline TeXMathJax-node 为内联 TeX 生成 SVG 输出
【发布时间】:2017-07-03 14:49:30
【问题描述】:

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

将上述 inline-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 的帮助下(见下面他的 cmets),我能够让 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);
});

【问题讨论】:

    标签: node.js svg mathjax


    【解决方案1】:

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

    【讨论】:

    • 请注意,mj-page 正在从 mathjax-node 中删除,以支持单独的模块,参见 github.com/mathjax/MathJax-node/issues/206
    • 我有点迷路了!还没有让 mj-page.js 也能正常工作。转换类似“当$a \ne 0$时,$(ax^2 + bx + c = 0)$有两种解决方案的最佳方法是什么,它们是$$x = {-b \pm \sqrt SVG 中的 {b^2-4ac} \over 2a}.$$"。
    • Mathjax-node 没有将整个文档转换为单个 svg 的功能。它为方程式创建 svg,mjpage 简化了较大文档中的批处理。
    • 但也许这就是你要找的东西。
    • 感谢您的澄清。我能够得到我想要的大部分。但是输出仍然包含像 span 元素等的 HTML,我正在寻找更简单的输出,它只包含文本和 svg。将发布我可以在上述问题中使用的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 2021-07-22
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    相关资源
    最近更新 更多