8000 Add substack cmd for mathtext by devRD · Pull Request #26151 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add substack cmd for mathtext #26151

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
merged 3 commits into from
Jul 27, 2023
Merged

Add substack cmd for mathtext #26151

merged 3 commits into from
Jul 27, 2023

Conversation

devRD
Copy link
Contributor
@devRD devRD commented Jun 19, 2023

PR summary

Fixes #14247

PR checklist

@devRD devRD marked this pull request as ready for review June 19, 2023 13:18
@devRD devRD marked this pull request as draft June 19, 2023 13:37
@devRD devRD marked this pull request as ready for review June 21, 2023 02:58
@@ -134,7 +134,7 @@
r'$\sum x\quad\sum^nx\quad\sum_nx\quad\sum_n^nx\quad\prod x\quad\prod^nx\quad\prod_nx\quad\prod_n^nx$', # GitHub issue 18085
r'$1.$ $2.$ $19680801.$ $a.$ $b.$ $mpl.$',
r'$\text{text}_{\text{sub}}^{\text{sup}} + \text{\$foo\$} + \frac{\text{num}}{\mathbf{\text{den}}}\text{with space, curly brackets \{\}, and dash -}$',

r'$\sum_{\substack{k = 1}{k \neq \lfloor n/2\rfloor}}^{n}P(i,j) \sum_{\substack{i \neq 0}{-1 \leq i \leq 3}{1 \leq j \leq 5}} F^i(x,y)$',
Copy link
Member

Choose a reason for hiding this comment

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

Although I believe that your syntax makes very much sense, it appears as if the existing \substack command is simply using \\ for next line.

Suggested change
r'$\sum_{\substack{k = 1}{k \neq \lfloor n/2\rfloor}}^{n}P(i,j) \sum_{\substack{i \neq 0}{-1 \leq i \leq 3}{1 \leq j \leq 5}} F^i(x,y)$',
r'$\sum_{\substack{k = 1\\ k \neq \lfloor n/2\rfloor}}^{n}P(i,j) \sum_{\substack{i \neq 0\\ -1 \leq i \leq 3\\ 1 \leq j \leq 5}} F^i(x,y)$',

Apart from that, the outcome looks good! Maybe one would like to test with \left \lfloor \frac{n}{2} \right\rfloor as well to see how that behaves?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really sure how to go about adding the \\ as next-line behavior to the substack input due to the python's handling of escape characters, I'm currently still looking into it.

Added the \left \lfloor \frac{n}{2} \right\rfloor as a test with the current implemention.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe the parser expert @anntzer has some input?

Copy link
Member

Choose a reason for hiding this comment

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

I would expect pyparsing's DelimitedList would be helpful here

Note, however, that it looks like it is in a slightly awkward transition from the delimited_list function to the DelimitedList class, the former being deprecated upon the introduction of the latter in pyparsing 3.1.0 (which we are incompatible with for other reasons right now, but should fix)

As such may need something like:

if pyparsing.__version__info__ < (3, 1):
    from pyparsing import delimited_list as DelimitedList
else:
    from pyparsing import DelimitedList

thickness = state.get_current_underline_thickness()
vlist = []

max_width = sorted(toks[1:], key= lambda c: c.width)[-1].width
Copy link
Contributor
@tfpf tfpf Jun 21, 2023

Choose a reason for hiding this comment

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

Maybe this could be written using max, which also takes a key argument.

max_width = max(toks[1:], key=lambda c: c.width).width

Something like that? Or even simpler:

max_width = max(map(lambda c: c.width, toks[1:]))

which seems more readable to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! That was neat

Comment on lines 2620 to 2623
vlist = [x for l in (vlist[i:i+1] +
[Vbox(0, thickness * 2.0)] *
(i < len(vlist) - 1)
for i in range(0, len(vlist), 1)) for x in l]
Copy link
Contributor
@tfpf tfpf Jun 23, 2023

Choose a reason for hiding this comment

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

Took me an embarrassingly long time to understand!:sweat_smile: I think it separates each element of vlist with Vbox(0, thickness * 2)?

This is probably one of those instances I'd prioritise readability.

vlist_ = [None, Vbox(0, thickness * 2)] * len(vlist)
vlist_[::2] = vlist
vlist_.pop()

This should achieve this same thing. (Another option is to append the Vboxes in the for loop just above.)

@oscargus: I'd defer to your opinion here. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe one could just do:

vlist = [val for pair in zip(vlist, [Vbox(0, thickness * 2)]*len(vlist)) for val in pair]
del vlist[-1]

?

(But, yes, I find double (and triple) list comprehensions quite hard to follow to start with.)

@devRD devRD force-pushed the mt-substack branch 2 times, most recently from 42735be to 2b85ec6 Compare June 24, 2023 14:44
@devRD devRD force-pushed the mt-substack branch 2 times, most recently from 8694d6a to e5f318d Compare July 9, 2023 22:42
@@ -1700,6 +1700,7 @@ def names(elt):
csname = expr.split("{", 1)[0]
err = (csname + "".join("{%s}" % name for name in names(args))
if expr == csname else expr)
print(csname, args, err)
Copy link
Member

Choose a reason for hiding this comment

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

I take it that there will be another update to this and @ksunden was only sorting out the parsing?

But this line should go.

@@ -134,7 +134,7 @@
r'$\sum x\quad\sum^nx\quad\sum_nx\quad\sum_n^nx\quad\prod x\quad\prod^nx\quad\prod_nx\quad\prod_n^nx$', # GitHub issue 18085
r'$1.$ $2.$ $19680801.$ $a.$ $b.$ $mpl.$',
r'$\text{text}_{\text{sub}}^{\text{sup}} + \text{\$foo\$} + \frac{\text{num}}{\mathbf{\text{den}}}\text{with space, curly brackets \{\}, and dash -}$',

r'$\sum_{\substack{k = 1}{k \neq \lfloor n/2\rfloor}}^{n}P(i,j) \sum_{\substack{i \neq 0}{-1 \leq i \leq 3}{1 \leq j \leq 5}} F^i(x,y) \sum_{\substack{\left \lfloor \frac{n}{2} \right\rfloor}} F(n)$',
Copy link
Member

Choose a reason for hiding this comment

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

This should then be modified to use \\, but ideally there should be no need for other changes.

devRD added 2 commits July 22, 2023 13:22
Co-Authored By: Kyle Sunden <git@ksunden.space>
Co-Authored By: Kyle Sunden <git@ksunden.space>
Co-authored-by: Kyle Sunden <git@ksunden.space>
@ksunden ksunden merged commit 1e6b5ec into matplotlib:main Jul 27, 2023
@QuLogic QuLogic added this to the v3.8.0 milestone Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

latex \substack doesn't work
5 participants
0