Disclaimer: I fully understand that the use of text.latex.preamble
in rcparams is not officially supported.
Dear hive mind,
I need/want to use the tikz
LaTeX package to draw symbols in matplotlib plt.text()
calls. It all works smoothly (in the sense that I get no LaTeX/matplotlib errors) … except that no symbols are actually drawn ?!
Here is a MWE:
from matplotlib import pyplot as plt
plt.style.use('./latex.mplstyle')
plt.close(1)
plt.figure(1, figsize=(4, 3))
plt.text(0.5, 0.7, r'This is regular text', ha='center', va='center')
plt.text(0.5, 0.5, r'$\rightarrow$ \begin{tikzpicture}\draw [thick] (0,0) circle [radius=2ex];\end{tikzpicture}', ha='center', va='center')
plt.savefig('test.png')
plt.show()
The content of latex.mplstyle
is:
text.usetex: True
text.latex.preamble: \usepackage{tikz}
The output looks like that:
Problem: there should be a circle to the right of the arrow.
And see how the arrow is neatly centered ? This suggests that the tikzpicture
is simply not being generated.
So what could matplotlib be doing differently compared to pdflatex
that returns the following image:
Screenshot 2021-08-13 at 09.44.13|218x208
As a new user, it appears that I cannot embed two pictures in this post. This image shows an arrow and a circle, like so: ->O)
when processing the following (same) LaTeX code:
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
$\rightarrow$ \begin{tikzpicture}\draw [thick] (0,0) circle [radius=2ex];\end{tikzpicture}
\end{document}
Can anyone think of something obvious ? The tikz
package is definitely being loaded correctly, as the \tikzpicture
command does not raise any error when compiling the matplotlib figure. So is it a matter of some multiple latex compilations missing for matplotlib ?
Any suggestion will be greatly appreciated !
Side-note 1: as far as I can tell, the same bug behavior implies that symbols from the tikzsymbols
LaTeX package do not appear either in plt.text()
calls.
Side-note 2: I evidently need to draw more (but not THAT much more) than a circle with tikz
… That’s just the MWE!