Skip to content
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

[ENH]: Make the font family "Computer Modern" more accessible #28172

Open
NicoJG opened this issue May 5, 2024 · 1 comment
Open

[ENH]: Make the font family "Computer Modern" more accessible #28172

NicoJG opened this issue May 5, 2024 · 1 comment

Comments

@NicoJG
Copy link

NicoJG commented May 5, 2024

Problem

The font family "Computer Modern" is commonly used in LaTeX documents.
Even though, in Matplotlib, it is easy to use "Computer Modern Math" for math-text through plt.rcParams['mathtext.fontstyle'] = 'cm', it is unnecessarily complicated to use Computer Modern for non-math-text.
Despite it being listed as the 3rd font in the default matplotlibrc file under font.serif, font.sans-serif and font.monospace.

One option is compiling the plot through an actual TeX engine, i.e. using plt.rcParams['text.usetex'] = True.
However, many people do not have LaTeX installed locally (because of Overleaf) and it takes up a lot of space.
Additionally, it is way slower than native Matplotlib rendering and thus a bad option if you want to generate a lot of plots.

Another suggestion I have found online is to install the font family in your operating system and use plt.rcParams['font.serif'] = 'Computer Modern Roman'.
I have found this to not be as straightforward to do because you have to find a good download, then there are so many different files to choose from, and the font might still be not found by Matplotlib after installing it.

After digging through Google and the matplotlib directories, the easiest way to use Computer Modern, in my opinion, are the following rc-parameter settings:

plt.rcParams['font.family'] = 'serif' # or 'sans-serif' or 'monospace'
plt.rcParams['font.serif'] = 'cmr10'
plt.rcParams['font.sans-serif'] = 'cmss10'
plt.rcParams['font.monospace'] = 'cmtt10'
plt.rcParams["axes.formatter.use_mathtext"] = True # to fix the minus signs

Furthermore, in my use case, I want to also be able to edit the plots afterwards in Inkscape, so I installed the TTF font files in my operating system and I use

plt.rcParams["pdf.fonttype"] = 42
plt.rcParams["svg.fonttype"] = 'none'

However, every time I save a PDF, I get the annoying fonttools warnings
'modified' ... and 'created' timestamp seems very low; regarding as unix timestamp.
This is caused by the TTF files having the wrong date Fri Jan 01 1904 01:00:00 GMT+0100 in their "Font Header Table" metadata. (I used fontdrop.info)

Proposed solution

Since Computer Modern is already available to use in Matplotlib, why doesn't

plt.rcParams['font.serif'] = 'Computer Modern Roman'
plt.rcParams['font.sans-serif'] = 'Computer Modern Sans Serif'
plt.rcParams['font.monospace'] = 'Computer Modern Typewriter'

just work out of the box, as the default matplotlibrc file suggests?
Maybe there is a way to make Matplotlib recognise the already available font files (cmr10 cmss10 cmtt10) under those names?
Alternatively, if it is impossible to make those names available out of the box, it might be good to list those font names somewhere in the documentation or the matplotlibrc.

I don't know enough about fonts to suggest a way to fix the Unicode minus sign, but the cmr10 minus sign warning already helps to find this workaround and something similar could be implemented if a font named "Computer Modern" is not found by Matplotlib.

Might it be possible to update the Computer Modern TTF files, so that the mentioned fonttools warning does not appear anymore? Those files were apparently added 18 years ago and never touched again. :D

Finally, a lot of people prefer to use the font family Latin Modern (through the lmodern CTAN package), which is just an improved version of Computer Modern and it also has a maths font.
Do you think it might be a good idea to maybe switch from Computer Modern to Latin Modern in Matplotlib?

@anntzer
Copy link
Contributor

anntzer commented May 5, 2024

The short answer is that the computer modern fonts shipped with Matplotlib use a really weird encoding which basically means support for figuring out where in the font file each glyph must be found is basically hard-coded for them (and therefore mostly only works for mathtext). I would say there's little point in trying to fix these, and it may be better to just switch to latin modern throughout (see the various older related issues: #5568, #15338 (comment), #26240 and I'm pretty sure there's a couple others).

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

No branches or pull requests

2 participants