First lets use a newer version of MathJax, if possible to iron out any old bugs. See the suggested way to setup MathJax via HTML here.
I also found I had to set automargin
to true so the Y axis didn't get cut off, but you may not need this.
So our final .qmd file looks something like this:
---
format:
html:
embed-resources: true
---
```{python}
import plotly.express as px
from IPython.display import HTML, display
display(
HTML(
# NOTE: See new HTML code below
'<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-svg.js"></script>'
)
)
fig = px.line(
x=[0, 1, 2, 3, 4],
y=[1, 2, 4, 8, 16],
)
fig.update_layout(yaxis_title="$2^x$", xaxis_title="$x$")
# NOTE: See new automargin option below
fig.update_yaxes(automargin=True)
fig.show()
```