forked from mikolalysenko/mathmode
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmathmode.js
More file actions
64 lines (57 loc) · 1.51 KB
/
Copy pathmathmode.js
File metadata and controls
64 lines (57 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var latex = require("latex");
var spawn = require("child_process").spawn;
function sanitize(expr) {
var sanitized = expr.replace(/([^\\](\\\\)*)(\$)/g, "$1");
if(sanitized.charAt(0) === "$") {
return sanitized.substr(1)
}
return sanitized
}
module.exports = function(expr, options) {
if(!options) {
options = {};
}
var format = options.format || "png";
var size = options.dpi || 300;
var package_list = options.packages || [ "amsmath" ];
var packages = [];
for(var i=0; i<package_list.length; ++i) {
var pkg = package_list[i];
if(pkg instanceof Array) {
packages.push("\\usepackage[" + pkg[1] + "]{" + pkg[0] + "}");
} else {
packages.push("\\usepackage{" + pkg + "}");
}
}
var tex_stream = latex([
"\\nonstopmode",
"\\documentclass{minimal}",
packages.join("\n"),
"\\usepackage[active,tightpage]{preview}",
"\\usepackage{transparent}",
options.macros || "",
"\\begin{document}",
"\\begin{preview} $",
sanitize(expr),
"$ \\end{preview}",
"\\end{document}"
], {
command: options.pdflatex_path || "pdflatex",
format: "pdf"
});
var convert_path = options.imagemagick_path || "convert";
var convert = spawn(convert_path, [
"-trim",
"-density", "" + size,
"-quality", "100",
"pdf:-",
format + ":-"
]);
var result = convert.stdout;
tex_stream.on("error", function(err) {
result.emit("error", err);
convert.kill();
});
tex_stream.pipe(convert.stdin);
return result;
}