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
35 changes: 28 additions & 7 deletions notebook/static/notebook/js/mathjaxutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ define([
// Other minor modifications are also due to StackExchange and are used with
// permission.

var inline = "$"; // the inline math delimiter

// 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 @@ -173,11 +171,16 @@ define([
// Look for math start delimiters and when
// found, set up the end delimiter.
//
if (block === inline || block === "$$") {
if (block === "$" || block === "$$") {
start = i;
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 +202,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