Skip to content

Better error message for unescaped underscores #18357

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/matplotlib/tests/test_texmanager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import matplotlib.pyplot as plt
import pytest

from matplotlib import pyplot as plt, checkdep_usetex
from matplotlib.texmanager import TexManager


needs_usetex = pytest.mark.skipif(
not checkdep_usetex(True),
reason="This test needs a TeX installation")


def test_fontconfig_preamble():
"""
Test that the preamble is included in _fontconfig
Expand All @@ -16,3 +23,15 @@ def test_fontconfig_preamble():
font_config2 = tm2.get_font_config()

assert font_config1 != font_config2


@needs_usetex
def test_usetex_missing_underscore():
"""
Test that failed TeX rendering due to an unescaped underscore has a
custom error message.
"""
with pytest.raises(RuntimeError,
match='caused by an unescaped underscore'):
plt.text(0, 0, 'foo_bar', usetex=True)
plt.draw() # TeX rendering is done at draw time
40 changes: 32 additions & 8 deletions lib/matplotlib/texmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,38 @@ def _run_checked_subprocess(self, command, tex, *, cwd=None):
'Failed to process string with tex because {} could not be '
'found'.format(command[0])) from exc
except subprocess.CalledProcessError as exc:
raise RuntimeError(
'{prog} was not able to process the following string:\n'
'{tex!r}\n\n'
'Here is the full report generated by {prog}:\n'
'{exc}\n\n'.format(
prog=command[0],
tex=tex.encode('unicode_escape'),
exc=exc.output.decode('utf-8'))) from exc
tex_log = exc.output.decode('utf-8')

# whether the exception was likely caused by an unescaped
# underscore. This cannot be easily determined unambiguously.
# By requiring `'$' not in tex` we err on the side of preventing
# false positives, because we only want to issue the customized
# error message if we are sure there's an unescaped underscore.
unescaped_underscore = (
'_' in tex and '$' not in tex and
re.match(r'.*Missing \$ inserted.*_\n', tex_log,
flags=re.DOTALL)
)
if unescaped_underscore:
message = (
'{prog} was not able to process the following string:\n'
'{tex!r}\n\n'
'This is likely caused by an unescaped underscore.\n'
'You may escape the underscore or add '
'r"\\usepackage{{underscore}}"\n'
'to the matplotlib rcParam text.latex.preamble'.format(
prog=command[0],
tex=tex.encode('unicode_escape')))
else:
message = (
'{prog} was not able to process the following string:\n'
'{tex!r}\n\n'
'Here is the full report generated by {prog}:\n'
'{exc}\n\n'.format(
prog=command[0],
tex=tex.encode('unicode_escape'),
exc=tex_log))
raise RuntimeError(message) from exc
_log.debug(report)
return report

Expand Down