-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: add math mode to formatter escape="latex"
#50040
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
Comments
The stackoverflow examples specifically reference Your given example is also not ideal to describe your problem since your can actually code the separate scenarios using import pandas as pd
df = pd.DataFrame([[1,"$x=3$"],[3,"Text"]], columns=["A", "under_score"])
print(df.style.format_index(axis=1, escape="latex").to_latex())
\begin{tabular}{lrl}
& A & under\_score \\
0 & 1 & $x=3$ \\
1 & 3 & Text \\
\end{tabular} However, I observe your point about |
Of course you can always write your own formatting function anyway and supply that directly as the callable. |
You have summarized it better than I can.
I just referenced the Stackoverflow answers to emphasize that its not just my problem, but reoccurring.
Thanks for nice workaround!
I edited my request to reflect the problem better.
Nice, I didn't saw that it is implemented. Should I add another mode and document it? |
My point was that one can use df = pd.DataFrame([["%", "&", "$x=3$"]])
print(df.style.format(escape="latex").to_latex())
\begin{tabular}{llll}
& 0 & 1 & 2 \\
0 & \% & \& & \$x=3\$ \\
\end{tabular} But the def _custom_formatter(s):
return s.replace("%", "\\%").replace("&", "\\&")
print(df.style.format(_custom_formatter).to_latex())
\begin{tabular}{llll}
& 0 & 1 & 2 \\
0 & \% & \& & $x=3$ \\
\end{tabular} Therefore, this is a second workaround, and I do not believe your requirements are currently beyond pandas. It is just that, perhaps, a common usage pattern (latex-math) is slightly beyond the basic input parameters, |
Oh, okay. I thought you were trying to tell that one can: def _custom_escape(s):
return s.replace("%", "\\%").replace("&", "\\&")
print(df.style.format(escape=_custom_escape).to_latex()) But that doesn't seem the case. Using the formatter multiple times is of course possible. :) Then just update me if I should go ahead with providing a PR or if its not worth it maintaining it in the future. |
This will be a user-friendly addition to the API arguments. |
take |
@seyon99 you can't take an issue that has already been taken and which also already has a solution via the pull request that has already been submitted and approved. |
Feature Type
Adding new functionality to pandas
Changing existing functionality in pandas
Removing existing functionality in pandas
Problem Description
Have a DataFrame that contains both content in latex mathmode (eg.
$x=3$
), but also content which need to be escaped (eg.something_with_underscore
).With the current
escape
mode of thestyle.format()
Function it is only possible to either escape both or none of them, instead of being agnostic of what is needed.Feature Description
The easy way is to add an
escape="latex-math"
option and exclude the$
escaping if this string is supplied.pandas/pandas/io/formats/style_render.py
Line 2299 in 8dab54d
Because there might be other people needing different escapings one could also refactor the escape argument to take both predefined strings (e.g. "html" and "latex") but also a map with characters to escape.
Could be called like this:
Both
Alternative Solutions
Replace the
\\$
character with$
afterwards.As mentioned by @attack68, if the content to be escaped is only in the header you can use
format_index
:Additional Context
Seems like other people have the same problem:
https://stackoverflow.com/questions/48516653/how-to-prevent-pandas-dataframe-to-latex-from-adding-escape-characters-before
https://stackoverflow.com/questions/44260547/pandas-to-latex-escapes-mathmode
The text was updated successfully, but these errors were encountered: