Skip to content

[MNT]: Use fix-cm rather than type1cm for LaTeX #27654

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

Open
jakelevi1996 opened this issue Jan 15, 2024 · 6 comments · May be fixed by #30100
Open

[MNT]: Use fix-cm rather than type1cm for LaTeX #27654

jakelevi1996 opened this issue Jan 15, 2024 · 6 comments · May be fixed by #30100

Comments

@jakelevi1996
Copy link

jakelevi1996 commented Jan 15, 2024

Summary

When I try use LaTeX with matplotlib (see script below), I get an error containing ! LaTeX Error: File type1cm.sty not found. On the site for type1cm, it says "Note that the LATEX distribution now contains a package fix-cm, which performs the task of type1cm, as well as doing the same job for T1- and TS1-encoded ec fonts". On my system/LaTeX installation, fix-cm is installed by default, whereas type1cm is not, so assuming this is generally true for other users, it would make more sense for matplotlib to use fix-cm instead of type1cm, as this should achieve the same results, and avoid forcing the user to install extra LaTeX packages.

import matplotlib.pyplot as plt
import matplotlib

matplotlib.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
matplotlib.rc('text', usetex=True)

plt.plot([1, 2], c="b")
plt.savefig("output.png")

Python 3.10.12
matplotlib Version: 3.8.2
Ubuntu 22.04.1 LTS

Proposed fix

Use fix-cm rather than type1cm for LaTeX

@jakelevi1996
Copy link
Author

Note: work around for me was as follows (source):

sudo apt install texlive-latex-extra cm-super dvipng

@story645
Copy link
Member

Do the tests pass if you use fix-cm?

@story645 story645 added the Documentation: build building the docs label Jan 15, 2024
@jakelevi1996
Copy link
Author

I've not run the matplotlib unit tests, I just wanted to get my script working. I raised this issue just as a suggestion to the matplotlib developers that it might be possible to use a more widely installed/less obscure package which achieves the same purpose

@jakelevi1996
Copy link
Author

Now that I've installed the extra packages on my system through apt, the tests would probably pass anyway, even if using fix-cm wasn't sufficient by itself to solve the problem

@story645
Copy link
Member

story645 commented Jan 15, 2024

I raised this issue just as a suggestion to the matplotlib developers that it might be possible to use a more widely installed/less obscure package which achieves the same purpose

I totally get you - I hate dealing w/ the latex installs. It's just that a requirement for switching over to fix-cm being the official recommendation is that all the tests would still have to pass ETA: so was curious if you knew if that was the case.

@anntzer
Copy link
Contributor

anntzer commented Jan 25, 2024

The patch would be something like

diff --git i/galleries/users_explain/text/usetex.py w/galleries/users_explain/text/usetex.py
index 0194a0030d..45773856e9 100644
--- i/galleries/users_explain/text/usetex.py
+++ w/galleries/users_explain/text/usetex.py
@@ -131,11 +131,6 @@ Possible hangups
 * Using MiKTeX with Computer Modern fonts, if you get odd \*Agg and PNG
   results, go to MiKTeX/Options and update your format files
 
-* On Ubuntu and Gentoo, the base texlive install does not ship with
-  the type1cm package. You may need to install some of the extra
-  packages to get all the goodies that come bundled with other LaTeX
-  distributions.
-
 * Some progress has been made so Matplotlib uses the dvi files
   directly for text layout. This allows LaTeX to be used for text
   layout with the pdf and svg backends, as well as the \*Agg and PS
diff --git i/lib/matplotlib/mpl-data/matplotlibrc w/lib/matplotlib/mpl-data/matplotlibrc
index 301afc3845..9b786fd201 100644
--- i/lib/matplotlib/mpl-data/matplotlibrc
+++ w/lib/matplotlib/mpl-data/matplotlibrc
@@ -327,8 +327,8 @@
                         # Note that it has to be put on a single line, which may
                         # become quite long.
                         # The following packages are always loaded with usetex,
-                        # so beware of package collisions:
-                        #   geometry, inputenc, type1cm.
+                        # beware of package collisions:
+                        #   color, fix-cm, geometry, graphicx, textcomp.
                         # PostScript (PSNFSS) font packages may also be
                         # loaded, depending on your font settings.
 
diff --git i/lib/matplotlib/mpl-data/stylelib/classic.mplstyle w/lib/matplotlib/mpl-data/stylelib/classic.mplstyle
index 976ab29190..b0c983e7d8 100644
--- i/lib/matplotlib/mpl-data/stylelib/classic.mplstyle
+++ w/lib/matplotlib/mpl-data/stylelib/classic.mplstyle
@@ -122,8 +122,8 @@ text.latex.preamble :  # IMPROPER USE OF THIS FEATURE WILL LEAD TO LATEX FAILURE
                        # Note that it has to be put on a single line, which may
                        # become quite long.
                        # The following packages are always loaded with usetex, so
-                       # beware of package collisions: color, geometry, graphicx,
-                       # type1cm, textcomp.
+                       # beware of package collisions:
+                       # color, fix-cm, geometry, graphicx, textcomp.
                        # Adobe Postscript (PSSNFS) font packages may also be
                        # loaded, depending on your font settings.
 
diff --git i/lib/matplotlib/texmanager.py w/lib/matplotlib/texmanager.py
index 812eab58b8..752a832f6c 100644
--- i/lib/matplotlib/texmanager.py
+++ w/lib/matplotlib/texmanager.py
@@ -158,7 +158,6 @@ class TexManager:
                for family in ['serif', 'sans-serif', 'monospace']}
         if requested_family == 'cursive':
             cmd.add(preambles['cursive'])
-        cmd.add(r'\usepackage{type1cm}')
         preamble = '\n'.join(sorted(cmd))
         fontcmd = (r'\sffamily' if requested_family == 'sans-serif' else
                    r'\ttfamily' if requested_family == 'monospace' else
@@ -200,6 +199,7 @@ class TexManager:
         font_preamble, fontcmd = cls._get_font_preamble_and_command()
         baselineskip = 1.25 * fontsize
         return "\n".join([
+            r"\RequirePackage{fix-cm}",
             r"\documentclass{article}",
             r"% Pass-through \mathdefault, which is used in non-usetex mode",
             r"% to use the default text font but was historically suppressed",

but this breaks (apparently legitimately) test_subplots.py::test_subplots_offsettext and test_usetex.py::test_usetex.

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