29

I'm using matplotlib to produce some graphics, and I'm using latex for the legends.

More specifically, I'm trying to use something like this:

loglog(x,x, '-r',label='$ \alpha $')
legend()
show()

However, this code does not present a legend on the figure, and gets error after I close the image.

I'm using the enthought package (for mac), but the error comes from the pylab/scipy.

The error the appears is exactly:

$ lpha $ (at char 0), (line:1, col:1)

However, if use the \mu or \gamma, it works well!! I only found about this problem on \beta and \alpha.

Does anyone knows what this can be? I believe python is interpreting "\a" as some character... but I don't know how should I debug / avoid it.

Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121

3 Answers3

60

The issue is that \a and \b have special meaning inside a Python string literal.

I recommend that you use raw strings whenever there is a backslash embedded inside a string:

r'$ \alpha $'
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 2
    For those trying to reproduce the answer, there is an (obvious) typo maybe worth mentioning: `r'$ \alpha $'` (the closing quote is missing). – bela83 May 06 '19 at 09:28
20

In addition to using raw strings (as mentioned in the post above) you can also escape the backslash. So typing \\alpha and \\beta will also work.

abergou
  • 441
  • 2
  • 7
0

Hi if your \alpha and \beta don't work on Latex

use $\alpha$
likewise $\beta$ so on.

hope it works