'Matplotlib in Rmarkdown/RStudio fails when calling LaTeX on `\$` with Anaconda

Problem description

I am having to use Anaconda on Windows, and am trying to write an RMarkdown document, knitted into a pdf, where within the RMarkdown I am using some Python snippets. However, when I try make matplotlib use LaTeX (with the rc.params) I find it does not render but hits an error I cannot understand nor fix. The offending lines are

mpl.rcParams.update({"text.usetex": True})
...
plt.title(r'Some Latex with symbol \$')

It is LaTeX trying to interpret the \$ which is throwing issues. As far as I can tell this should be correctly escaped. If I remove the \$ everything works as expected, (or if I replace it with e.g. $e=mc^2$).

The error message

Quitting from lines 31-34 (example.Rmd) 
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  RuntimeError: Evaluation error: KeyError: b'tcrm1200'

Detailed traceback: 
  File "C:\Users\Harry\ANACON~1\lib\site-packages\matplotlib\pyplot.py", line 722, in savefig
    res = fig.savefig(*args, **kwargs)
  File "C:\Users\Harry\ANACON~1\lib\site-packages\matplotlib\figure.py", line 2180, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "C:\Users\Harry\ANACON~1\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 88, in print_figure
    super().print_figure(*args, **kwargs)
  File "C:\Users\Harry\ANACON~1\lib\site-packages\matplotlib\backend_bases.py", line 2082, in print_figure
    **kwargs)
  File "C:\Users\Harry\ANACON~1\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2503, in print_pdf
    self.figure.draw(renderer)
  File "C:\Users\Harry\ANACON~1\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\Harry\ANACON~1\lib\site-packages\matplotlib\figure.py"
Calls: <Anonymous> ... py_capture_output -> force -> <Anonymous> -> py_call_impl
Execution halted

MWE

The following is a .Rmd file running on Rstudio 1.2.5001 (should be using Python 3.7 with Conda3, but I'm not so sure how to dig out the specifics on Windows...).

---
output: pdf_document
---


```{r}
library(reticulate)
```

```{python, echo=FALSE, include=FALSE}
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'C:\Users\Harry\Anaconda3\Library\plugins\platforms'
```

```{python, echo=FALSE, include=FALSE}
import matplotlib as mpl
import matplotlib.pyplot as plt
# Setting some default plotting features to give nicer plots. This can be commented out for beginners. 
rc_fonts = {
    "text.usetex": True,
    'text.latex.preview': True, # Gives correct legend alignment.
    'mathtext.default': 'regular',
    'figure.figsize': (6, 4),
    "font.family": "serif",
    "font.serif": "computer modern roman",
}
mpl.rcParams.update(rc_fonts)
```

```{python}
plt.plot([0, 2, 1, 4])
plt.title(r'Some Latex with symbol \$')
plt.show()
```


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source