Skip to content

Instantly share code, notes, and snippets.

@pkra
Last active May 30, 2016 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkra/c60098af5c1d8c37473416caad0418f6 to your computer and use it in GitHub Desktop.
Save pkra/c60098af5c1d8c37473416caad0418f6 to your computer and use it in GitHub Desktop.
An example module for wrapping PNG generation around mathjax-node
var mjAPI = require("../lib/main.js");
var svg2png = require('svg2png');
function createPNG(result, callback){
var sourceBuffer = new Buffer(result.svg, "utf-8");
svg2png(sourceBuffer).then(function(buffer){
result.png = "data:image/png;base64," + buffer.toString('base64');
return callback(result);
})
};
exports.math2png = function(options, callback){
var mjConfig = Object.assign({}, options.config);
console.log("**Note**: options.config can only be assigned once");
var typesetOptions = Object.assign({}, options.typeset);
// make sure SVG output will be generated and disable mml and html
typesetOptions.svg = true;
typesetOptions.mml = false;
typesetOptions.html = false;
mjAPI.config(mjConfig);
mjAPI.typeset(typesetOptions, function(result){
if (result.errors) callback(result);
createPNG(result, callback);
});
}
// Usage:
//
// var math2png = require("math2png.js").math2png;
// var options = {
// config: {
// },
// typeset: {
// math: "x^2",
// format: "TeX"
// }
// }
// math2png(options, function(result){
// console.log(result.png);
// console.log("style='" + result.style + "width:" + result.width + "; height:" + result.height +";'");
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment