@@ -1117,7 +1117,8 @@ def format(
1117
1117
2 & \textbf{\$\%\#} \\
1118
1118
\end{tabular}
1119
1119
1120
- Using ``escape`` in 'latex-math' mode.
1120
+ Applying ``escape`` in 'latex-math' mode. In the example below
1121
+ we enter math mode using the charackter ``$``.
1121
1122
1122
1123
>>> df = pd.DataFrame([[r"$\sum_{i=1}^{10} a_i$ a~b $\alpha \
1123
1124
... = \frac{\beta}{\zeta^2}$"], ["%#^ $ \$x^2 $"]])
@@ -1129,6 +1130,20 @@ def format(
1129
1130
1 & \%\#\textasciicircum \space $ \$x^2 $ \\
1130
1131
\end{tabular}
1131
1132
1133
+ We can use the charackter ``\(`` to enter math mode and the charackter ``\)``
1134
+ to close math mode.
1135
+
1136
+ >>> df = pd.DataFrame([[r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha \
1137
+ ... = \frac{\beta}{\zeta^2}\)"], ["%#^ \( \$x^2 \)"]])
1138
+ >>> df.style.format(escape="latex-math").to_latex()
1139
+ ... # doctest: +SKIP
1140
+ \begin{tabular}{ll}
1141
+ & 0 \\
1142
+ 0 & \(\sum_{i=1}^{10} a_i\) a\textasciitilde b \(\alpha
1143
+ = \frac{\beta}{\zeta^2}\) \\
1144
+ 1 & \%\#\textasciicircum \space \( \$x^2 \) \\
1145
+ \end{tabular}
1146
+
1132
1147
Pandas defines a `number-format` pseudo CSS attribute instead of the `.format`
1133
1148
method to create `to_excel` permissible formatting. Note that semi-colons are
1134
1149
CSS protected characters but used as separators in Excel's format string.
@@ -2357,17 +2372,20 @@ def _escape_latex(s):
2357
2372
.replace ("~" , "\\ textasciitilde " )
2358
2373
.replace ("^ " , "^\\ space " ) # since \textasciicircum gobbles spaces
2359
2374
.replace ("^" , "\\ textasciicircum " )
2375
+ .replace ("ab2§=§8yz(" , "\\ ( " )
2376
+ .replace ("ab2§=§8yz)" , "\\ ) " )
2360
2377
.replace ("ab2§=§8yz" , "\\ textbackslash " )
2361
2378
)
2362
2379
2363
2380
2364
2381
def _escape_latex_math (s ):
2365
2382
r"""
2366
- All characters between two characters ``$`` are preserved.
2383
+ All characters in LaTeX math mode are preserved.
2367
2384
2368
- The substrings in LaTeX math mode, which start with the character ``$``
2369
- and end with ``$``, are preserved without escaping. Otherwise
2370
- regular LaTeX escaping applies. See ``_escape_latex()``.
2385
+ The substrings in LaTeX math mode, which either are surrounded
2386
+ by two characters ``$`` or start with the character ``\(`` and end with ``\)``,
2387
+ are preserved without escaping. Otherwise regular LaTeX escaping applies.
2388
+ See ``_escape_latex()``.
2371
2389
2372
2390
Parameters
2373
2391
----------
@@ -2379,16 +2397,37 @@ def _escape_latex_math(s):
2379
2397
str :
2380
2398
Escaped string
2381
2399
"""
2382
- s = s .replace (r"\$" , r"rt8§=§7wz" )
2383
- pattern = re .compile (r"\$.*?\$" )
2384
- pos = 0
2385
- ps = pattern .search (s , pos )
2386
- res = []
2387
- while ps :
2388
- res .append (_escape_latex (s [pos : ps .span ()[0 ]]))
2389
- res .append (ps .group ())
2390
- pos = ps .span ()[1 ]
2400
+
2401
+ def _math_mode_with_dollar (s ):
2402
+ s = s .replace (r"\$" , r"rt8§=§7wz" )
2403
+ pattern = re .compile (r"\$.*?\$" )
2404
+ pos = 0
2391
2405
ps = pattern .search (s , pos )
2406
+ res = []
2407
+ while ps :
2408
+ res .append (_escape_latex (s [pos : ps .span ()[0 ]]))
2409
+ res .append (ps .group ())
2410
+ pos = ps .span ()[1 ]
2411
+ ps = pattern .search (s , pos )
2412
+
2413
+ res .append (_escape_latex (s [pos : len (s )]))
2414
+ return "" .join (res ).replace (r"rt8§=§7wz" , r"\$" )
2415
+
2416
+ def _math_mode_with_parentheses (s ):
2417
+ s = s .replace (r"\(" , r"LEFT§=§6yzLEFT" ).replace (r"\)" , r"RIGHTab5§=§RIGHT" )
2418
+ res = []
2419
+ for item in re .split (r"LEFT§=§6yz|ab5§=§RIGHT" , s ):
2420
+ if item .startswith ("LEFT" ) and item .endswith ("RIGHT" ):
2421
+ res .append (item .replace ("LEFT" , r"\(" ).replace ("RIGHT" , r"\)" ))
2422
+ else :
2423
+ res .append (
2424
+ _escape_latex (item ).replace ("LEFT" , r"\(" ).replace ("RIGHT" , r"\)" )
2425
+ )
2426
+ return "" .join (res )
2392
2427
2393
- res .append (_escape_latex (s [pos : len (s )]))
2394
- return "" .join (res ).replace (r"rt8§=§7wz" , r"\$" )
2428
+ if s .replace (r"\$" , "ab" ).find (r"$" ) > - 1 :
2429
+ return _math_mode_with_dollar (s )
2430
+ elif s .find (r"\(" ) > - 1 :
2431
+ return _math_mode_with_parentheses (s )
2432
+ else :
2433
+ return _escape_latex (s )
0 commit comments