-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: add math mode to formatter escape="latex-math" #50398
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
ENH: add math mode to formatter escape="latex-math" #50398
Conversation
4fb4d86
to
adedc02
Compare
Hi @attack68, could you please review this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- The
escape
option is also a global pandas option configurable. We should expand the documentation there to specify the new option also.
A question: the characters _, {, }, ^ and \ are frequently used within the math mode, i.e. between $
's to specify equations. Does preserving the $'s but still escaping those math-related characters inside $
's break the representation of the math in Latex?
For example what happens to $ \alpha = \frac{\beta}{\zeta^2} $
under this escape mode?
pandas/io/formats/style_render.py
Outdated
Use 'latex-math' to replace the characters ``&``, ``%``, ``#``, ``_``, | ||
``{``, ``}``, ``~``, ``^``, and ``\`` | ||
in the cell display string with LaTeX-safe sequences. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whilst this is true I think it is clearer to say:
"Replaces the same characters as the latex
option except $
which is preserved to allow conversion to math mode".
We should also add an example probably.
pandas/io/formats/style_render.py
Outdated
def _escape_latex_math(s): | ||
r""" | ||
Replace the characters ``&``, ``%``, ``#``, ``_``, ``{``, ``}``, | ||
``~``, ``^``, and ``\`` in the string with LaTeX-safe sequences. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add here: note ``$`` not escaped
.
Hi @attack68, I checked the example For example: On one hand, the characters I added an example for math-mode and corrected LaTeX mode example. |
I think we may be able to use regex to detect these. Im not brialliant at regex, but doing a search on stack overflow we can use look back and look forward, I think. These would be the rules I thiunk we aim for:
|
Thank you, I understand. I'll try to use regex. |
Hi @attack68. As you suggested I made a solution using regex to detect On the other hand, math expressions can be surrounded by the characters I was thinking about updating the PR but I still have one question. Should we consider the case when we have an expression containing both math-mode types, |
I think a basic PR just to handle and test the $ math mode case will be a good start. Once that is in place we could extend it in the future, and with the tests developed provides a reliable base from which to work |
8a329f8
to
4f9e8e1
Compare
Hi @attack68. I added a basic implementation for math mode between |
pandas/io/formats/style_render.py
Outdated
str : | ||
Escaped string | ||
""" | ||
s = s.replace(r"\$", r"ab2§=§8yz") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I understand what you are doing here, you are substituting out an escaped $ sign, using a proxy uuid string ab2§=§8yz
, but this string is the same string as I have used in the _escape_latex
method for the same reason for dealing with some other character combiantions. I wonder if this might cause cross-pollution. Regardless, since it is a procy uuid string, it would be better to change the digits I think (using only digits that are not escaped!)
pandas/io/formats/style_render.py
Outdated
|
||
>>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]]) | ||
>>> df = pd.DataFrame([["123"], ["~ ^"], ["%#"]]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its fine to change this example. Just curious as to the reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think one change worth making, otherwise I think it is good to add it. The test demonstrates the effect nicely. Didn't check the CI tests, we make sur ethey are green before merging.
I changed uuid string to be unique, as you suggested. And also reverted the example for latex mode. I changed it by mistake, there is nothing wrong with $
in latex mode (the online latex compiler which I used at first had some sort of bug).
CI tests look good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think one change worth making, otherwise I think it is good to add it.
The test demonstrates the effect nicely.
Didn't check the CI tests, we make sur ethey are green before merging.
Ah I just noticed there is no addition to the |
Thank you, I added a line to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks.
Could you merge in main and resolve the merge conflict? |
I merged main in and resolved the merge conflict. |
I resolved a new merge conflict and did merge in main. Sorry, it took so long. |
@mroeschke can we still get this in to 2.0? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay sure. This is okay for 2.0 since it looks to have been ready for a while and started before the RC release
Thanks @natmokval |
Thank you for reviewing my PR. |
escape="latex"
#50040Added
latex-math
mode to avoid escaping$
.We probably shouldn't escape
\[
and\]
. They can not be applied between\begin{tabular}
and\end{tabular}
, which is used for both Series and DataFrames.