10000 ENH: add math mode to formatter escape="latex-math" by natmokval · Pull Request #50398 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

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
Next Next commit
ENH: add escape math mode with escape=latex-math
  • Loading branch information
natmokval committed Jan 28, 2023
commit 4f9e8e189cab32c8261c5df84fa5f9a2353d57fc
60 changes: 55 additions & 5 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ def format(
Use 'latex' to replace the characters ``&``, ``%``, ``$``, ``#``, ``_``,
``{``, ``}``, ``~``, ``^``, and ``\`` in the cell display string with
LaTeX-safe sequences.
Use 'latex-math' to replace the characters the same way as in 'latex' mode,
except for math substrings, which start and end with ``$``.
Escaping is done before ``formatter``.

.. versionadded:: 1.3.0
Expand Down Expand Up @@ -1105,16 +1107,28 @@ def format(
<td .. >NA</td>
...

Using a ``formatter`` with LaTeX ``escape``.
Using a ``formatter`` with ``escape`` in 'latex' mode.

>>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]])
>>> df = pd.DataFrame([["123"], ["~ ^"], ["%#"]])
Copy link
Contributor

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.

Copy link
Contributor Author

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.

>>> df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
... # doctest: +SKIP
\begin{tabular}{ll}
{} & {0} \\
& 0 \\
0 & \textbf{123} \\
1 & \textbf{\textasciitilde \space \textasciicircum } \\
2 & \textbf{\$\%\#} \\
2 & \textbf{\%\#} \\
\end{tabular}

Using ``escape`` in 'latex-math' mode.

>>> df = pd.DataFrame([[r"$\sum_{i=1}^{10} a_i$ a~b $\alpha \
... = \frac{\beta}{\zeta^2}$"], ["%#^ $ \$x^2 $"]])
>>> df.style.format(escape="latex-math").to_latex()
... # doctest: +SKIP
\begin{tabular}{ll}
& 0 \\
0 & $\sum_{i=1}^{10} a_i$ a\textasciitilde b $\alpha = \frac{\beta}{\zeta^2}$ \\
1 & \%\#\textasciicircum \space $ \$x^2 $ \\
\end{tabular}

Pandas defines a `number-format` pseudo CSS attribute instead of the `.format`
Expand Down Expand Up @@ -1743,9 +1757,12 @@ def _str_escape(x, escape):
return escape_html(x)
elif escape == "latex":
return _escape_latex(x)
elif escape == "latex-math":
return _escape_latex_math(x)
else:
raise ValueError(
f"`escape` only permitted in {{'html', 'latex'}}, got {escape}"
f"`escape` only permitted in {{'html', 'latex', 'latex-math'}}, \
got {escape}"
)
return x

Expand Down Expand Up @@ -2344,3 +2361,36 @@ def _escape_latex(s):
.replace("^", "\\textasciicircum ")
.replace("ab2§=§8yz", "\\textbackslash ")
)


def _escape_latex_math(s):
r"""
All characters between two characters ``$`` are preserved.

The substrings in LaTeX math mode, which start with the character ``$``
and end with ``$``, are preserved without escaping. Otherwise
regular LaTeX escaping applies. See ``_escape_latex()``.

Parameters
----------
s : str
Input to be escaped

Return
------
str :
Escaped string
"""
s = s.replace(r"\$", r"ab2§=§8yz")
Copy link
Contributor

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!)

pattern = re.compile(r"\$.*?\$")
pos = 0
ps = pattern.search(s, pos)
res = []
while ps:
res.append(_escape_latex(s[pos : ps.span()[0]]))
res.append(ps.group())
pos = ps.span()[1]
ps = pattern.search(s, pos)

res.append(_escape_latex(s[pos : len(s)]))
return "".join(res).replace(r"ab2§=§8yz", r"\$")
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/style/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def test_format_decimal(formatter, thousands, precision, func, col):


def test_str_escape_error():
msg = "`escape` only permitted in {'html', 'latex'}, got "
msg = "`escape` only permitted in {'html', 'latex', 'latex-math'}, got "
with pytest.raises(ValueError, match=msg):
_str_escape("text", "bad_escape")

Expand Down
0