Issue in latex rendering

Hi everyone,

I’m encountering an issue with the st.write method for rendering LaTeX expressions; it’s being displayed as plain text instead of properly formatted LaTeX. However, the same LaTeX expression works correctly with st.latex.

I’m using Streamlit version 1.33.0.

Here’s the code:

This works:

st.latex(r"""
\text{Annual Cost} = \left(\frac{\text{Miles per Year}}{\text{Fuel Efficiency}}\right) \times \text{Price per Gallon}
""")

This doesn’t:

st.write("""
$$
\text{Annual Cost} = \left(\frac{\text{Miles per Year}}{\text{Fuel Efficiency}}\right) \times \text{Price per Gallon}
$$
""")

I’m new to Streamlit and not sure if I’m missing something in the syntax. Also, does math text mode \(. . . \) and display mode \[. . . \] work in Streamlit?

Thank you

The backslash is used as an escape character in python string literals. If you want an actual backslash, use a raw string literal or two backslashes.

st.write(r"""
$$
\text{Annual Cost} = \left(\frac{\text{Miles per Year}}{\text{Fuel Efficiency}}\right) \times \text{Price per Gallon}
$$
""")
st.write("""
$$
\\text{Annual Cost} = \\left(\\frac{\\text{Miles per Year}}{\\text{Fuel Efficiency}}\\right) \\times \\text{Price per Gallon}
$$
""")
1 Like

Thank you @Goyo, it worked.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.