@@ -989,9 +989,8 @@ def format(
989989 Use 'latex' to replace the characters ``&``, ``%``, ``$``, ``#``, ``_``,
990990 ``{``, ``}``, ``~``, ``^``, and ``\`` in the cell display string with
991991 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 ``\)``.
995994 Escaping is done before ``formatter``.
996995
997996 .. versionadded:: 1.3.0
@@ -1108,16 +1107,30 @@ def format(
11081107 <td .. >NA</td>
11091108 ...
11101109
1111- Using a ``formatter`` with LaTeX ``escape``.
1110+ Using a ``formatter`` with LaTeX ``escape`` in 'latex' mode .
11121111
1113- >>> df = pd.DataFrame([["123"], ["~ ^"], ["$ %#"]])
1112+ >>> df = pd.DataFrame([["123"], ["~ ^"], ["%#"]])
11141113 >>> df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
11151114 ... # doctest: +SKIP
11161115 \begin{tabular}{ll}
1117- {} & {0} \\
1116+ & 0 \\
11181117 0 & \textbf{123} \\
11191118 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 \) \\
11211134 \end{tabular}
11221135
11231136 Pandas defines a `number-format` pseudo CSS attribute instead of the `.format`
@@ -2354,31 +2367,17 @@ def _escape_latex(s):
23542367
23552368def _escape_latex_math (s ):
23562369 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 ``\)``.
23592374
23602375 Parameters
23612376 ----------
23622377 s : str
2363- Input to be escaped
23642378
23652379 Return
23662380 ------
2367- str :
2368- Escaped string
2381+ str
23692382 """
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