johannesjmeyer / rsmf

Right-size my figures
174 stars 9 forks source link

mathtex ValueError making rsmf+matplotlib plots #20

Closed drtobybrown closed 3 years ago

drtobybrown commented 3 years ago

MWE:

import rsmf
from matplotlib import pyplot as plt
from astropy.io import fits
from astropy.wcs import WCS
from astropy.utils.data import get_pkg_data_filename

formatter = rsmf.setup(r"\documentclass[a4paper,12pt,noarxiv]{revtex4-1}")
fig = formatter.figure()

filename = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')

hdu = fits.open(filename)[0]
wcs = WCS(hdu.header)

fig.add_subplot(111, projection=wcs)
plt.imshow(hdu.data, origin='lower', cmap=plt.cm.viridis)
plt.xlabel('RA')
plt.ylabel('Dec')

throws the following error.

ValueError: Error processing '5\(\displaystyle \mathregular{^h}\)41\(\displaystyle \mathregular{^m}\)30\(\displaystyle \mathregular{^s}\)'
LaTeX Output:
! Undefined control sequence.
<argument> ...tfont 5\(\displaystyle \mathregular 
                                                  {^h}\)41\(\displaystyle \m...
<*> ...{^m}\)30\(\displaystyle \mathregular{^s}\)}

!  ==> Fatal error occurred, no output PDF file produced!

I think this is because mathregular is a matplotlib command rather than a LaTeX command.

Setting plt.rcParams['text.usetex']=True fixes the issue for me but shouldn't rsmf do this by default?

johannesjmeyer commented 3 years ago

Thanks @drtobybrown for pointing that out to me. I will make a PR that enables text.usetex across rsmf. I actually was under the wrong impression that I would have to set it to False because usetex essentially is superfluous in the pgf backend because tex is used anyways.

johannesjmeyer commented 3 years ago

@drtobybrown could you try to install rsmf again from the branch add_usetex and re-run the code?

pip install git+https://www.github.com/johannesjmeyer/rsmf@add_usetex --upgrade
drtobybrown commented 3 years ago

That works, thanks!