将 mathjax 输出转换为 png
Convert mathjax output to png
我正在尝试将 mathjax 输出转换为 png 文件,我使用了如下代码,我有一个 html 代码,如
<div id="target"></div>
和一个 js 文件,如
window.MathJax = {
jax: ["input/TeX", "output/SVG"],
extensions: ["tex2jax.js", "MathMenu.js", "MathZoom.js"],
showMathMenu: false,
showProcessingMessages: false,
messageStyle: "none",
SVG: {
useGlobalCache: false
},
TeX: {
extensions: ["AMSmath.js", "AMSsymbols.js", "autoload-all.js"]
},
AuthorInit: function() {
MathJax.Hub.Register.StartupHook("End", function() {
var mj2img = function(texstring, callback) {
var input = texstring;
var wrapper = document.createElement("div");
wrapper.innerHTML = input;
var output = { svg: "", img: ""};
MathJax.Hub.Queue(["Typeset", MathJax.Hub, wrapper]);
MathJax.Hub.Queue(function() {
var mjOut = wrapper.getElementsByTagName("svg")[0];
mjOut.setAttribute("xmlns", "http://www.w3.org/2000/svg");
// thanks, https://spin.atomicobject.com/2014/01/21/convert-svg-to-png/
output.svg = mjOut.outerHTML;
var image = new Image();
image.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(output.svg)));
image.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
output.img = canvas.toDataURL('image/png');
callback(output);
};
});
}
mj2img("\[f: X \to Y\]", function(output){
document.getElementById("target").innerText = output.img + '\n' + output.svg;
});
});
}
};
(function(d, script) {
script = d.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.onload = function() {
// remote script has loaded
};
script.src = 'https://cdn.mathjax.org/mathjax/latest/MathJax.js';
d.getElementsByTagName('head')[0].appendChild(script);
}(document));
但我得到了一些垃圾值和 svg 代码。检查 here。如何让它在浏览器中转换 png 文件?
您当前正在将 Base64 图像数据和 SVG 输出连接并显示为字符串,并将结果作为字符串分配给目标的 innerText。
相反,Base64 数据应显示为 <img>
元素(参见:How to display Base64 images in HTML),而 SVG 可指定为容器元素的 innerHTML。
mj2img('\[f: X \to Y\]', function (output) {
// document.getElementById("target").innerText = output.img + '\n' + output.svg;
const target = document.getElementById('target');
const img = document.createElement('img');
img.src = output.img;
const svg_container = document.createElement('div');
svg_container.innerHTML = output.svg;
target.append(img, svg_container);
});
我正在尝试将 mathjax 输出转换为 png 文件,我使用了如下代码,我有一个 html 代码,如
<div id="target"></div>
和一个 js 文件,如
window.MathJax = {
jax: ["input/TeX", "output/SVG"],
extensions: ["tex2jax.js", "MathMenu.js", "MathZoom.js"],
showMathMenu: false,
showProcessingMessages: false,
messageStyle: "none",
SVG: {
useGlobalCache: false
},
TeX: {
extensions: ["AMSmath.js", "AMSsymbols.js", "autoload-all.js"]
},
AuthorInit: function() {
MathJax.Hub.Register.StartupHook("End", function() {
var mj2img = function(texstring, callback) {
var input = texstring;
var wrapper = document.createElement("div");
wrapper.innerHTML = input;
var output = { svg: "", img: ""};
MathJax.Hub.Queue(["Typeset", MathJax.Hub, wrapper]);
MathJax.Hub.Queue(function() {
var mjOut = wrapper.getElementsByTagName("svg")[0];
mjOut.setAttribute("xmlns", "http://www.w3.org/2000/svg");
// thanks, https://spin.atomicobject.com/2014/01/21/convert-svg-to-png/
output.svg = mjOut.outerHTML;
var image = new Image();
image.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(output.svg)));
image.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
output.img = canvas.toDataURL('image/png');
callback(output);
};
});
}
mj2img("\[f: X \to Y\]", function(output){
document.getElementById("target").innerText = output.img + '\n' + output.svg;
});
});
}
};
(function(d, script) {
script = d.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.onload = function() {
// remote script has loaded
};
script.src = 'https://cdn.mathjax.org/mathjax/latest/MathJax.js';
d.getElementsByTagName('head')[0].appendChild(script);
}(document));
但我得到了一些垃圾值和 svg 代码。检查 here。如何让它在浏览器中转换 png 文件?
您当前正在将 Base64 图像数据和 SVG 输出连接并显示为字符串,并将结果作为字符串分配给目标的 innerText。
相反,Base64 数据应显示为 <img>
元素(参见:How to display Base64 images in HTML),而 SVG 可指定为容器元素的 innerHTML。
mj2img('\[f: X \to Y\]', function (output) {
// document.getElementById("target").innerText = output.img + '\n' + output.svg;
const target = document.getElementById('target');
const img = document.createElement('img');
img.src = output.img;
const svg_container = document.createElement('div');
svg_container.innerHTML = output.svg;
target.append(img, svg_container);
});