8000 ENH: add LaTeX math mode with parentheses by natmokval · Pull Request #51903 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from 1 commit
Commits
File filter 10BC0

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
Prev Previous commit
Next Next commit
improve code style
  • Loading branch information
natmokval committed Mar 21, 2023
commit ed27582a92f222e1be2cdcb40e9d2d487567968d
8 changes: 2 additions & 6 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,12 +2479,8 @@ def _escape_latex_math(s):
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.

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, 0)
mode = []
if ps_d:
mode.append(ps_d.span()[0])
Expand Down
0