googlecolab / colabtools

Python libraries for Google Colaboratory
Apache License 2.0
2.17k stars 705 forks source link

LaTeX code not rendered in HTML outputs #3941

Open rawlins opened 1 year ago

rawlins commented 1 year ago

Describe the current behavior Latex code is not rendering in IPython.display.HTML output.

Describe the expected behavior Rendered latex, or at least: Jupyter renders latex in this context.

What web browser you are using firefox (but this is browser independent)

Additional context

from IPython.display import Markdown, HTML
display(Markdown("$x^y$"), HTML("$x^y$"))

Output in colab:

Screenshot 2023-08-16 at 12 55 33 PM

Output in Jupyter (this is a reasonably current version but this has worked as long as I've been using it):

Screenshot 2023-08-16 at 12 55 43 PM

Demo notebook here: https://colab.research.google.com/drive/1lI20l3gkngDgQZsxWqeLX0Ef0FzINJZW?usp=sharing

I realize this may or may not be a bug from your perspective, but it is at least a somewhat unfortunate divergence from Jupyter: I maintain a package that does some relatively involved rendering using _repr_html_ in order to mix html-based layout and math formulas, and I have yet to figure out a really great workaround short of loading mathjax in every iframe.

westurner commented 4 months ago

Just spent a few minutes on this before I realized that it's a colab issue:

latexstr = '$x_2=x^2$'
display(
    1, IPython.display.HTML(latexstr)._repr_html_(),
    #IPython.display.Latex(latexstr)._repr_html_(),
    #IPython.display.Math(latexstr)._repr_html_(),
    2,IPython.display.Latex(latexstr),
    3,IPython.display.Math(latexstr)
)
assert IPython.display.HTML(latexstr)._repr_html_() == '$x_2=x^2$'
gutow commented 2 months ago

I have had to add a work around to algebra-with-sympy because of this. The standard IPython display of strings returned from the IPython.display_formatter to outputs seems to wrap them in the HTML() call when operating in a Jupyter-like environment. My solution was to pass mixed LaTex and other strings as LaTex only. Not a great solution, but the string stuff I needed could be rewritten to be LaTex. This will not work if the string part also needs to be interpreted as HTML. I agree that this should be fixed to match the behavior of Jupyter Lab/Notebook.

rawlins commented 3 weeks ago

I meant to follow up with an explicit description of my workaround here and keep forgetting, but just ran across this thread again. The workaround is insane but has seemed pretty robust (and not nearly as slow as I would have thought, because katex is so fast). Essentially I inject katex autorender via the following recipe when I want to display an html display object that has some latex code:

  1. First, do something like IPython.display.display(IPython.display.Latex("")) which will cause colab to inject katex itself into the iframe (but only ever rendering the Latex part of the cell afaict, even if done in the other order).
  2. Then display the html normally (e.g. display an ipython HTML object)
  3. Then, inject autorender by displaying it with an ipython Javascript object. I've just put a minified version of a specific version (not updated very recently) in my code. Doing this triggers rendering on the html object in step 2. Edit: I think the render call in that line is also needed on top of just the autorender extension.