1

I want to use latex in the legend of some plot made using python

I used the following:

import math
import numpy as np
import matplotlib.pyplot as plt
import time
plt.rcParams['text.usetex'] = True

I run the code but I got the message:

Failed to process string with tex because latex could not be found

but I have latex installed on my Mac. In the terminal I checked by typing

which latex 

and I got

/Library/TeX/texbin/latex

I'm using "Python 3.8"

Thank you for any help.

A. PI
  • 111
  • 1
  • 3
  • Can you check if the latex directory is in your `PATH` variable? That's the only requirement from the [docs](https://matplotlib.org/stable/tutorials/text/usetex.html) you may have missed. – shriakhilc Jan 05 '22 at 14:22
  • This might be trivial, but how can I check? – A. PI Jan 05 '22 at 14:24
  • You can run `echo $PATH` in a terminal, and see if it contains `/Library/TeX/texbin/latex` (your result from `which latex`) in it. If it doesn't, you can look for Mac specific instructions to add this to the path – shriakhilc Jan 05 '22 at 14:26
  • This is what I got: /opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin – A. PI Jan 05 '22 at 14:28
  • `/Library/TeX/texbin` is in the PATH, so I guess this is not the issue. – shriakhilc Jan 05 '22 at 14:29
  • Try this answer: https://stackoverflow.com/a/6082265/6698642 . It seems Python uses a different path than $PATH. You'd be appending `/Library/TeX/texbin` rather than `/usr/texbin` though. – shriakhilc Jan 05 '22 at 14:32
  • `/Library/TeX/texbin/latex` is usually just a link, check that it points to the correct target. – samcarter_is_at_topanswers.xyz Jan 05 '22 at 14:37

1 Answers1

0

Same problem was closed here Possible solution: The path issue does not arise when running python from the command line, nor if opening spyder from the command line. It arises only when running spyder from anaconda.

It works in my case

A. PI
  • 111
  • 1
  • 3