以交互方式保存图(html),同时保留 LaTeX 字体

Save plotly figure interactively (html) whilst preserving LaTeX font

我使用 python 创建了一个绘图图形,我知道可以使用 html 格式保存交互式图形:

fig.write_html("name_of_figure.html")

对于轴标签,以及图形的标题,我使用了这样的 LaTeX 字体:

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

当我直接在浏览器中渲染它时,LaTeX 字体显示正确。但是,当我以 .html 格式保存图形时,标题以及轴标签不会使用 LaTeX 字体呈现。我宁愿看到像 $\text{Some title}_2$.
这样的纯文本 我该怎么做才能避免这个问题?

include_mathjax = 'cdn' 参数添加到 .write_html

并仔细阅读 the documentation of write_html function :)

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')