Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support units for the third\genfrac argument#22171

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
oscargus wants to merge1 commit intomatplotlib:main
base:main
Choose a base branch
Loading
fromoscargus:genfracunits
Draft
Show file tree
Hide file tree
Changes fromall 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
11 changes: 11 additions & 0 deletionsdoc/users/next_whats_new/mathtext_genfrac_supports_unit.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
``mathtext`` now supports units for the bar thickness ``\genfrac`` argument
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This follows the standard LaTeX version where units are required.

.. code-block:: python

import matplotlib.pyplot as plt

plt.text(0.5, 0.5, r'$\genfrac{(}{)}{0.5cm}{0}{foo}{bar}$')
plt.draw()
43 changes: 38 additions & 5 deletionslib/matplotlib/_mathtext.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1958,6 +1958,7 @@ def __init__(self):
p.customspace = Forward()
p.end_group = Forward()
p.float_literal = Forward()
p.float_with_wo_unit = Forward()
p.font = Forward()
p.frac = Forward()
p.dfrac = Forward()
Expand DownExpand Up@@ -1995,6 +1996,7 @@ def __init__(self):
p.symbol_name = Forward()
p.token = Forward()
p.underset = Forward()
p.unit = Forward()
p.unknown_symbol = Forward()

# Set names on everything -- very useful for debugging
Expand All@@ -2004,6 +2006,10 @@ def __init__(self):

p.float_literal <<= Regex(r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)")
p.int_literal <<= Regex("[-+]?[0-9]+")
p.unit <<= Regex("(pt|mm|cm|in|em|mu|"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think you can get rid of the grouping parentheses, which should be marginally more efficient.

"sp|bp|dd|pc|nc|nd|cc)")
p.float_with_wo_unit <<= Group((p.float_literal + p.unit) |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

what's "wo"?

p.float_literal)

p.lbrace <<= Literal('{').suppress()
p.rbrace <<= Literal('}').suppress()
Expand DownExpand Up@@ -2093,7 +2099,7 @@ def __init__(self):
+ (p.lbrace
+ Optional(p.ambi_delim | p.right_delim_safe, default='')
+ p.rbrace)
+ (p.lbrace + p.float_literal + p.rbrace)
+ (p.lbrace + p.float_with_wo_unit + p.rbrace)
+ p.simple_group + p.required_group + p.required_group)
| Error("Expected "
r"\genfrac{ldelim}{rdelim}{rulesize}{style}{num}{den}"))
Expand DownExpand Up@@ -2322,6 +2328,22 @@ def _make_space(self, percentage):
r'\!': -0.16667, # -3/18 em = -3 mu
}

_unit_multipliers = {
# pt|mm|cm|in|mu|sp|bp|dd|pc|nc|nd|cc
"pt": 1,
'mm': 7227/2540,
'cm': 7227/254,
'in': 7227/100,
'mu': 7227/2540000,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I can't find a ref for mu, nc, nd as standard tex units, perhaps you can link their definition? (as a comment here at least, not in the code)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I used some stack exchange thread combined with other sources, but cannot seem to find where I got ex and em from now. I agree with that they shouldn't be there though (just added what was there without really thinking about the meaning).

Here are some better sources:
https://en.wikibooks.org/wiki/LaTeX/Lengths
https://latexref.xyz/Units-of-length.html

(And here is a good discussion why ex and em are bad choices:https://tex.stackexchange.com/questions/4239/which-measurement-units-should-one-use-in-latex
)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

'sp': 1/65536,
'bp': 803/800,
'dd': 1238/1157,
'pc': 12,
'nc': 1370/107,
'nd': 685/642,
'cc': 14856/1157
}

def space(self, s, loc, toks):
tok, = toks
num = self._space_widths[tok]
Expand DownExpand Up@@ -2741,7 +2763,7 @@ def _genfrac(self, ldelim, rdelim, rule, style, num, den):
thickness = state.font_output.get_underline_thickness(
state.font, state.fontsize, state.dpi)

rule =float(rule)
rule =self._get_float_with_unit(rule, state.fontsize)

if style is not self._MathStyle.DISPLAYSTYLE:
num.shrink()
Expand DownExpand Up@@ -2786,22 +2808,33 @@ def frac(self, s, loc, toks):
thickness = state.font_output.get_underline_thickness(
state.font, state.fontsize, state.dpi)
(num, den), = toks
return self._genfrac('', '', thickness, self._MathStyle.TEXTSTYLE,
return self._genfrac('', '',[thickness], self._MathStyle.TEXTSTYLE,
num, den)

def dfrac(self, s, loc, toks):
state = self.get_state()
thickness = state.font_output.get_underline_thickness(
state.font, state.fontsize, state.dpi)
(num, den), = toks
return self._genfrac('', '', thickness, self._MathStyle.DISPLAYSTYLE,
return self._genfrac('', '',[thickness], self._MathStyle.DISPLAYSTYLE,
num, den)

def binom(self, s, loc, toks):
(num, den), = toks
return self._genfrac('(', ')', 0.0, self._MathStyle.TEXTSTYLE,
return self._genfrac('(', ')',[0.0], self._MathStyle.TEXTSTYLE,
num, den)

def _get_float_with_unit(self, val, fontsize):
ret = float(val[0])
if len(val) == 2:
if val[1].lower() == 'em':
# "1 em is a distance equal to the type size"
ret = ret*fontsize
else:
# Remaining units
ret = ret*self._unit_multipliers[val[1]]
return ret

def _genset(self, s, loc, toks):
(annotation, body), = toks
state = self.get_state()
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp