Skip to content

ENH: add LaTeX math mode with parentheses #51903

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

Conversation

natmokval
Copy link
Contributor

@natmokval natmokval commented Mar 11, 2023

This pr is related to pr #50398

A new way to enter latex-math mode was added. Now we can apply math mode by starting substrings with the character \( and ending with the character \).

@natmokval natmokval marked this pull request as ready for review March 11, 2023 16:41
@natmokval
Copy link
Contributor Author

@attack68, could you please take a look at this pr?

@mroeschke mroeschke requested a review from attack68 March 13, 2023 17:21
@mroeschke mroeschke added the Styler conditional formatting using DataFrame.style label Mar 13, 2023
The substrings in LaTeX math mode, which either are surrounded
by two characters ``$`` or start with the character ``\(`` and end with ``\)``,
are preserved without escaping. Otherwise regular LaTeX escaping applies.
See ``_escape_latex()``.
Copy link
Contributor

Choose a reason for hiding this comment

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

_escape_latex is an internal function so shouldn't be publically stated or pointed to

Copy link
Contributor Author

Choose a reason for hiding this comment

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

completely agree, I'll remove _escape_latex() from docs

@attack68
Copy link
Contributor

ALso may or may not need a whatsnew release note dependin which release this gets into

@MarcoGorelli
Copy link
Member

I think it would be 2.1 at this point

@natmokval natmokval force-pushed the 50040-add-math-mode-formatter-escape=latex-part2 branch from 50e51c7 to 2e283a7 Compare March 15, 2023 17:23
@natmokval
Copy link
Contributor Author

Hi @attack68, could you please take a look at my last changes?

@natmokval
Copy link
Contributor Author

Hi @attack68. I added an example to latex-math mode and a line to whatsnew
Docs for Styler was updated as well: I added a description for latex-math mode to the parameter escape.

Could you please take a look at my last changes?

Comment on lines 2482 to 2487
pattern_d = re.compile(r"\$.*?\$")
pattern_p = re.compile(r"\(.*?\)")
pos_d = 0
pos_p = 0
ps_d = pattern_d.search(s, pos_d)
ps_p = pattern_p.search(s, pos_p)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pattern_d = re.compile(r"\$.*?\$")
pattern_p = re.compile(r"\(.*?\)")
pos_d = 0
pos_p = 0
ps_d = pattern_d.search(s, pos_d)
ps_p = pattern_p.search(s, pos_p)
ps_d = re.compile(r"\$.*?\$").search(s, 0)
ps_p = re.compile(r"\(.*?\)").search(s, pos_p)

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I completely agree. I applied this change in the next commit.

I did it locally and pushed the commit.

str :
Escaped string
"""
s = s.replace(r"\$", r"rt8§=§7wz")
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this code.
You are replacing the string \$ with a uuid string, first.
Then, you are searching for a pattern (r"\$.*?\$") that cannot exist, since it was replaced.

Then in line 2490 you are replacing the same string s again with the same uuid, but this is unnecessary since it has already done this in line 2481.
I think your tests pass and this does the correct thing but I think some of these lines are redundant and do nothing for the overall effect??

Copy link
Contributor Author

@natmokval natmokval Mar 25, 2023

Choose a reason for hiding this comment

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

Thank you for the comment.
It’s right, the replacement in line 2490 has a mistake. The right one would be the reverse replacement: s.replace(r"rt8§=§7wz", r"\$")

I can explain what I am trying to do. When I checked the function _escape_latex_math I noticed that for a string like r"$&%#^$" which contains only one sign "$" and only one combination "\$" I got a wrong result, because the function processed this string in math mode. By doing the replacement in line 2481 I exclude from the consideration the string “\$” to avoid confusing it with “$”. Then I get the correct result and do the reverse replacement. If we don’t have a combination of one sign "$" and one sign "\$" we don’t need to do this check, but I prefer to leave it.

I corrected my mistake a made a new commit. I also added an example for this case in the test.

@natmokval
Copy link
Contributor Author

Hi @attack68, could you please take a look at my last changes?

@attack68
Copy link
Contributor

not sure why this is failing some checks, but otherwise LGTM.

@natmokval
Copy link
Contributor Author

not sure why this is failing some checks, but otherwise LGTM.

Thank you @attack68, I updated my branch. Now, all checks pass without failures.

Copy link
Contributor

@attack68 attack68 left a comment

Choose a reason for hiding this comment

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

lgtm

@mroeschke mroeschke added this to the 2.1 milestone Mar 31, 2023
@mroeschke mroeschke merged commit c61c7ba into pandas-dev:main Mar 31, 2023
@mroeschke
Copy link
Member

Thanks @natmokval

topper-123 pushed a commit to topper-123/pandas that referenced this pull request Apr 1, 2023
* ENH: add math mode with parentheses

* ENH: add math mode with parentheses II

* ENH: add math mode with parentheses III

* ENH: add an example to latex-math mode and a line to whatsnew

* ENH: update docs for Styler: add description latex-math mode to escape

* improve code style

* add an example to test and correct _escape_latex_math
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants