Why am I getting an error when trying to use LaTeX in chart shortcuts

I am trying to show:

enter image description here

... like my 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, k\in \mathbb{N}_{+}\cup\left \{ 0 \right \}$", rotation='horizontal', size='12') 

Sorry, I get an error:

 ValueError: $k=2,\left \langle \left | -k ight |;k ight angle, k\in \mathbb{N}_{+}\cup\left \{ 0 ight \}$ ^ Expected end of text (at char 0), (line:1, col:1) 

Why? I thought I could use LaTeX freely. How do I format text in the figtext() method to achieve the above mathematical sentence? Thank you in advance.

+5
source share
2 answers

This can be fixed with a 1-letter correction:

 py.figtext(0.5, 0.05, r"$k=2,\left \langle \left | -k \right |;k \right \rangle, k\in \mathbb{N}_{+}\cup\left \{ 0 \right \}$", rotation='horizontal', size='12') 

Note r before the string literal. The reason for the error is that several character combinations in your latex string are valid Python escape sequences for things like tabs and newlines. A string literal with the r prefix (for example, r"foo\nbar" ) makes Python interpret the string as a string literal strings i.e. without converting escaped character combinations to special characters.

+7
source

The backslashes in your string are interpreted as Python escape sequences. For example, \r interpreted as a carriage return. Use the raw string by creating the string r"$k=2,\left \langle \left..." .

+3
source

Source: https://habr.com/ru/post/1209028/


All Articles