Latex公式表達式在服務端進行轉換成可用數據
使用語言與擴展
准備工作
- 安裝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"
也可以修改成其他通訊協議方式
記錄一下