服务端Latex解析成图片或者HTML或者SVG方案

Latex公式表达式在服务端进行转换成可用数据

使用语言与扩展

准备工作

贴代码

var mjAPI = require('mathjax-node-svg2png');

mjAPI.config({
        displayMessages: false,    // determines whether Message.Set() calls are logged
        displayErrors:   true,     // determines whether error messages are shown on the console
        undefinedCharError: false, // determines whether "unknown characters" (i.e., no glyph in the configured fonts) are saved in the error array
        extensions: '',            // a convenience option to add MathJax extensions
        fontURL: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/fonts/HTML-CSS', // for webfont urls in the CSS for HTML output
        paths: {},                  // configures custom path variables (e.g., for third party extensions, cf. test/config-third-party-extensions.js)
        MathJax: {
            showProcessingMessages: false,
            messageStyle: "none",
            extensions: ["tex2jax.js"],
            jax: ["input/TeX", "output/HTML-CSS"],
            tex2jax: {
              //inlineMath: [ ['$','$'], ["\\(","\\)"] ],
              displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
              processEscapes: true
            },
            "HTML-CSS": { fonts: ["TeX"], showMathMenu: false }
        }               // standard MathJax configuration options, see https://docs.mathjax.org for more detail.
});

mjAPI.start();

var math = '   \\triangle ABC  ,  AC\\bot BC   ,  AC=8\\rm cm   ,  BC=4\\rm cm  ,  AP\\bot AC  ,  A  ,  D  ,  E ';

mjAPI.typeset({
    math: math,
    format: 'inline-TeX',
    svg: false, //关闭SVG转换
    png: true, //开启图片转换  如果转入其他参考GitHUb进行设置
    scale: 1,
    //html: true,
},function(data){
    console.log('data:')
    console.log(data);
}
)

当使用其他语言开发时,可以通过命令行方案进行调用改动方案

var math = '   \\triangle ABC  ,  AC\\bot BC   ,  AC=8\\rm cm   ,  BC=4\\rm cm  ,  AP\\bot AC  ,  A  ,  D  ,  E ';

//修改为

var math = process.argv[2];

//CLI命令为   node  xxx.js  "公式文字"  示例: node test.js " \triangle ABC  ,  AC\bot BC   ,  AC=8\rm cm   ,  BC=4\rm cm  ,  AP\bot AC  ,  A  ,  D  ,  E"

也可以修改成其他通讯协议方式  

 

记录一下

 

posted @ 2019-06-05 17:08  倔强新手猿  阅读(1932)  评论(0编辑  收藏  举报