Ask Your Question

latex code of plots

asked 2022-11-24 19:57:05 +0100

moon gravatar image

Dear, I'm trying to get the LaTeX code of a given plot. While it works fine in cocalc, I'm not able to get it done in my sageMath environment. It looks like I'm missing some libraries.

print(latex(s)) #checkpoint
with open("tmp/demo.tex", 'w') as f:
    f.write(str(latex(s))) #ultimate target

Here is the error generated: ValueError: Matplotlib requires either xelatex, lualatex, or pdflatex.

Any idea ?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2022-11-24 22:05:53 +0100

Emmanuel Charpentier gravatar image

updated 2022-11-25 19:09:35 +0100

The error message tels you quite clearly that you need a local LaTeX installation.

Try installing one locally (on Linux and Windows, TeXLive seems to be the most easly installable and the most up to date distribution...).

The tutorial has some (somewhat outdated IMHO)advice for this.

Why you are at it, drop a look to SageTeX, a nice system allowing the creation of "active LaTeX documents" allowing to ensure consistency of text and Sage code.

EDIT : The beginning of latex(s) is a helpful command stating :

%% Creator: Matplotlib, PGF backend
%% To include the figure in your LaTeX document, write
%%   \input{<filename>.pgf}
%% Make sure the required packages are loaded in your preamble
%%   \usepackage{pgf}
%% Also ensure that all the required font packages are loaded; for instance,
%% the lmodern package is sometimes necessary when using math font.
%%   \usepackage{lmodern}
%% Figures using additional raster images can only be included by \input if
%% they are in the same directory as the main LaTeX file. For loading figures
%% from other directories you can use the `import` package
%%   \usepackage{import}
%% and then include the figures with
%%   \import{<path to file>}{<filename>.pgf}
%% Matplotlib used the following preamble
%%   \usepackage{fontspec}
%%   \setmainfont{DejaVuSerif.ttf}[Path=\detokenize{/usr/local/sage-9/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/matplotlib/mpl-data/fonts/ttf/}]
%%   \setsansfont{DejaVuSans.ttf}[Path=\detokenize{/usr/local/sage-9/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/matplotlib/mpl-data/fonts/ttf/}]
%%   \setmonofont{DejaVuSansMono.ttf}[Path=\detokenize{/usr/local/sage-9/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/matplotlib/mpl-data/fonts/ttf/}]

Therefore, not only you must have {pdf|xe|lua}latex somewhere in your path, you must also have the pgf LaTeX package (and its dependencies) installed in your LaTeX installation.

FWIW, works like clockwork for me on 9.8.beta3 running on Debian testing where the Debian package texlive-full is installed (this is wildly overshoot for Sage's needs, but I have other uses for LaTeX...).


edit flag offensive delete link more


Thanks for this answer. As you can imagine I have laTeX installed and running. pdflatex run also in the shell command line.

moon gravatar imagemoon ( 2022-11-24 22:51:35 +0100 )edit

Is either of pdflatex, xelatex, lualatex in your PATH ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2022-11-25 09:24:14 +0100 )edit

got pdflatex as I'm a regular LaTeX user (actually I use only this env). TeXshop works fine. However for this specific need I have to dump the file that will include the TeX commands (my plots) into overleaf. pdflatex is in /usr/local/bin same goes for xelatex. no trace of lulatex.

moon gravatar imagemoon ( 2022-11-26 10:54:21 +0100 )edit

Got an idea. Lauched sage in command line and got this link text (it doesn't appear in jupyter) if one can't access here is what it says:

│ SageMath version 9.7, Release Date: 2022-09-19                     │
│ Using Python 3.10.5. Type "help()" for help.                       │
  Input In [1]
SyntaxError: invalid syntax

I feel like I made a little progress but can't do much I'm afraid. and moreover why is that a Syntaxe Error. This is the way I would have wrote it.

moon gravatar imagemoon ( 2022-11-26 17:33:59 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2022-11-24 19:57:05 +0100

Seen: 197 times

Last updated: Nov 25 '22