11
votes

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?

1

1 Answers

8
votes

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.