Open rawlins opened 1 year 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$'
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.
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:
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).HTML
object)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.
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
Output in colab:
Output in Jupyter (this is a reasonably current version but this has worked as long as I've been using it):
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.