1

UPDATE: This is not a duplicate of Align TeX equations in matplotlib.

  • The sample code shown in this question does not produce the output I would have expected or wanted given the specified input LaTeX string. When I ran the code shown in this question, there were no errors, but the output was simply the same as the original input string (i.e., it had not been rendered to nice math type).

ORIGINAL QUESTION

I am attempting to use the python package, "matplotlib", to convert LaTeX markup into an image. Per the instructions, I have completed the following:

1) Have a working LaTeX installation on my machine (I'm using TexLive).

2) Have properly set my matplotlib.rcParams such that:

  • text.usetex = True
  • text.latex.preamble = r"\usepackage{amsmath}"

The exact code I'm using to set the params is:

params = {
  "text.usetex": True,
  "text.latex.preamble": [r"\usepackage{amsmath}"], (have tried this part both as a string and as a list as shown here)
  "verbose.level": "debug-annoying"
}
matplotlib.rcParams.update(params)

3) Have verified that my LaTeX snippet is written correctly by running it through the pdflatex tool independently. The sample code is:

$\begin{eqnarray} 3=3 \end{eqnarray}$

and the equivalent string I'm using in Python is:

r"$\begin{eqnarray} 3=3 \end{eqnarray}$"

The problem is that when I try to run the LaTeX fragment above through matplotlib's "to_png" or "math_to_image" functions, I consistently get a Value Error message stating that "\begin" is an unknown symbol. It doesn't seem to matter what's inside the curly braces; matplotlib seems to get hung up on the "\begin" piece in particular.

I'm at a loss for what could be wrong with my configuration given that I have followed all of the matplotlib instructions for using LaTeX as well as other stackoverflow posts reporting similar sounding issues (in most cases, adding \usepackage{amsmath} seems to fix the problem for everyone else).

Hoping to find some leads here. Cheers!

SO1804
  • 54
  • 1
  • 7
  • Quick sanity check, since you haven't included the code snippet - are you properly escaping $\begin{eqnarray} 3=3 \end{eqnarray}$ i.e do you have `r"$\begin{eqnarray} 3=3 \end{eqnarray}$"`or `"$\\begin{eqnarray} 3=3 \\end{eqnarray}$"`? – Probie Apr 26 '18 at 04:14
  • Ah sorry for the omission. Yes, I am using r"$\begin{eqnarray}...". – SO1804 Apr 26 '18 at 04:15
  • Are you sure your rcparams are being loaded properly? Have you tried setting them from inside of python? `from matplotlib import rc` `rc('text', usetex=True)` – Probie Apr 26 '18 at 04:26
  • Yes. I've actually only tried setting the params from inside Python; haven't tried making edits to the config file itself. I've edited the original post to show how I'm setting the parameters. – SO1804 Apr 26 '18 at 04:33
  • The problem is that you wouldn't use `$` for a multiline environment, even not in pure latex. You may also leave out `amsmath`, because `eqnarray` is part of latex itself, although you might consider using `amsmath`'s `align` instead. – ImportanceOfBeingErnest Apr 26 '18 at 10:39
  • $ is required per the matplotlib documentation in order for the tool to detect what needs to be rendered via LaTeX vs. regular text. Also, the use of "eqnarray" in my example was just for illustrative purposes. I get the same error when using \begin{equation} or even the "\text" symbol, which other stackoverflow posts confirm is part of the amsmath package. – SO1804 Apr 26 '18 at 16:14

0 Answers0