# ENH: added StackExchange-style MathJax filtering #2349

Merged
merged 4 commits into from Oct 20, 2012

## Conversation

Projects
None yet
8 participants
Contributor

### ahmadia commented Aug 27, 2012

 Attempts to fix Issue #2289 Resubmission of accidentally merged Pull Request #2337

### travisbot commented Aug 27, 2012

 This pull request passes (merged 0af930d7 into 011222a).

Closed

Owner

### fperez commented Sep 6, 2012

 This is great! First, I'm testing the problem blocks listed in #2289. The following in a cell: ## Problem 1 $$\begin{array}{c} y_1 \\\ y_2 \mathtt{t}_i \\\ z_{3,4} \end{array}$$ ## Problem 2 $$\begin{array}{c} y_1 \cr y_2 \mathtt{t}_i \cr y_{3} \end{array}$$ ## Problem 3 $$\begin{eqnarray} x' &=& &x \sin\phi &+& z \cos\phi \\ z' &=& - &x \cos\phi &+& z \sin\phi \\ \end{eqnarray}$$ ## Problem 4 Inline math $\mathbf{A}_{i}B_{j}$.  produces this image: while master gives: So this is obviously major progress! Thanks a lot @ahmadia, I'll have a look now at the code. Pinging @minrk in case he's OK considering this for a 0.13.1 backport, as I think this is super high value.

### fperez and 1 other commented on an outdated diff Sep 6, 2012

IPython/frontend/html/notebook/static/js/textcell.js

#### ellisonbg Oct 11, 2012

Owner

Don't know how I feel about top level attributes. You have protected them using the JS module pattern though. The alternative would be to create a class that has all of this logic. @Carreau what do you think about this?

### ellisonbg and 1 other commented on an outdated diff Oct 11, 2012

IPython/frontend/html/notebook/static/js/mathjaxutils.js
 + var blocks, start, end, last, braces; // used in searching for math + var math; // stores math until pagedown (Markdown parser) is done + var HUB = MathJax.Hub; + + // 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; + + // The math is in blocks i through j, so + // collect it into one block and clear the others. + // Replace &, <, and > by named entities. + // For IE, put
at the ends of comments since IE removes \n. + // Clear the current math positions and store the index of the + // math, then push the math string onto the storage array. + // The preProcess function is called on all blocks if it has been passed in + function processMath(i, j, preProcess) {

#### ellisonbg Oct 11, 2012

Owner

We are trying to use names such as process_math in our JS code. Can you convert all your names to this convention?

Contributor

sure, is there a style guide somewhere that the team is following?

#### ellisonbg Oct 11, 2012

Owner

No, not really. Here is a summary:

• For the most part we are following Python's PEP 8 standard, so our devs can use one standard for coding in both JS and Python.
• But there are many things in JS that PEP 8 doesn't cover. For those, we have decided on an style, and started to use it semi-consistently. We have not documented it, but just have a look at some of the other .js files to see what it looks like.
• These things are evolving.
Owner

### ellisonbg commented Oct 11, 2012

 So this is looking pretty good now. A few comments: The code is pretty complex and it is difficult to tell from just looking at it that it will work. How much of this is taken from StackExchange? I do like the sample notebook that demonstrates the capabilities though. Please fix the naming of things: likeThis -> like_this. What version of MathJax is required for the new features like equation numbering?

### ellisonbg commented on an outdated diff Oct 11, 2012

IPython/frontend/html/notebook/static/js/mathjaxutils.js
 + j--; + } + blocks[i] = "@@" + math.length + "@@"; // replace the current block text with a unique tag to find later + if (preProcess) + block = preProcess(block); + math.push(block); + start = end = last = null; + } + + // Break up the text into its component parts and search + // through them for math delimiters, braces, linebreaks, etc. + // Math delimiters must match and braces must balance. + // Don't allow math to pass through a double linebreak + // (which will be a paragraph). + // + function removeMath(text) {

#### ellisonbg Oct 11, 2012

Owner

We are trying to use the var foo = function () style of function defs, please convert to this.

Owner

### ellisonbg commented Oct 11, 2012

 I just tried this branch and it is completely broken with the version of MathJax I am using. @Carreau @minrk @fperez how should we handle MathJax versioning issues? Sort of difficult.. Minimally, we should make sure that the install_mathjax function will only install versions that work with the current code base.
Owner

### ellisonbg commented Oct 11, 2012

 I just updated to v2.0 of MathJax and it is still broken. I see a JS console error: Uncaught Error: Can't make callback from given data, MathJax.js:29 The notebook doesn't even finish loading.
Owner

### ellisonbg commented Oct 11, 2012

 Got it, you have to clear your browser cache when updating MathJax. The example notebook looks spectacular!
Contributor

### ahmadia commented Oct 11, 2012

 The code is pretty complex and it is difficult to tell from just looking at it that it will work. How much of this is taken from StackExchange? I do like the sample notebook that demonstrates the capabilities though. The majority of the logic for stripping TeX/LaTeX before sending text to PageDown then replacing it after PageDown has rendered the Markdown-formatted text to HTML-formatted text comes from the StackExchange javascript source. Unfortunately, the javascript is no longer available from the URL I originally accessed it at, but was linked to from this question. I improved the source code by incorporating a more robust regular expression parser with explicit licensing. The StackExchange code is not copyright, but it is also not released in any way. I was given permission by the moderator team in a StackExchange logged and hosted chat session to reuse the code. As pointed out on the discussion list, it would be great if we had a better Markdown parser than Pagedown, but until the competing projects are more mature, I think we are better off piggy-backing larger projects like StackExchange. Are there any other concerns beside the naming issues?
Owner

### ellisonbg commented Oct 11, 2012

 Other than the naming issues, I want to wait and see what the others think about using module level constants like this. We might want to make it an object instead, but it probably doesn't matter because you have properly encapsulated those attributes. Thanks for providing a bit more history on the code from SE. Could you put that in the file as a comment for future develoeprs?
Contributor

### ahmadia commented Oct 11, 2012

 @ellisonbg - It turns out the licensing information wasn't 100% correct. Davide Cervone (@dpvc on github), the author of MathJax, appears to have provided it to the StackExchange website. See the comment thread on his answer here. He's granting permission under an Apache2 license. I'll hold off on touching the pull request until a few of the other developers have had a chance to look at it.

 ahmadia  partial refactor, can't quite get environments working  9481beb ahmadia  correct environment rendering  7d64520 ahmadia  added math typesetting example notebook  2d0fdb4 ahmadia  Refactored to JS standards. Fixed Attribution.  thisStyle --> this_style. function foo() --> var foo = function() StackExchange improperly attributed for Davide Cervone's Markdown+MathJax handling. This has been fixed. Ref: http://stackoverflow.com/a/11231030/122022 http://www.math.union.edu/~dpvc/transfer/mathjax/mathjax-editing.js 881ad26
Contributor

Owner

### ellisonbg commented Oct 20, 2012

 OK I think this looks good, merging.

### ellisonbg added a commit that referenced this pull request Oct 20, 2012

 ellisonbg Merge pull request #2349 from ahmadia/mathjax_fix ENH: added StackExchange-style MathJax filtering d66b481

### ellisonbg merged commit d66b481 into ipython:master Oct 20, 2012 1 check passed

#### 1 check passed

default The Travis build passed
Details
Owner

### Carreau commented Oct 21, 2012

 Am I the only one to get a totally broken notebook after this merge ? With mathjax.js line 29 throwing Uncaught Error: Can't make callback from given data ?
Owner

### Carreau commented Oct 21, 2012

 FYI, it break when user have old mathjax version installed locally, they then need to upgrade to 2.0, check that the old version is removed (new version is installed in profile) and clear cache.
Contributor

### ahmadia commented Oct 21, 2012

 Brian pointed that out in the PR, and I didn't properly fix it :( A On Sun, Oct 21, 2012 at 4:20 PM, Bussonnier Matthias < notifications@github.com> wrote: FYI, it break when user have old mathjax version installed locally, they then need to upgrade to 2.0, check that the old version is removed (new version is installed in profile) and clear cache. — Reply to this email directly or view it on GitHubhttps://github.com/ipython/ipython/pull/2349#issuecomment-9643562.
Contributor

### ahmadia commented Oct 21, 2012

 @Carreau @ellisonbg - I'm not familiar with the bit of code responsible for local MathJax installs. Is this something that some attention from me can help, or should I leave it to one of you to fix?
Contributor

### ahmadia commented Oct 21, 2012

 @Carreau - One thing I think I can do is try and detect if the MathJax library is out of date and issue an alert to the user. Would this be useful?
Owner

### ellisonbg commented Oct 21, 2012

 Yes, I would put this information in the mathjaxinit function. There is a version attribute of MathJax namespace and its value should be "2.0". If it doesn't match that, let's pop up a jquery dialog displaying a message that the user needs to update MathJax. Note, they will probably also have to clear their browser cache after doing the update. On Sun, Oct 21, 2012 at 9:20 AM, ahmadia notifications@github.com wrote: @Carreau https://github.com/Carreau - One thing I think I can do is try and detect if the MathJax library is out of date and issue an alert to the user. Would this be useful? — Reply to this email directly or view it on GitHubhttps://github.com/ipython/ipython/pull/2349#issuecomment-9644148. Brian E. Granger Cal Poly State University, San Luis Obispo bgranger@calpoly.edu and ellisonbg@gmail.com
Owner

### Carreau commented Oct 21, 2012

 Yes, warning would be usefull I think, but number of user with local mathjax is IMHO small. As for mathjax installing, you could have a look at #2446 and #2267 to see how it is done.
Owner

### ellisonbg commented Oct 24, 2012

 I am seeing two problems related to this branch: Rendering in notebook load is really slow and is blocking the notebook UI. I am still seeing the error (Uncaught Error: Can't make callback from given data, MathJax:29 ) when I reload a notebook with equations in it. Can others try this out.
Owner

### Carreau commented Oct 24, 2012

 I am still seeing the error (Uncaught Error: Can't make callback from given data, MathJax:29 ) when I reload a notebook with equations in it. Can others try this out. For me only sometimes.
Owner

### ellisonbg commented Oct 24, 2012

 Yes, it is inconsistent for me too - probably a timing issue or something. On Wed, Oct 24, 2012 at 10:05 AM, Bussonnier Matthias < notifications@github.com> wrote: I am still seeing the error (Uncaught Error: Can't make callback from given data, MathJax:29 ) when I reload a notebook with equations in it. Can others try this out. For me only sometimes. — Reply to this email directly or view it on GitHubhttps://github.com/ipython/ipython/pull/2349#issuecomment-9747517. Brian E. Granger Cal Poly State University, San Luis Obispo bgranger@calpoly.edu and ellisonbg@gmail.com

### ketch commented Oct 25, 2012

 After installing this, I also see very slow math rendering or (often, not always) the notebook doesn't seem to load at all; I just get a single cell that says "Type Markdown and LaTeX: α2". I see both effects in both Safari and Chrome. I would love to have this functionality, though. It's very annoying that with 0.13 I can't cut/paste valid laTeX in ipython notebooks. But for now this has forced me to roll back to the release version.
Owner

### ellisonbg commented Oct 25, 2012

 I see the same things. On Thu, Oct 25, 2012 at 5:23 AM, David Ketcheson notifications@github.comwrote: After installing this, I also see very slow math rendering or (often, not always) the notebook doesn't seem to load at all; I just get a single cell that says "Type Markdown and LaTeX: α2". I would love to have this functionality, though. It's very annoying that with 0.13 I can't cut/paste valid laTeX in ipython notebooks. — Reply to this email directly or view it on GitHubhttps://github.com/ipython/ipython/pull/2349#issuecomment-9775424. Brian E. Granger Cal Poly State University, San Luis Obispo bgranger@calpoly.edu and ellisonbg@gmail.com
Contributor

### ahmadia commented Oct 25, 2012

 Thanks for feedback and apologies for the bug. I'm improving the logic with regards to rendering, will report back soon. A On Thu, Oct 25, 2012 at 5:47 PM, Brian E. Granger notifications@github.comwrote: I see the same things. On Thu, Oct 25, 2012 at 5:23 AM, David Ketcheson notifications@github.comwrote: After installing this, I also see very slow math rendering or (often, not always) the notebook doesn't seem to load at all; I just get a single cell that says "Type Markdown and LaTeX: á2". I would love to have this functionality, though. It's very annoying that with 0.13 I can't cut/paste valid laTeX in ipython notebooks. Reply to this email directly or view it on GitHub< https://github.com/ipython/ipython/pull/2349#issuecomment-9775424>. Brian E. Granger Cal Poly State University, San Luis Obispo bgranger@calpoly.edu and ellisonbg@gmail.com Reply to this email directly or view it on GitHubhttps://github.com/ipython/ipython/pull/2349#issuecomment-9784917.
Owner

### ellisonbg commented Oct 25, 2012

 Thanks! On Thu, Oct 25, 2012 at 11:21 AM, ahmadia notifications@github.com wrote: Thanks for feedback and apologies for the bug. I'm improving the logic with regards to rendering, will report back soon. A On Thu, Oct 25, 2012 at 5:47 PM, Brian E. Granger notifications@github.comwrote: I see the same things. On Thu, Oct 25, 2012 at 5:23 AM, David Ketcheson notifications@github.comwrote: After installing this, I also see very slow math rendering or (often, not always) the notebook doesn't seem to load at all; I just get a single cell that says "Type Markdown and LaTeX: á2". I would love to have this functionality, though. It's very annoying that with 0.13 I can't cut/paste valid laTeX in ipython notebooks. Reply to this email directly or view it on GitHub< https://github.com/ipython/ipython/pull/2349#issuecomment-9775424>. Brian E. Granger Cal Poly State University, San Luis Obispo bgranger@calpoly.edu and ellisonbg@gmail.com Reply to this email directly or view it on GitHub< https://github.com/ipython/ipython/pull/2349#issuecomment-9784917>. — Reply to this email directly or view it on GitHubhttps://github.com/ipython/ipython/pull/2349#issuecomment-9788241. Brian E. Granger Cal Poly State University, San Luis Obispo bgranger@calpoly.edu and ellisonbg@gmail.com

Merged

Contributor

### ahmadia commented Oct 25, 2012

 See #2517 for several superficial fixes and the fix for the notebook. Note that I've also turned rendering off for placeholder cells, we can reenable this pretty trivially (comment in #2517 on the fix suggested there).

### fperez added a commit that referenced this pull request Nov 2, 2012

 fperez Merge pull request #2517 from ahmadia/mathjax_fix2 Addresses the javascript errors resulting in a blank notebook pointed out in #2349 as well as significant performance degradations that were introduced by that merge. It also disables equation numbering/references, fixes broken offline access and re-typesets individual Notebook Cells instead of the entire document on edit. Closes #2289. 2962dae

Closed

Closed

Closed

### mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014

 ellisonbg Merge pull request #2349 from ahmadia/mathjax_fix ENH: added StackExchange-style MathJax filtering eee115c

### mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014

 fperez Merge pull request #2517 from ahmadia/mathjax_fix2 Addresses the javascript errors resulting in a blank notebook pointed out in #2349 as well as significant performance degradations that were introduced by that merge. It also disables equation numbering/references, fixes broken offline access and re-typesets individual Notebook Cells instead of the entire document on edit. Closes #2289. 68777e0

Closed

Open