@@ -1118,7 +1118,7 @@ def format(
1118
1118
\end{tabular}
1119
1119
1120
1120
Applying ``escape`` in 'latex-math' mode. In the example below
1121
- we enter math mode using the charackter ``$``.
1121
+ we enter math mode using the character ``$``.
1122
1122
1123
1123
>>> df = pd.DataFrame([[r"$\sum_{i=1}^{10} a_i$ a~b $\alpha \
1124
1124
... = \frac{\beta}{\zeta^2}$"], ["%#^ $ \$x^2 $"]])
@@ -1130,7 +1130,7 @@ def format(
1130
1130
1 & \%\#\textasciicircum \space $ \$x^2 $ \\
1131
1131
\end{tabular}
1132
1132
1133
- We can use the charackter ``\(`` to enter math mode and the charackter ``\)``
1133
+ We can use the character ``\(`` to enter math mode and the character ``\)``
1134
1134
to close math mode.
1135
1135
1136
1136
>>> df = pd.DataFrame([[r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha \
@@ -2359,7 +2359,8 @@ def _escape_latex(s):
2359
2359
Escaped string
2360
2360
"""
2361
2361
return (
2362
- s .replace ("\\ " , "ab2§=§8yz" ) # rare string for final conversion: avoid \\ clash
2362
+ s .replace ("\\ " , "ab2§=§8yz" )
2363
+ .replace ("\\ " , "ab2§=§8yz" ) # rare string for final conversion: avoid \\ clash
2363
2364
.replace ("ab2§=§8yz " , "ab2§=§8yz\\ space " ) # since \backslash gobbles spaces
2364
2365
.replace ("&" , "\\ &" )
2365
2366
.replace ("%" , "\\ %" )
@@ -2372,8 +2373,6 @@ def _escape_latex(s):
2372
2373
.replace ("~" , "\\ textasciitilde " )
2373
2374
.replace ("^ " , "^\\ space " ) # since \textasciicircum gobbles spaces
2374
2375
.replace ("^" , "\\ textasciicircum " )
2375
- .replace ("ab2§=§8yz(" , "\\ ( " )
2376
- .replace ("ab2§=§8yz)" , "\\ ) " )
2377
2376
.replace ("ab2§=§8yz" , "\\ textbackslash " )
2378
2377
)
2379
2378
@@ -2385,7 +2384,6 @@ def _escape_latex_math(s):
2385
2384
The substrings in LaTeX math mode, which either are surrounded
2386
2385
by two characters ``$`` or start with the character ``\(`` and end with ``\)``,
2387
2386
are preserved without escaping. Otherwise regular LaTeX escaping applies.
2388
- See ``_escape_latex()``.
2389
2387
2390
2388
Parameters
2391
2389
----------
@@ -2419,15 +2417,33 @@ def _math_mode_with_parentheses(s):
2419
2417
for item in re .split (r"LEFT§=§6yz|ab5§=§RIGHT" , s ):
2420
2418
if item .startswith ("LEFT" ) and item .endswith ("RIGHT" ):
2421
2419
res .append (item .replace ("LEFT" , r"\(" ).replace ("RIGHT" , r"\)" ))
2422
- else :
2420
+ elif "LEFT" in item and "RIGHT" in item :
2423
2421
res .append (
2424
2422
_escape_latex (item ).replace ("LEFT" , r"\(" ).replace ("RIGHT" , r"\)" )
2425
2423
)
2424
+ else :
2425
+ res .append (
2426
+ _escape_latex (item )
2427
+ .replace ("LEFT" , r"\textbackslash (" )
2428
+ .replace ("RIGHT" , r"\textbackslash )" )
2429
+ )
2426
2430
return "" .join (res )
2427
2431
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
+ s = s .replace (r"\$" , r"rt8§=§7wz" )
2433
+ pattern_d = re .compile (r"\$.*?\$" )
2434
+ pattern_p = re .compile (r"\\(.*?\\)" )
2435
+ pos_d = 0
2436
+ pos_p = 0
2437
+ ps_d = pattern_d .search (s , pos_d )
2438
+ ps_p = pattern_p .search (s , pos_p )
2439
+ mode = []
2440
+ if ps_d :
2441
+ mode .append (ps_d .span ()[0 ])
2442
+ if ps_p :
2443
+ mode .append (ps_p .span ()[0 ])
2444
+ if len (mode ) == 0 :
2445
+ return _escape_latex (s .replace (r"\$" , r"rt8§=§7wz" ))
2446
+ if s [min (mode )] == r"$" :
2447
+ return _math_mode_with_dollar (s .replace (r"\$" , r"rt8§=§7wz" ))
2432
2448
else :
2433
- return _escape_latex ( s )
2449
+ return _math_mode_with_parentheses ( s . replace ( r"\$" , r"rt8§=§7wz" ) )
0 commit comments