Numbering in init_printing(latex_mode='equation')

135 views
Skip to first unread message

Glenn Ramsey

unread,
Jan 8, 2024, 2:50:45 AM1/8/24
to sympy
Hi,

When using init_printing(latex_mode='equation') the equations are numbered in the output and these appear sequentially in pdf output produced by nbconvert.

Is there a way to label the equations so they can be referred to in markdown cells?

Glenn

Glenn Ramsey

unread,
Jan 15, 2024, 3:48:24 PM1/15/24
to sy...@googlegroups.com
Perusing the source it seems not.

I think it might be possible to implement this by adding a label="foo" keyword
argument to sympy.display(...).

A question now is if I did that and referred to that in the markdown with
something like this: $\@ref(foo)$, would that get picked up? I'm really only
interested in this for static presentation output (pdf, html) so it wouldn't
matter if it doesn't work in interactive modes.

Glenn

Aaron Meurer

unread,
Jan 15, 2024, 5:06:42 PM1/15/24
to sy...@googlegroups.com
On Mon, Jan 15, 2024 at 1:48 PM Glenn Ramsey <glenn.r...@gmail.com> wrote:
>
> On 8/01/24 11:57, Glenn Ramsey wrote:
>
> > When using init_printing(latex_mode='equation') the equations are numbered in
> > the output and these appear sequentially in pdf output produced by nbconvert.
> >
> > Is there a way to label the equations so they can be referred to in markdown cells?
>
> Perusing the source it seems not.
>
> I think it might be possible to implement this by adding a label="foo" keyword
> argument to sympy.display(...).

That sounds reasonable.

Although maybe this is already possible with init_printing() by
setting latex_printer to something that adds the label.

>
> A question now is if I did that and referred to that in the markdown with
> something like this: $\@ref(foo)$, would that get picked up? I'm really only
> interested in this for static presentation output (pdf, html) so it wouldn't
> matter if it doesn't work in interactive modes.

I think the only way to know if it will work or not is to test it. I
suspect it will work for pdf output, but I have no idea if it will
work in the browser. It depends on whether MathJax supports \ref.

Aaron Meurer

>
> Glenn
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/89dce9a0-548d-4d46-a862-a3305438bd44%40gmail.com.

Memeriksa

unread,
Jan 16, 2024, 12:43:51 AM1/16/24
to sympy

Matthias Geier

unread,
Jan 16, 2024, 1:08:26 PM1/16/24
to sy...@googlegroups.com
Hi Glenn, all.

On Mon, Jan 15, 2024 at 11:06 PM Aaron Meurer <asme...@gmail.com> wrote:
> On Mon, Jan 15, 2024 at 1:48 PM Glenn Ramsey <glenn.r...@gmail.com> wrote:
> > On 8/01/24 11:57, Glenn Ramsey wrote:

[...]

> > A question now is if I did that and referred to that in the markdown with
> > something like this: $\@ref(foo)$, would that get picked up? I'm really only

You should not use $-signs, because references don't happen in math
mode, they are part of normal text.
The correct usage is \ref{foo} or \eqref{foo}, without any additional
characters.

> > interested in this for static presentation output (pdf, html) so it wouldn't
> > matter if it doesn't work in interactive modes.
>
> I think the only way to know if it will work or not is to test it. I
> suspect it will work for pdf output, but I have no idea if it will
> work in the browser. It depends on whether MathJax supports \ref.

MathJax does support \ref{...}, as long as it is configured appropriately.

There are some examples in the nbsphinx docs:
https://nbsphinx.readthedocs.io/en/0.9.3/markdown-cells.html#Manual-Equation-Numbering

This also works on nbviewer (which is based on nbconvert):
https://nbviewer.org/urls/nbsphinx.readthedocs.io/en/0.9.3/markdown-cells.ipynb#Manual-Equation-Numbering

It also works on Github:
https://github.com/spatialaudio/nbsphinx/blob/0.9.3/doc/markdown-cells.ipynb

Of course, this also works for PDF output:
https://nbsphinx.readthedocs.io/_/downloads/en/0.9.3/pdf/

It does work on the old Jupyter notebook:
https://mybinder.org/v2/gh/spatialaudio/nbsphinx/0.9.3?filepath=doc/markdown-cells.ipynb

And it does work on JupyterLab:
https://mybinder.org/v2/gh/spatialaudio/nbsphinx/0.9.3?labpath=doc/markdown-cells.ipynb

cheers,
Matthias

Glenn Ramsey

unread,
Jan 18, 2024, 4:44:32 PM1/18/24
to sy...@googlegroups.com
Hi Matthias, Aaron, All,

It works. I've hacked something together to add a \label{...} inside the
equation environment. It is called like this:

init_printing(latex_mode="equation", label="quadratic")
display(ex2)

Now the equation can be referenced using \ref{quadratic} or \eqref{quadratic} in
a notebook markdown cell.

init_printing() needs to be called to set the equation reference before every
display() call. Once init_printing() is called with a label argument it will
persist thereafter. This is kind of clunky but it gets the job done.

In PDF output from 'jupyter nbconvert' the numbering works but in interactive
Jupyter views the equation references display as ???, which was expected.

It doesn't work in the vscode Jupyter extension because that uses KaTex, which
doesn't support equation references and produces an error message instead of
displaying the equation.

Glenn
Reply all
Reply to author
Forward
0 new messages