Thanks to visit codestin.com
Credit goes to github.com

Skip to content

latex textrm does not work in Cairo backend #17932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
esheldon opened this issue Jul 15, 2020 · 7 comments · Fixed by #17948
Closed

latex textrm does not work in Cairo backend #17932

esheldon opened this issue Jul 15, 2020 · 7 comments · Fixed by #17948

Comments

@esheldon
Copy link

Bug report

Bug summary

Use of \textrm latex in Cairo backend raises an exception

Code for reproduction

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.use('Cairo')
mpl.rc('text', usetex=True)

fig, ax = plt.subplots()

ax.set_xlabel(r'$x [\textrm{cm}]$')
ax.scatter([1, 2], [1, 2], marker='o')
fig.savefig('test-cairo.png')

Raises an exception

ValueError:
x [\textrm{cm}]
^ 
Unknown symbol: \textrm, found '\'  (at char 3), (line:1, col:4) 

Expected outcome

The textrm should work to produce rm text. For reference, this works fine in the Agg backend.

Matplotlib version

  • Operating system: linux ubuntu
  • Matplotlib version: 3.2.2
  • Matplotlib backend: Cairo
  • Python version: 3.7.6

Installed with conda

@anntzer
Copy link
Contributor

anntzer commented Jul 16, 2020

The cairo backend does not support usetex at all; at least as a quick improvement one could at least log a warning, e.g.

diff --git i/lib/matplotlib/backends/backend_cairo.py w/lib/matplotlib/backends/backend_cairo.py
index 773886e56..e4fa01b6c 100644
--- i/lib/matplotlib/backends/backend_cairo.py
+++ w/lib/matplotlib/backends/backend_cairo.py
@@ -7,6 +7,7 @@ This backend depends on cairocffi or pycairo.
 """
 
 import gzip
+import logging
 import math
 
 import numpy as np
@@ -35,6 +36,7 @@ from matplotlib.transforms import Affine2D
 
 
 backend_version = cairo.version
+_log = logging.getLogger(__name__)
 
 
 if cairo.__name__ == "cairocffi":
@@ -236,14 +238,15 @@ class RendererCairo(RendererBase):
 
         # Note: (x, y) are device/display coords, not user-coords, unlike other
         # draw_* methods
+        if ismath == "TeX":
+            _log.warning("The cairo backend does not support usetex and this "
+                         "setting will be ignored.")
         if ismath:
             self._draw_mathtext(gc, x, y, s, prop, angle)
-
         else:
             ctx = gc.ctx
             ctx.new_path()
             ctx.move_to(x, y)
-
             ctx.select_font_face(*_cairo_font_args_from_font_prop(prop))
             ctx.save()
             ctx.set_font_size(prop.get_size_in_points() * self.dpi / 72)
@@ -286,6 +289,9 @@ class RendererCairo(RendererBase):
     def get_text_width_height_descent(self, s, prop, ismath):
         # docstring inherited
 
+        if ismath == "TeX":
+            _log.warning("The cairo backend does not support usetex and this "
+                         "setting will be ignored.")
         if ismath:
             width, height, descent, fonts, used_characters = \
                 self.mathtext_parser.parse(s, self.dpi, prop)

@esheldon
Copy link
Author

I use the Cairo backend with tex. This figure was generated using it, and all text was run through latex

test

@esheldon
Copy link
Author

Please do not remove the ability to use tex with cairo, or add any additional warnings

@anntzer
Copy link
Contributor

anntzer commented Jul 16, 2020

Why, exactly, do you want to use the cairo backend for this purpose?
However you are right, things actually work; it's just a matter of falling back to the base class implementation:

diff --git i/lib/matplotlib/backends/backend_cairo.py w/lib/matplotlib/backends/backend_cairo.py
index 773886e56..244f7103d 100644
--- i/lib/matplotlib/backends/backend_cairo.py
+++ w/lib/matplotlib/backends/backend_cairo.py
@@ -286,6 +286,8 @@ class RendererCairo(RendererBase):
     def get_text_width_height_descent(self, s, prop, ismath):
         # docstring inherited
 
+        if ismath == "TeX":
+            return super().get_text_width_height_descent(s, prop, ismath)
         if ismath:
             width, height, descent, fonts, used_characters = \
                 self.mathtext_parser.parse(s, self.dpi, prop)

@esheldon
Copy link
Author

In my work I have only ever seen the \textrm command fail with Cairo, nothing else. E.g. substituting \mathrm for \textrm works fine, it is something particular to that latex command.

@jklymak
Copy link
Member

jklymak commented Jul 16, 2020

Also have you tried mplcairo? It's not part of core matplotlib yet because it's a bit harder to build but it seems more robust and is actively maintained.

@esheldon
Copy link
Author

Cairo works fine except for this one issue, and has the advantage over Agg which, for example, has alignment problems #3400

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants