复制文本 - 加倍的字母(Mathjax)
问题描述:
我想问你关于“加倍字母”的问题,同时用Mathjax复制页面。复制文本 - 加倍的字母(Mathjax)
- 在第一页我尝试复制Mathjax公式是这样的: Look at picture 1
然后我收到记事本:∫x2dx= 2X +C∫x2dx= 2X + C
所以复制两次相同的公式。
- 在第二页我尝试复制Mathjax式是这样的: Look at picture 2
在这里,当我复制Mathjax式然后我接收在记事本中:(X + 1 )2 =?
所以这里它复制公式是正确的,它没有复制一倍。
这里是我的问题 - 这是什么原因,第一页给双倍公式,同时复制?这对我来说是个大问题,因为谷歌在第一页上阅读公式也是“两次”。我该如何修复这个问题?
(我不能把指向这些页面,因为我有太多低信誉)
答
这是由于MathJax的AssistiveMML extension中注入视觉隐藏,但访问MATHML到DOM一起MathJax的输出。
虽然MathML片段得到user-select:none
(为了防止复制&粘贴),但所有浏览器都不支持此CSS功能。
作为读者,您可以通过Accessibility> Assisitve MathML下的MathJax菜单(righ/cmd单击表达式)禁用扩展。
答
您可以添加此脚本以将MathJax元素直接复制为LaTeX或MML!它使用jQuery,但你可以manualy删除它:
var MathJaxRevertRendering = function(html){
var $ = jQuery;
var stencil = $('<div>' + html + '</div>');
var replaceMathJax = function(){
if ($(this).parents('.mjx-chtml').length) return;
var math = $(this).find('math');
if (math.length){
var latex = math.find('annotation[encoding="LaTeX"]');
if (latex.length){
$(this).replaceWith('$$' + $.trim(latex.html()) + '$$');
}else{
$(this).replaceWith(math[0].outerHTML);
}
}
};
if (stencil.find('.mjx-chtml').length){
stencil.find('.MathJax_Preview,script').remove();
stencil.find('.mjx-chtml').each(replaceMathJax);
html = stencil.html();
}
return html;
};
document.addEventListener('copy', function(e){
var getSelectionHtml = function() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
};
var html = getSelectionHtml();
var htmlLatexed = MathJaxRevertRendering(html);
if (html != htmlLatexed){
e.clipboardData.setData('text/html', htmlLatexed);
var removeHTML = true;
e.clipboardData.setData('text/plain', htmlLatexed.split(/(<math)|(<\/math>)/).reduce(function(buffer, val){
if (!val) return buffer;
if (val == '<math' || val == '</math>'){
removeHTML = !removeHTML;
buffer += val;
}else{
buffer+=removeHTML ? jQuery('<div>'+val+'</div>').text() : val;
}
return buffer;
}, ''));
e.preventDefault();
}
});
然后,任谁复制与MathJax元素的东西在你的网站会看到它的数学格式正确,当它粘贴到任何编辑器。