@@ -989,6 +989,9 @@ 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.
992995 Escaping is done before ``formatter``.
993996
994997 .. versionadded:: 1.3.0
@@ -1742,9 +1745,12 @@ def _str_escape(x, escape):
17421745 return escape_html (x )
17431746 elif escape == "latex" :
17441747 return _escape_latex (x )
1748+ elif escape == "latex-math" :
1749+ return _escape_latex_math (x )
17451750 else :
17461751 raise ValueError (
1747- f"`escape` only permitted in {{'html', 'latex'}}, got { escape } "
1752+ f"`escape` only permitted in {{'html', 'latex', 'latex-math'}}, \
1753+ got { escape } "
17481754 )
17491755 return x
17501756
@@ -2343,3 +2349,35 @@ def _escape_latex(s):
23432349 .replace ("^" , "\\ textasciicircum " )
23442350 .replace ("ab2§=§8yz" , "\\ textbackslash " )
23452351 )
2352+
2353+
2354+ def _escape_latex_math (s ):
2355+ r"""
2356+ Replace the characters ``&``, ``%``, ``#``, ``_``, ``{``, ``}``,
2357+ ``~``, ``^``, and ``\`` in the string with LaTeX-safe sequences.
2358+
2359+ Parameters
2360+ ----------
2361+ s : str
2362+ Input to be escaped
2363+
2364+ Return
2365+ ------
2366+ str :
2367+ Escaped string
2368+ """
2369+ return (
2370+ s .replace ("\\ " , "ab2§=§8yz" ) # rare string for final conversion: avoid \\ clash
2371+ .replace ("ab2§=§8yz " , "ab2§=§8yz\\ space " ) # since \backslash gobbles spaces
2372+ .replace ("&" , "\\ &" )
2373+ .replace ("%" , "\\ %" )
2374+ .replace ("#" , "\\ #" )
2375+ .replace ("_" , "\\ _" )
2376+ .replace ("{" , "\\ {" )
2377+ .replace ("}" , "\\ }" )
2378+ .replace ("~ " , "~\\ space " ) # since \textasciitilde gobbles spaces
2379+ .replace ("~" , "\\ textasciitilde " )
2380+ .replace ("^ " , "^\\ space " ) # since \textasciicircum gobbles spaces
2381+ .replace ("^" , "\\ textasciicircum " )
2382+ .replace ("ab2§=§8yz" , "\\ textbackslash " )
2383+ )
0 commit comments