Question

I'm rather new at using python and especially numpy, and matplotlib. Running the code below (which works fine without the \frac{}{} part) yields the error:

Normalized Distance in Chamber ($
                             rac{x}{L}$)
                            ^
Expected end of text (at char 32), (line:1, col:33)

The math mode seems to work fine for everything else I've tried (symbols mostly, e.g. $\mu$ works fine and displays µ) so I'm not sure what is happening here. I've looked up other peoples code for examples and they just seem to use \frac{}{} with nothing special and it works fine. I don't know what I'm doing differently. Here is the code. Thanks for the help!

import numpy as np
import math
import matplotlib.pylab as plt

[ ... bunch of calculations ... ]

plt.plot(xspace[:]/L,vals[:,60])
plt.axis([0,1,0,1])
plt.xlabel('Normalized Distance in Chamber ($\frac{x}{L}$)')
plt.savefig('test.eps')

Also, I did look up \f and it seems its an "escape character", but I don't know what that means or why it would be active within TeX mode.

Was it helpful?

Solution

In many languages, backslash-letter is a way to enter otherwise hard-to-type characters. In this case it's a "form feed". Examples:

\n — newline
\r — carriage return
\t — tab character
\b — backspace

To disable that, you either need to escape the backslash itself (backslash-backslash is a backslash)

'Normalized Distance in Chamber ($\\frac{x}{L}$)'

Or use "raw" strings where escape sequences are disabled:

r'Normalized Distance in Chamber ($\frac{x}{L}$)'

This is relevant to Python, not TeX.

Documentation on Python string literals

OTHER TIPS

"\f" is a form-feed character in Python. TeX never sees the backslash because Python interprets the \f in your Python source, before the string is sent to TeX. You can either double the backslash, or make your string a raw string by using r'Normalized Distance ... etc.'.

You have to add an r front of the string to avoid parsing the \f.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top