
正文
服务端Latex解析成图片或者HTML或者SVG方案
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Latex公式表达式在服务端进行转换成可用数据
使用语言与扩展
- node.js
- Mathjax (文档链接)
- MathJax在nodejs上解决方案 mathjax/MathJax-node(GitHub)
- 转换成图片或者SVG扩展 pkra/mathjax-node-svg2png
准备工作
- 安装nodejs
- 安装Mathjax-node扩展 (GitHub)
- 安装图片SVG扩展(pkra/mathjax-node-svg2png)
贴代码
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"
也可以修改成其他通讯协议方式
记录一下






