Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex on python: \alpha and \beta don't work?

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.

like image 698
Jorge Leitao Avatar asked Oct 20 '11 13:10

Jorge Leitao


2 Answers

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 $'
like image 143
NPE Avatar answered Oct 24 '22 20:10

NPE


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.

like image 21
abergou Avatar answered Oct 24 '22 20:10

abergou