Skip to content
Advertisement

Save plotly figure interactively (html) whilst preserving LaTeX font

I created a plotly figure using python and I am aware that one can save the interactive figure in html format by using:

fig.write_html("name_of_figure.html")

For the axis labels, as well as the title of the figure, I used LaTeX fonts like this:

fig.update_layout(title=r'$text{Some title}_2$')

When I render it in my browser directly the LaTeX fonts are displayed correctly. However, when I save the figure in .html format, the title, as well as the axis labels, are not rendered using LaTeX fonts. I rather see the plain text like $text{Some title}_2$.
What can I do to circumvent that problem?

Advertisement

Answer

Add an include_mathjax = 'cdn' parameter to .write_html.

And read the documentation of write_html function carefully :)

include_mathjax: bool or string (default False)

Specifies how the MathJax.js library is included in the output html div string. MathJax is required in order to display labels with LaTeX typesetting.

If False, no script tag referencing MathJax.js will be included in the output.

If ‘cdn’, a script tag that references a MathJax CDN location will be included in the output. HTML div strings generated with this option will be able to display LaTeX typesetting as long as internet access is available.

If a string that ends in ‘.js’, a script tag is included that references the specified path. This approach can be used to point the resulting HTML div string to an alternative CDN.

import plotly.express as px
fig = px.line(x = [0,1,2], y = [0,1,4])
fig.update_layout(title=r'$text{Some title}_2$')
fig.write_html("example_figure.html", include_mathjax = 'cdn')
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement