8000 \times in minor ticklabels not recognized due to \mathdefault · Issue #10029 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

\times in minor ticklabels not recognized due to \mathdefault #10029

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
jmd-dk opened this issue Dec 16, 2017 · 15 comments · Fixed by #23702
Closed

\times in minor ticklabels not recognized due to \mathdefault #10029

jmd-dk opened this issue Dec 16, 2017 · 15 comments · Fixed by #23702

Comments

@jmd-dk
Copy link
jmd-dk commented Dec 16, 2017

Bug report

Bug summary
The '\times' glyph in minor ticklabels of log plots is not being recognized because it is getting wrapped inside '\mathdefault{...}'. The problem and a temporary solution (simply removing '\mathdefault') is fully described on Stack Overflow.

Code for reproduction

import matplotlib
import matplotlib.pyplot as plt

# Customize matplotlib
matplotlib.rcParams.update({# Use mathtext, not LaTeX
                            'text.usetex': False,
                            # Use the Computer modern font
                            'font.family': 'serif',
                            'font.serif': 'cmr10',
                            'mathtext.fontset': 'cm',
                            # Use ASCII minus
                            'axes.unicode_minus': False,
                            })

# Plot
plt.semilogy([-0.03, 0.05], [0.3, 0.05])
plt.savefig('test.png')

Actual outcome

python/lib/python3.6/site-packages/matplotlib/mathtext.py:866: MathTextWarning: Font 'default' does not have a glyph for '\times' [U+d7]
  MathTextWarning)
python/lib/python3.6/site-packages/matplotlib/mathtext.py:867: MathTextWarning: Substituting with a dummy symbol.
  warn("Substituting with a dummy symbol.", MathTextWarning)

Actual test.png

Expected outcome
Expected test.png
This is only a problem with matplotlib 2.x, as 1.x never automatically places minor ticklabels.

Matplotlib version

  • Operating system: Linux
  • Matplotlib version: 2.0.2, but the bug is consistently reproduced in all of matplotlib 2.x
  • Matplotlib backend: Agg, TkAgg
  • Python version: 3.6

I have installed Python and matplotlib from source.

@jmd-dk
Copy link
Author
jmd-dk commented Apr 14, 2021

May I please bump this? More than 3 years down the line, this is still a problem in Matplotlib 3.4.1.

8000

@jklymak
Copy link
Member
jklymak commented Apr 14, 2021

Seems reasonable. Did you have a fix?

@anntzer
Copy link
Contributor
anntzer commented Apr 14, 2021

My guess is that the solution would be similar to #17007 (comment) / #18397.

@jmd-dk
Copy link
Author
jmd-dk commented Apr 14, 2021

@jklymak Not an actual one. I have an ugly workaround, as demonstrated on StackOverflow (see link above). Reading my answer there, the underlying problem should be apparent. I think it won't be hard to fix for one with knowledge of the matpltolib.mathtext internals.

@anntzer
Copy link
Contributor
anntzer commented Apr 14, 2021

@aitikgupta Perhaps this and the issues linked in my previous message may be of interest for you.

@aitikgupta
Copy link
Contributor
aitikgupta commented Apr 15, 2021

A simple fix would be what @anntzer mentioned, i.e. similar to #18397

A long term fix would be define fallback for mathtext such that the process of finding symbols:

where symbols (e.g. minus) and letters/digits are in different font files (cmr10/cmsy10);

would be automated. I can take this up during the GSoC period once the fallback for non-mathtext is implemented, for now I guess it'd be better to go with the simple fix.

Or an even longer term fix would again be what @anntzer mentioned in #17007 (comment):

Long term I would like to get rid of the numerous cmXX10 fonts and replace them by the monolithic Latin Modern Math font, which should avoid all these problems.

@tfpf
Copy link
Contributor
tfpf commented Aug 15, 2022

This is still not fixed on the current main branch. Is there any progress on the long-term fix?

@oscargus
Copy link
Member

It is since a few days possible to use font fallback, i.e., when a glyph is not found in the requested font, the next font in the list is tried.

I guess that there should be a solution using this, but based on my tests (and not really knowing much about the currently introduced change), it may be that this is not implemented for mathtext.

@tfpf
Copy link
Contributor
tfpf commented Aug 16, 2022

Is it worth considering the short-term fix, then? Since this bug is quite old.

@anntzer
Copy link
Contributor
anntzer commented Aug 18, 2022

The following basically implements #10029 (comment), I think:

diff --git i/lib/matplotlib/_mathtext.py w/lib/matplotlib/_mathtext.py
index 0aa7bec574..53c2eac216 100644
--- i/lib/matplotlib/_mathtext.py
+++ w/lib/matplotlib/_mathtext.py
@@ -462,11 +462,15 @@ class UnicodeFonts(TruetypeFonts):
             found_symbol = False
             font = self._get_font(new_fontname)
             if font is not None:
-                if font.family_name == "cmr10" and uniindex == 0x2212:
-                    # minus sign exists in cmsy10 (not cmr10)
-                    font = get_font(
-                        cbook._get_data_path("fonts/ttf/cmsy10.ttf"))
-                    uniindex = 0xa1
+                if font.family_name == "cmr10":
+                    if uniindex == 0xd7:  # times sign from cmsy10, not cmr10
+                        font = get_font(
+                            cbook._get_data_path("fonts/ttf/cmsy10.ttf"))
+                        uniindex = 0xa3
+                    if uniindex == 0x2212:  # minus sign from cmsy10, not cmr10
+                        font = get_font(
+                            cbook._get_data_path("fonts/ttf/cmsy10.ttf"))
+                        uniindex = 0xa1
                 glyphindex = font.get_char_index(uniindex)
                 if glyphindex != 0:
                     found_symbol = True

#20235 should also be reverted with that patch.

@oscargus
Copy link
Member

Discussing this at the dev call today, it seems like implementing font fallback for mathtext is not really feasible. So something like the patch above is the way to go. Are you interested in picking this up @tfpf ?

@tfpf
Copy link
Contributor
tfpf commented Aug 20, 2022

Sure, will do.

#20235 should also be reverted with that patch.

If I understand correctly, this is necessary because font fallback in regular text was implemented by #20740? (I think that should be a separate pull request, though?)

@anntzer
Copy link
Contributor
anntzer commented Aug 20, 2022

This is unrelated to regular font fallback. mathtext has "always" supported font fallback using its own code (because it was necessary from the very beginning), but a large amount of complexity is that the font files we use for math have irregular encodings that basically need to be hardcoded.

@tfpf
Copy link
Contributor
tfpf commented Aug 21, 2022

It is still not completely clear to me why it should be reverted.:sweat_smile: If minus signs are not present in that font, and are being substituted in as a result of Mathtext's fallback, it looks like that warning is required?

import matplotlib.pyplot as plt
plt.rcdefaults()
plt.rc('font', family=['cmr10'])
plt.rc('mathtext', fontset='cm')
plt.rc('axes.formatter', use_mathtext=False)
plt.semilogy([-0.03, 0.05], [0.3, 0.05])
plt.show()

This throws

UserWarning: cmr10 font should ideally be used with mathtext, set axes.formatter.use_mathtext to True
UserWarning: Glyph 8722 (\N{MINUS SIGN}) missing from current font.

and fails to render minus signs on the horizontal axis. (Setting use_mathtext to True enables the Mathtext fallback for minus signs; setting family to ['cmr10', 'STIXGeneral'] triggers regular font fallback. Either of these two actions results in the minus signs being rendered correctly.)

So I believe the warning is genuine and should not be removed?

@anntzer
Copy link
Contributor
anntzer commented Aug 21, 2022

I guess it's no big deal either way, the warning can also be left until we actually switch to a full latin modern font with all glyphs in a single font file.

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.

8 participants
0