Ask Your Question

Revision history [back]

First of all, you should enter all strings containing some LaTeX code with the prefix r (for "raw" string), so that the backslash character is correctly interpreted as part of a LaTeX command. For instance, you should write

plot(..., axes_labels=[r'$\si{\volt}$', r'$\si{\second}$'])

Another option would be to double all backslashes:

plot(..., axes_labels=['$\\si{\\volt}$', '$\\si{\\second}$'])

Unfortunately, the import of the siunitx package does not work in SageMath 8.3. This bug has been fixed in the ticket #25799. Since this ticket has been merged in SageMath 8.4.beta0, everything will be OK with SageMath 8.4. I've just checked that with SageMath 8.4.beta4, after having installed the system (Ubuntu in my case) packages texlive-science (which provides siunitx) and dvipng (required by Matplotlib to process LaTeX texts) the following works:

sage: from matplotlib import rc
sage: rc('text', usetex=True)
sage: rc('text.latex', preamble=r'\usepackage{siunitx}')
sage: plot(sin(x), (x, 0, 4), axes_labels=[r'$\si{\volt}$', r'$\si{\second}$'])  # output is OK
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: text(r"\SI{1}{\ampere}", (2, 2))  # output is OK
Launched png viewer for Graphics object consisting of 1 graphics primitive

If you don't want to wait for the release of SageMath 8.4 (scheduled for October), the solution would be to use the development version of SageMath via

git clone https://github.com/sagemath/sage.git 
cd sage
git checkout develop
git pull
MAKE="make -j8" make

(see here for more details).