首页 > 解决方案 > 使用 mathjax-node 无法获得与浏览器上的 MathJax 相同的结果

问题描述

我想用 mathjax-node 渲染一些公式,但用户也应该能够用光标单独选择在它们下划线的每个元素。例如,如果出现求和,他可以选择极值、内部公式的元素、迭代变量等。当元素呈现为 <span> 对象时是可能的,例如,可以直接从 URL 使用 MathJax 2.7.7 来完成。

在浏览器上使用 MathJax 时,我有一个具有以下配置的页面:

<!DOCTYPE html>
<html>
    <head>
        ...
        <script type="text/x-mathjax-config">
            MathJax.Hub.Config({
                extensions: ["tex2jax.js","[Contrib]/forminput/forminput.js"],
                jax: ["input/TeX","output/HTML-CSS"],
                tex2jax: {
                    inlineMath: [ ['$','$'], ["\\(","\\)"] ],
                    displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
                    processEscapes: true,
                    processEnvironments: true,
                },
                TeX: {extensions: ["AMSmath.js","AMSsymbols.js"]},
                displayAlign: 'center',

                "HTML-CSS": {
                    styles: {'.MathJax_Display': {"margin": 0}},
                    linebreaks: { automatic: true }
                }
            });
        </script>
        <script 
            type="text/javascript"
            src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-MML-AM_CHTML"
        />

在这种情况下,如果我有这个公式 $\sum_{i=0}^ni>2$,结果是:

在此处输入图像描述

但现在我正在使用从https://github.com/mathjax/MathJax-node/tarball/master下载的 mathjax-node 。根据文档https://www.npmjs.com/package/mathjax-node我必须输入:

var mjAPI = require("mathjax-node");
mjAPI.config({
    MathJax: {
        // traditional MathJax configuration
    }
});
mjAPI.start();

(在配置部分,它不再识别行:“extensions: ["tex2jax.js","[Contrib]/forminput/forminput.js"]")。

使用 typeset 函数获得带有结果的对象:

var yourMath = '\sum_{i=0}^ni>2';

let result = mjAPI.typeset({
    math: yourMath,
    format: "TeX", // or "inline-TeX", "MathML"
    mml:true,      // or svg:true, or html:true
},

然后用“result.mml”、“result.svg”或“result.html”调用应该渲染的部分。

我只设法使用“html:true”选项渲染为 <span> 对象。但它看起来与我在网络上使用 MathJax 时不同。例如,如果输入“格式:'inline-TeX'”,我会得到:

在此处输入图像描述

这里问了类似的问题:mathjax-node: different output when formatted on pages than in node project,解决方案是在排版中添加“css:true”选项,但我已经尝试过了,它看起来仍然一样。我想我仍然应该将其称为“result.html”。

标签: javascripthtmlnode.jsmathjax

解决方案


我正在回答我自己的问题。如果“result”是返回的对象,我不明白应该如何使用“result.css”属性。唯一要做的就是将它包含在显示网页样式的配置中。例如,网页可以是:

<html>
    <head>
        <style>
            result.css
        </style>
    </head>
    <body>
        result.html
    </body>
</html>

推荐阅读