Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython Notebook and Mathjax labeled equations

When I write the following in markdown mode in the iPython notebook:

\begin{equation}
\begin{split}
\dot u &= \pm u^2 +\delta u^3\\
\dot v &= v\\
\dot w &= -w,
\end{split}\label{eq:nf1}
\end{equation}

Equation \eqref{eq:nf1} bla bla bla ...

It does not render. It seems that the problem is

\label{eq:nf1}

since if I take it out, it renders.

I am doing references since at the end of the day I want to convert all I did to latex+PDF. In the PDF output I do receive numbered and well referenced equations, which is what I want. But It would be nice to also have the rendered equation in the iPython Notebook.

Is there a workaround for this? This is, can I render labeled equations in iPython Notebook?

like image 653
PepeToro Avatar asked Sep 16 '13 08:09

PepeToro


People also ask

How do you enumerate a formula in a Jupyter Notebook?

Go to your Jupyter Notebook editor (I am using Anaconda right now), Edit menu, the last item 'nbextensions config'. It opens a page where you can see a list of extensions, one of which is "Equation Auto Numbering". Enable it and restart your notebook.

How do you use math symbols in Jupyter Notebook?

To insert in-line math use the $ symbol within a Markdown cell. For example, the text $this_{is}^{inline}$ will produce: t h i s i s i n l i n e .

Can I use LaTeX in Jupyter Notebook?

The Jupyter Notebook uses MathJax to render LaTeX inside HTML / Markdown. Just put your LaTeX math inside $ $ . Or enter in display math mode by writing between $$ $$ .

How do you type math symbols in markdown?

Math inside RMarkdownIn side a text chunk, you can use mathematical notation if you surround it by dollar signs $ for “inline mathematics” and $$ for “displayed equations”. Do not leave a space between the $ and your mathematical notation. Example: $\sum_{n=1}^{10} n^2$ is rendered as ∑10n=1n2.


1 Answers

I've found a workaround, but it's not pretty.

In order to have MathJax number equations, you have to add a configuration flag (from MathJax docs). Configuration flags in IPython are set in IPython/html/static/notebook/js/mathjaxutils.js (as of IPython 1.1, at least). It seems like there should be a configuration option to hook into this, but I wasn't able to find one. Instead, I edited this manually:

>>> locate mathjaxutils.js
/home/me/.virtualenvs/nengo/lib/python2.7/site-packages/IPython/html/static/notebook/js/mathjaxutils.js
>>> emacs -nw `locate mathjaxutils.js`

In that file, modify the MathJax.Hub.Config call to include TeX: { equationNumbers: { autoNumber: "AMS" } },. It should look like:

// MathJax loaded                                                             
MathJax.Hub.Config({
    TeX: { equationNumbers: { autoNumber: "AMS" } }, # Add this line
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"] ],
        displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
        processEscapes: true,
        processEnvironments: true
    },
    displayAlign: 'left', // Change this to 'center' to center equations.     
    "HTML-CSS": {
        styles: {'.MathJax_Display': {"margin": 0}}
    }
});

Clear your cache and reload the notebook and you should have numbered equations that reference properly.

If you stop here, then rerunning a cell with a labelled equation will cause that equation to stop rendering, because the label already exists. As noted in the comments, there's a workaround for this as well.

In the same file as above, mathjaxutils.js, edit the process_math function to configure the hub's queue: hub.Queue( ["resetEquationNumbers",MathJax.InputJax.TeX], ["Typeset",hub] );. It should look like:

var process_math = function (i, j, pre_process, math, blocks) {
    var hub = MathJax.Hub;
    hub.Queue( ["resetEquationNumbers",MathJax.InputJax.TeX], ["Typeset",hub] );
    ...

This allows cells with labelled equations to be rerendered; however, equation numbering will then be local to each cell, meaning that you can't reference equations across cells.

like image 146
tbekolay Avatar answered Sep 22 '22 20:09

tbekolay