@@ -989,9 +989,8 @@ def format(
989
989
Use 'latex' to replace the characters ``&``, ``%``, ``$``, ``#``, ``_``,
990
990
``{``, ``}``, ``~``, ``^``, and ``\`` in the cell display string with
991
991
LaTeX-safe sequences.
992
- Use 'latex-math' to replace the characters ``&``, ``%``, ``#``, ``_``,
993
- ``{``, ``}``, ``~``, ``^``, and ``\``
994
- in the cell display string with LaTeX-safe sequences.
992
+ All characters remain in the string as is. LaTeX math mode starts with
993
+ the character ``$`` or ``\(`` and ends with ``$`` or ``\)``.
995
994
Escaping is done before ``formatter``.
996
995
997
996
.. versionadded:: 1.3.0
@@ -1108,16 +1107,30 @@ def format(
1108
1107
<td .. >NA</td>
1109
1108
...
1110
1109
1111
- Using a ``formatter`` with LaTeX ``escape``.
1110
+ Using a ``formatter`` with LaTeX ``escape`` in 'latex' mode .
1112
1111
1113
- >>> df = pd.DataFrame([["123"], ["~ ^"], ["$ %#"]])
1112
+ >>> df = pd.DataFrame([["123"], ["~ ^"], ["%#"]])
1114
1113
>>> df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
1115
1114
... # doctest: +SKIP
1116
1115
\begin{tabular}{ll}
1117
- {} & {0} \\
1116
+ & 0 \\
1118
1117
0 & \textbf{123} \\
1119
1118
1 & \textbf{\textasciitilde \space \textasciicircum } \\
1120
- 2 & \textbf{\$\%\#} \\
1119
+ 2 & \textbf{\%\#} \\
1120
+ \end{tabular}
1121
+
1122
+ Using a ``formatter`` with LaTeX ``escape`` in 'latex-math' mode.
1123
+
1124
+ >>> df = pd.DataFrame([[r"$ \alpha = \frac{\beta}{\zeta^2} $", \
1125
+ r"$ \$ \ \ \# \{\} $"],[r"\( \sum_{i=1}^{10} a_i \)", \
1126
+ r"\( \int_0^\infty \mathrm{e}^{-x}\,\mathrm{d}x \)"]])
1127
+ >>> print(df.style.format(escape="latex-math").to_latex())
1128
+ ... # doctest: +SKIP
1129
+ \begin{tabular}{lll}
1130
+ & 0 & 1 \\
1131
+ 0 & $ \alpha = \frac{\beta}{\zeta^2} $ & $ \$ \ \ \# \{\} $ \\
1132
+ 1 & \( \sum_{i=1}^{10} a_i \) & \( \int_0^\infty \mathrm{e}^{-x}\, \
1133
+ \mathrm{d}x \) \\
1121
1134
\end{tabular}
1122
1135
1123
1136
Pandas defines a `number-format` pseudo CSS attribute instead of the `.format`
@@ -2354,31 +2367,17 @@ def _escape_latex(s):
2354
2367
2355
2368
def _escape_latex_math (s ):
2356
2369
r"""
2357
- Replace the characters ``&``, ``%``, ``#``, ``_``, ``{``, ``}``,
2358
- ``~``, ``^``, and ``\`` in the string with LaTeX-safe sequences.
2370
+ All characters in the string are preserved.
2371
+
2372
+ Note that LaTeX math mode starts with the character ``$`` or ``\(``
2373
+ and ends with ``$`` or ``\)``.
2359
2374
2360
2375
Parameters
2361
2376
----------
2362
2377
s : str
2363
- Input to be escaped
2364
2378
2365
2379
Return
2366
2380
------
2367
- str :
2368
- Escaped string
2381
+ str
2369
2382
"""
2370
- return (
2371
- s .replace ("\\ " , "ab2§=§8yz" ) # rare string for final conversion: avoid \\ clash
2372
- .replace ("ab2§=§8yz " , "ab2§=§8yz\\ space " ) # since \backslash gobbles spaces
2373
- .replace ("&" , "\\ &" )
2374
- .replace ("%" , "\\ %" )
2375
- .replace ("#" , "\\ #" )
2376
- .replace ("_" , "\\ _" )
2377
- .replace ("{" , "\\ {" )
2378
- .replace ("}" , "\\ }" )
2379
- .replace ("~ " , "~\\ space " ) # since \textasciitilde gobbles spaces
2380
- .replace ("~" , "\\ textasciitilde " )
2381
- .replace ("^ " , "^\\ space " ) # since \textasciicircum gobbles spaces
2382
- .replace ("^" , "\\ textasciicircum " )
2383
- .replace ("ab2§=§8yz" , "\\ textbackslash " )
2384
- )
2383
+ return s
0 commit comments