50

The fact is, that in official documentation Jupyter - motivating examples stands

Equation numbering and referencing will be available in a future version of the Jupyter notebook.

I know there is a lot of discussion about this topic. There are some people who claim to solve this issue with some workarounds.

But for ordinary user it is hard to understand the workarounds, or how dirty/useful the hacks really are.

So my questions are:

  1. what means the "available in future version"? Does it mean something like "new month/year" or something like "probably never because it is too impossible"?

  2. If any of the workarounds provided on the Internet safe for a human consumption? I mean is it worthy? Because it is possible to use Sphinx or something else for creation of tutorials, it will be more work, but it will be more work that implementing some hacks, installing plug-ins and so on?

Note: For somebody it could seems to be a question requiring opinion based answer, but I am pretty sure it is not. Any advice can help me (or others users) to make a good/bad decision.

3
  • 1
    It's partly opinionated, partly unanswerable. #1 can only be answered by the jupyter devs, if anybody. #2 is way too broad, and a request for off-site resources. I'm afraid none of your question is on-topic on Stack Overflow. Dec 20, 2016 at 12:15
  • 1
    @AndrasDeak Thank you for response. I get your point. But still I think this question is legitimate because it seems that too many people need to know the same thing. And even if the question is unanswerable today, it can be answered acceptably in future.
    – matousc
    Dec 20, 2016 at 12:47
  • I don't have too strong feelings about it (hence no downvotes or closevotes on your question yet:P), so we'll see what others think. Just wanted to give you a heads-up:) Dec 20, 2016 at 12:51

5 Answers 5

41

I believe that essentially all information relevant to this question can be found in this long Github issue thread.

The conversation there has been ongoing for (at this moment) 3.5 6.5 8 years and is still active. Important highlights:

  • You can very simply turn on numbering by executing a cell with the following content:

    %%javascript
    MathJax.Hub.Config({
        TeX: { equationNumbers: { autoNumber: "AMS" } }
    });
    
  • There is an extension for equation numbering.

  • Developer minrk has suggested that this extension is the right approach and could be merged into master (but the functionality would be turned off by default).

To install the extension via pip:

pip install jupyter_contrib_nbextensions

To install the extensions via Anaconda:

conda install -c conda-forge jupyter_contrib_nbextensions 

After using one of the ways to install provided above, enable the extension:

jupyter contrib nbextension install --user
jupyter nbextension enable equation-numbering/main
8
  • 2
    It worked perfectly. But how do I make a reference to the equation in a different cell? $\ref{label}$ doesn't seem to work.
    – bmello
    Sep 27, 2019 at 20:04
  • 3
    See linked extension by David for example usage. Jun 6, 2020 at 11:11
  • Yet did not find the automatic referencing to the equations. Any help? Sep 14, 2021 at 8:36
  • what if would I like recall the equation during text? Nov 30, 2021 at 9:45
  • Numbering seems to restart at "1" for each cell when I try this.
    – MRule
    Nov 30, 2021 at 10:02
27

Here is a working example, to be entered in a markdown cell:

\begin{equation*}
\mathbf{r} \equiv \begin{bmatrix}
y \\
\theta
\end{bmatrix}
\label{eq:vector_ray} \tag{1}
\end{equation*}

Vector **r** is defined by equation $\eqref{eq:vector_ray}$

It's self explanatory but here's some details:

\label : name describing he equation

\tag : the label appearing next to the equationcan be a number or letters

\eqref : reference to the labeled equation

This will be shown as: enter image description here

4
  • 4
    This only seems to work if the eqref is in the same cell as the label - any way to make it work across the entire notebook?
    – Corvus
    Nov 6, 2019 at 10:22
  • 1
    \eqref works beautifully for me across the whole notebook when combining it with automatic numbering, just make a normal equation (no star in above example and no tag) and reference it as usual. I need to press "reset equation numbering" for the references to update
    – poppie
    Apr 3, 2020 at 13:13
  • This works for me -- I am wondering why it shows the periods on either side of the (1) when referring to the equation? I would love to remove those.
    – eric
    May 17, 2020 at 16:57
  • @poppie, where did you "press" "reset equation numbering"? I can't seem to be able to reference an equation in a different cell....
    – jrive
    Feb 10 at 15:21
1

A quick follow-up on cross cell references for all JupyterLab users:

Since (to my knowledge) jupyter_contrib_nbextensions do not work with JupyterLab, you can just use the code from the equation-numbering extension directly in your notebooks. This is how it kind of worked for me:

Cell A (code cell):

%%javascript
MathJax.Hub.Config({
    TeX: { equationNumbers: { autoNumber: "AMS" } }
});

Cell B (code cell):

%%javascript
MathJax.Hub.Queue(
  ["resetEquationNumbers", MathJax.InputJax.TeX],
  ["PreProcess", MathJax.Hub],
  ["Reprocess", MathJax.Hub]
);

Cell C (markdown cell):

\begin{equation}
    a = b + c \label{eq:some_eq}
\end{equation}

This is a same-cell reference to Eq. \ref{eq:some_eq}.

Cell D (markdown cell):

This is a cross cell reference to Eq. \ref{eq:some_eq}

For the cross-cell reference to work you have to run cell B again after running cell D (this also answers the question in @jrive's comment but appearently I don't have enugh reputation to write a comment).

I say it "kind of worked" because there is one problem: So far I could not figure out how to make the auto-numbering work properly across multiple notebooks. If I reset the numbering as described above, the equations are numbered in the same order the cells were run (not sure about this though, it sometimes seems a bit random), regardless of the notebook they belong to. So you might end up having Eq. 1, 2 and 4 in one notebook and Eq. 3 in the second. This might look somewhat weird, but at least all references link to the correct equation... Maybe someone has a solution for this?

0

The only consistent way I have been able to cross-reference equations between markdown cells is to put an internal html link <a id="eq_1.1"></a> as a target before the \begin{equation} or \begin{align} in the equation. Whatever is inside the id quotes is any arbitrary target string that isn't duplicated. However, including "eq_" helps keep the links straight. This sets up the link target.

Then, where you want to reference this equation in another cell, insert [internal link name](#eq_1.1) in the markdown cell. Whatever is inside the [] is any arbitrary string. What is inside the () must be # followed by the target string. You have to run both the target and the reference cell before this will work. You can use this same method to cross-reference tables, images, etc.

Ref:

-1

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. You will see that a button appears on the top of your notebook for resetting the numbering of equations. You will need to press that button every now and then.

1
  • Conda->Jupyter->Notebook -> Edit -> "nbextensions config" simply isn't there. May 30, 2022 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.