Skip to content

Backport PR #21568 on branch v3.5.x (Enhancing support for tex and datetimes) #21625

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

Merged
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
7 changes: 5 additions & 2 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,11 @@ def _wrap_in_tex(text):
p = r'([a-zA-Z]+)'
ret_text = re.sub(p, r'}$\1$\\mathdefault{', text)

# Braces ensure dashes are not spaced like binary operators.
ret_text = '$\\mathdefault{'+ret_text.replace('-', '{-}')+'}$'
# Braces ensure symbols are not spaced like binary operators.
ret_text = ret_text.replace('-', '{-}').replace(':', '{:}')
# To not concatenate space between numbers.
ret_text = ret_text.replace(' ', r'\;')
ret_text = '$\\mathdefault{' + ret_text + '}$'
ret_text = ret_text.replace('$\\mathdefault{}$', '')
return ret_text

Expand Down
26 changes: 15 additions & 11 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import pytest

from matplotlib import rc_context
from matplotlib import rc_context, style
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import image_comparison
Expand Down Expand Up @@ -323,13 +323,17 @@ def callable_formatting_function(dates, _):

@pytest.mark.parametrize('delta, expected', [
(datetime.timedelta(weeks=52 * 200),
[r'$\mathdefault{%d}$' % (year,) for year in range(1990, 2171, 20)]),
[r'$\mathdefault{%d}$' % year for year in range(1990, 2171, 20)]),
(datetime.timedelta(days=30),
[r'Jan$\mathdefault{ %02d 1990}$' % (day,) for day in range(1, 32, 3)]),
[r'$\mathdefault{1990{-}01{-}%02d}$' % day for day in range(1, 32, 3)]),
(datetime.timedelta(hours=20),
[r'$\mathdefault{%02d:00:00}$' % (hour,) for hour in range(0, 21, 2)]),
[r'$\mathdefault{01{-}01\;%02d}$' % hour for hour in range(0, 21, 2)]),
(datetime.timedelta(minutes=10),
[r'$\mathdefault{01\;00{:}%02d}$' % minu for minu in range(0, 11)]),
])
def test_date_formatter_usetex(delta, expected):
style.use("default")

d1 = datetime.datetime(1990, 1, 1)
d2 = d1 + delta

Expand Down Expand Up @@ -609,14 +613,14 @@ def test_concise_formatter_show_offset(t_delta, expected):
'$\\mathdefault{25}$', '$\\mathdefault{29}$', 'Feb',
'$\\mathdefault{05}$', '$\\mathdefault{09}$']),
(datetime.timedelta(hours=40),
['Jan$\\mathdefault{{-}01}$', '$\\mathdefault{04:00}$',
'$\\mathdefault{08:00}$', '$\\mathdefault{12:00}$',
'$\\mathdefault{16:00}$', '$\\mathdefault{20:00}$',
'Jan$\\mathdefault{{-}02}$', '$\\mathdefault{04:00}$',
'$\\mathdefault{08:00}$', '$\\mathdefault{12:00}$',
'$\\mathdefault{16:00}$']),
['Jan$\\mathdefault{{-}01}$', '$\\mathdefault{04{:}00}$',
'$\\mathdefault{08{:}00}$', '$\\mathdefault{12{:}00}$',
'$\\mathdefault{16{:}00}$', '$\\mathdefault{20{:}00}$',
'Jan$\\mathdefault{{-}02}$', '$\\mathdefault{04{:}00}$',
'$\\mathdefault{08{:}00}$', '$\\mathdefault{12{:}00}$',
'$\\mathdefault{16{:}00}$']),
(datetime.timedelta(seconds=2),
['$\\mathdefault{59.5}$', '$\\mathdefault{00:00}$',
['$\\mathdefault{59.5}$', '$\\mathdefault{00{:}00}$',
'$\\mathdefault{00.5}$', '$\\mathdefault{01.0}$',
'$\\mathdefault{01.5}$', '$\\mathdefault{02.0}$',
'$\\mathdefault{02.5}$']),
Expand Down