Why I get error while trying to use LaTeX in plots' label
Question:
I try to show:
…as my plots’ x axis label. For this purpose I use the pylab.figtext()
function, i.e:
py.figtext(0.5, 0.05, "$k=2,left langle left | -k right |;k right rangle,
kin mathbb{N}_{+}cupleft { 0 right }$", rotation='horizontal', size='12')
Unfortunately, I get error:
ValueError:
$k=2,left langle left | -k
ight |;k
ight
angle, kin mathbb{N}_{+}cupleft { 0
ight }$
^
Expected end of text (at char 0), (line:1, col:1)
Why is that? I thought, that I can use LaTeX freely. How should I format my text in figtext()
method in order to achieve the aforementioned mathematical sentence? Thank you in advance.
Answers:
The backslashes in your string are interpreted as Python string escapes. For instance r
is interpreted as a carriage return. Use a raw string by making your string r"$k=2,left langle left..."
.
This can be fixed by a 1 letter correction:
py.figtext(0.5, 0.05, r"$k=2,left langle left | -k right |;k right rangle,
kin mathbb{N}_{+}cupleft { 0 right }$", rotation='horizontal', size='12')
Note the r before the string literal. The cause of the error is that several of the character combinations in your latex string are valid Python escape sequences for such things as tabs and new-lines. A string literal prefixed with an r (e.g. r"foonbar"
) makes Python interpret the string as a raw string literal, i.e. without converting the escaped character combinations to special characters.
I had a similar problem when working with one of the examples on this page and r before my string literal didn’t help. I escaped $ signs and it worked.
r"Standardized [$$N (mu=0, ; sigma=1)$$]",
Yes, placing an ‘r’ at the front works.
Check this line : plt.xlabel(r'$rho$'); plt.ylabel('$sigma_{yy}$(MPa)');
without ‘r’ and with ‘r’
I try to show:
…as my plots’ x axis label. For this purpose I use the pylab.figtext()
function, i.e:
py.figtext(0.5, 0.05, "$k=2,left langle left | -k right |;k right rangle,
kin mathbb{N}_{+}cupleft { 0 right }$", rotation='horizontal', size='12')
Unfortunately, I get error:
ValueError:
$k=2,left langle left | -k
ight |;k
ight
angle, kin mathbb{N}_{+}cupleft { 0
ight }$
^
Expected end of text (at char 0), (line:1, col:1)
Why is that? I thought, that I can use LaTeX freely. How should I format my text in figtext()
method in order to achieve the aforementioned mathematical sentence? Thank you in advance.
The backslashes in your string are interpreted as Python string escapes. For instance r
is interpreted as a carriage return. Use a raw string by making your string r"$k=2,left langle left..."
.
This can be fixed by a 1 letter correction:
py.figtext(0.5, 0.05, r"$k=2,left langle left | -k right |;k right rangle,
kin mathbb{N}_{+}cupleft { 0 right }$", rotation='horizontal', size='12')
Note the r before the string literal. The cause of the error is that several of the character combinations in your latex string are valid Python escape sequences for such things as tabs and new-lines. A string literal prefixed with an r (e.g. r"foonbar"
) makes Python interpret the string as a raw string literal, i.e. without converting the escaped character combinations to special characters.
I had a similar problem when working with one of the examples on this page and r before my string literal didn’t help. I escaped $ signs and it worked.
r"Standardized [$$N (mu=0, ; sigma=1)$$]",
Yes, placing an ‘r’ at the front works.
Check this line : plt.xlabel(r'$rho$'); plt.ylabel('$sigma_{yy}$(MPa)');
without ‘r’ and with ‘r’