jupyter-book icon indicating copy to clipboard operation
jupyter-book copied to clipboard

IPython.display with LaTeX does not render in book

Open matthew-brett opened this issue 2 years ago • 1 comments

Describe the bug

If I have a page with the following code:

# test

```{python}
from IPython.display import display, Markdown

display(Markdown('$y$'))
```

It renders correctly in the Jupyter notebook, with the $y$ rendered as LaTeX, but in the book, it renders as literal $y$. Should I expect this output not to render as LaTeX? Is there any workaround?

Reproduce the bug

git clone https://github.com/matthew-brett/minjb/
cd minjb
make

Inspect _build/html/test.html

List your environment

$ jupyter-book --version
Jupyter Book      : 0.15.1
External ToC      : 0.3.1
MyST-Parser       : 0.18.1
MyST-NB           : 0.17.2
Sphinx Book Theme : 1.0.1
Jupyter-Cache     : 0.6.1
NbClient          : 0.7.4

MacOS M2 Sonoma 14.0

matthew-brett avatar Oct 05 '23 16:10 matthew-brett

To handle dollarmath (as this bug is about), I've found the following workaround:

import re
from latex2mathml.converter import convert 
from IPython.display import display, Markdown

def handle_dollarmath(text):
    replace_latex = lambda match: convert(match.group(1))
    return re.sub(r'\$(.*?)\$', replace_latex, text)

display(Markdown(handle_dollarmath("*my markdown* $x^2$")))

Ideally, I think the markdown code inside a "display(Markdown" should be reinterpreted as MyST markdown during website compilation. This would allow the use of the same markdown syntax including admonitions and roles, but that would require a change in Jupyter book.

MortenHannemose avatar Sep 18 '24 07:09 MortenHannemose