Fix error in latex (pgf) generation of Graphics objects due to spec changes upstream (matplotlib)#41262
Merged
vbraun merged 1 commit intosagemath:developfrom Dec 21, 2025
Merged
Conversation
Contributor
Author
|
Another slightly off-topic comment: For the preamble, as current latex versions defaults to utf8 encoding, it may be a good idea to revise the current preset preamble, as it may lead to problems in some cases. But I think that this is not urgent at all, as I am not sure about how widely spread this utf8 support currently is. |
vbraun
pushed a commit
to vbraun/sage
that referenced
this pull request
Dec 20, 2025
sagemathgh-41262: Fix error in latex (pgf) generation of Graphics objects due to spec changes upstream (matplotlib) This pull request fixes a problem of latex generation of ``Graphics`` objects due to upstream spec change. Symptom: When trying to generate latex for a ``Graphics`` object, even simple, sage produces the following error. ``` sage: latex(line([(0, 0), (1, 1)])) ------------------------------------------------------------------------ --- ValueError Traceback (most recent call last) Cell In[2], line 1 ----> 1 latex(line([(Integer(0), Integer(0)), (Integer(1), Integer(1))])) File ~/svn/sage/src/sage/misc/latex.py:922, in LatexCall.__call__(self, x, combine_all) 893 r""" 894 Return a :class:`LatexExpr` built out of the argument ``x``. 895 (...) 919 x 2 920 """ 921 if has_latex_attr(x): --> 922 return LatexExpr(x._latex_()) 923 try: 924 f = latex_table[type(x)] File ~/svn/sage/src/sage/plot/graphics.py:3440, in Graphics._latex_(self, **kwds) 3424 """ 3425 Return a string plotting ``self`` with PGF. 3426 (...) 3437 '%% Creator: Matplotlib, PGF backend... 3438 """ 3439 tmpfilename = tmp_filename(ext='.pgf') -> 3440 self.save(filename=tmpfilename, **kwds) 3441 with open(tmpfilename) as tmpfile: 3442 latex_list = tmpfile.readlines() File ~/svn/sage/src/sage/misc/decorators.py:416, in suboptions.__call__.<locals>.wrapper(*args, **kwds) 412 del kwds[key] 414 kwds[self.name + "options"] = suboptions --> 416 return func(*args, **kwds) File ~/svn/sage/src/sage/plot/graphics.py:3394, in Graphics.save(self, filename, **kwds) 3390 pgf_options = { 3391 "pgf.texsystem": latex_implementations[0], 3392 } 3393 from matplotlib import rcParams -> 3394 rcParams.update(pgf_options) 3395 from matplotlib.backends.backend_pgf import FigureCanvasPgf 3396 figure.set_canvas(FigureCanvasPgf(figure)) File <frozen _collections_abc>:982, in update(self, other, **kwds) File ~/svn/sage/local/var/lib/sage/venv- python3.12.5/lib/python3.12/site-packages/matplotlib/__init__.py:774, in RcParams.__setitem__(self, key, val) 772 cval = self.validate[key](val) 773 except ValueError as ve: --> 774 raise ValueError(f"Key {key}: {ve}") from None 775 self._set(key, cval) 776 except KeyError as err: ValueError: Key pgf.preamble: Could not convert ['\\usepackage[utf8x]{inputenc}', '\\usepackage[T1]{fontenc}'] to str ``` This is due to a change of spec in matplotlib. More precisely, the option ``pgf.preamble`` accepts no longer a list of strings, but only a string. This can be seen also in the [updated tutorial](https://matplotlib.org/stable/users/explain/text/pgf.html). The current pull request resolves this problem. After applying the changes, the previous command produces some pgf output that can be used as a latex presentation of the ``Graphics`` object. I am not sure about how to add test for this change. There are already some tests in the ``_latex_`` function, but somehow it is not executed. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. URL: sagemath#41262 Reported by: fwjmath Reviewer(s):
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request fixes a problem of latex generation of
Graphicsobjects due to upstream spec change.Symptom: When trying to generate latex for a
Graphicsobject, even simple, sage produces the following error.This is due to a change of spec in matplotlib. More precisely, the option
pgf.preambleaccepts no longer a list of strings, but only a string. This can be seen also in the updated tutorial.The current pull request resolves this problem. After applying the changes, the previous command produces some pgf output that can be used as a latex presentation of the
Graphicsobject.I am not sure about how to add test for this change. There are already some tests in the
_latex_function, but somehow it is not executed.📝 Checklist