pythonplotlykaleido

How to have a line break in the math mode text in plotly?


Suppose you would like to have a line break in the math mode text in plotly. The following solutions have been tried, but none of them is working in different reasons:

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Box(y=[10, 14]))

fig.update_layout(xaxis_title="$\alpha \\ \beta$") # causes to_image error
# fig.update_layout(xaxis_title=r"$\alpha \\ \beta$") # no breaks between math shape of alpha and beta
# fig.update_layout(xaxis_title="$\\alpha$<br>$\\beta$") # only shows math shape of alpha and no beta at all!
# fig.update_layout(xaxis_title="$\\alpha \\\\ \\beta$") # # no breaks between math shape of alpha and beta
# fig.update_layout(xaxis_title="$$\\alpha \\\\ \\beta$$") # no breaks between math shape of alpha and beta
# fig.update_layout(xaxis_title="$$\\alpha$$ <br> $$\\beta$$") # # only shows math shape of alpha and no beta at all!

fig.show()
fig.write_image("this_image.pdf")

So, the question is how to set a line break in a math mode text?


Solution

  • You seem to be struggling, by trial and error, with 3 (at least) problems (and it is hard to debug when you have found the correct way for one of the problem, but won't even know it unless you solve both in the same trial)

    1. Escaping

    Firstly, how to escape things. That is a pure python problem. You need to pass a string that contains things like \alpha and \\ to plotly. But in python '\alpha' is just 'lpha' (that is \a, the control char that, once upon a time, was meant to make the printer ding) followed by "lpha". And "\\" is a string containing a single \ (which is displayed \\ when you ask its value, because python escape it when printing values in interactive mode. But print("\\") prints a single \).

    So, the way to prevent escaping, as you have obviously tried already, is either to use "..." strings, and escape anything that python would interpret, like doubling \\. So "\\alpha \\\\ \\beta"

    Or to use r"..." strings.

    So that explains why your first line couldn't work

    2. Passing latex to plotly

    In plotly, only labels that start and end with $ are latex. And you can't have several $. So, that invalidates your 3rd and 6th attempts as well.

    3. Math mode

    The other problem is that, then, you are in math mode in LaTeX. And you can't use \\ in math mode (well, you can. But with specific meaning in specific context. Not just to mean "insert line break here".) In math mode ($ or $$) you are supposed to described single atomic equations ($ can, in latex document, be wrapped, but by latex compiler, not by you)

    There are some environment that allows to insert \\. Like align. But you can't start a align since you are supposed to start in in text mode, in place of $$, which, with plotly can't work, because you start any latex already in a $ environment. And can't leave it before the very end

    The closest thing to your attempt could be r"$\alpha \text{\\} \beta$" since \text in latex is a math mode command that put you back temporarily in text mode. But you can't use \\ in it :D

    Solution

    There are many. Just one among many other, you could use a matrix to stack your α and β

    fig.update_layout(xaxis_title=r"$\begin{matrix}\alpha\\ \beta\end{matrix}$")
    

    Works just fine here

    enter image description here