Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #980: incorrect LaTeX capturing and MathJax rendering #2220

Merged
merged 8 commits into from
Mar 9, 2017
31 changes: 27 additions & 4 deletions notebook/static/notebook/js/mathjaxutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ define([

// MATHSPLIT contains the pattern for math delimiters and special symbols
// needed for searching for math in the text input.
var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i;
var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[{}$]|[{}]|(?:\n\s*)+|@@\d+@@|\\\\(?:\(|\)|\[|\]))/i;

// The math is in blocks i through j, so
// collect it into one block and clear the others.
Expand Down Expand Up @@ -178,6 +178,11 @@ define([
end = block;
braces = 0;
}
else if (block === "\\\\\(" || block === "\\\\\[") {
start = i;
end = block.slice(-1) === "(" ? "\\\\\)" : "\\\\\]";
braces = 0;
}
else if (block.substr(1, 5) === "begin") {
start = i;
end = "\\end" + block.substr(6);
Expand All @@ -199,9 +204,27 @@ define([
// and clear the math array (no need to keep it around).
//
var replace_math = function (text, math) {
text = text.replace(/@@(\d+)@@/g, function (match, n) {
return math[n];
});
//
// Replaces a math placeholder with its corresponding group.
// The math delimiters "\\(", "\\[", "\\)" and "\\]" are replaced
// removing one backslash in order to be interpreted correctly by MathJax.
//
var math_group_process = function (match, n) {
var math_group = math[n];

if (math_group.substr(0, 3) === "\\\\\(" && math_group.substr(math_group.length - 3) === "\\\\\)") {
math_group = "\\\(" + math_group.substring(3, math_group.length - 3) + "\\\)";
} else if (math_group.substr(0, 3) === "\\\\\[" && math_group.substr(math_group.length - 3) === "\\\\\]") {
math_group = "\\\[" + math_group.substring(3, math_group.length - 3) + "\\\]";
}

return math_group;
};

// Replace all the math group placeholders in the text
// with the saved strings.
text = text.replace(/@@(\d+)@@/g, math_group_process);

return text;
};

Expand Down