Skip to content

Commit 01fb7bb

Browse files
authored
Merge pull request #20706 from kir0ul/issue_17774
Include `underscore.sty`
2 parents 64e898a + 05a2312 commit 01fb7bb

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Improve underscore support in LaTeX
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The `underscore <https://ctan.org/pkg/underscore>`_ package is now a requirement to improve support for underscores in LaTeX.

doc/devel/dependencies.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,13 @@ Required:
222222

223223
* a minimal working LaTeX distribution
224224
* `Graphviz <http://www.graphviz.org/download>`_
225-
* the LaTeX packages *cm-super* and *dvipng* (if your OS bundles TeXLive, the
225+
* the following LaTeX packages (if your OS bundles TeXLive, the
226226
"complete" version of the installer, e.g. "texlive-full" or "texlive-all",
227-
will often automatically include these packages)
227+
will often automatically include these packages):
228+
229+
* `cm-super <https://ctan.org/pkg/cm-super>`_
230+
* `dvipng <https://ctan.org/pkg/dvipng>`_
231+
* `underscore <https://ctan.org/pkg/underscore>`_
228232

229233
Optional, but recommended:
230234

lib/matplotlib/tests/test_usetex.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,22 @@ def test_usetex_packages(pkg):
100100
text.get_window_extent())
101101

102102

103-
def test_textcomp_full():
104-
plt.rcParams["text.latex.preamble"] = r"\usepackage[full]{textcomp}"
103+
@pytest.mark.parametrize(
104+
"preamble",
105+
[r"\usepackage[full]{textcomp}", r"\usepackage{underscore}"],
106+
)
107+
def test_latex_pkg_already_loaded(preamble):
108+
plt.rcParams["text.latex.preamble"] = preamble
105109
fig = plt.figure()
106110
fig.text(.5, .5, "hello, world", usetex=True)
107111
fig.canvas.draw()
112+
113+
114+
def test_usetex_with_underscore():
115+
plt.rcParams["text.usetex"] = True
116+
df = {"a_b": range(5)[::-1], "c": range(5)}
117+
fig, ax = plt.subplots()
118+
ax.plot("c", "a_b", data=df)
119+
ax.legend()
120+
ax.text(0, 0, "foo_bar", usetex=True)
121+
plt.draw()

lib/matplotlib/texmanager.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ def _get_preamble(self):
174174
# relies on a custom preamble to change the geometry.
175175
r"\usepackage[papersize=72in, margin=1in]{geometry}",
176176
self.get_custom_preamble(),
177+
# Use `underscore` package to take care of underscores in text
178+
# The [strings] option allows to use underscores in file names
179+
r"\makeatletter"
180+
r"\@ifpackageloaded{underscore}{}"
181+
r"{\usepackage[strings]{underscore}}"
182+
r"\makeatother",
177183
# textcomp is loaded last (if not already loaded by the custom
178184
# preamble) in order not to clash with custom packages (e.g.
179185
# newtxtext) which load it with different options.

0 commit comments

Comments
 (0)