r""" All characters in LaTeX math mode are preserved. The substrings in LaTeX math mode, which either are surrounded by two characters ``$`` or start with the character ``\(`` and end with ``\)``, are preserved without escaping. Otherwise regular LaTeX escaping applies. Paramet
(s: str)
| 2646 | |
| 2647 | |
| 2648 | def _escape_latex_math(s: str) -> str: |
| 2649 | r""" |
| 2650 | All characters in LaTeX math mode are preserved. |
| 2651 | |
| 2652 | The substrings in LaTeX math mode, which either are surrounded |
| 2653 | by two characters ``$`` or start with the character ``\(`` and end with ``\)``, |
| 2654 | are preserved without escaping. Otherwise regular LaTeX escaping applies. |
| 2655 | |
| 2656 | Parameters |
| 2657 | ---------- |
| 2658 | s : str |
| 2659 | Input to be escaped |
| 2660 | |
| 2661 | Return |
| 2662 | ------ |
| 2663 | str : |
| 2664 | Escaped string |
| 2665 | """ |
| 2666 | s = s.replace(r"\$", r"rt8§=§7wz") |
| 2667 | ps_d = re.compile(r"\$.*?\$").search(s, 0) |
| 2668 | ps_p = re.compile(r"\(.*?\)").search(s, 0) |
| 2669 | mode = [] |
| 2670 | if ps_d: |
| 2671 | mode.append(ps_d.span()[0]) |
| 2672 | if ps_p: |
| 2673 | mode.append(ps_p.span()[0]) |
| 2674 | if len(mode) == 0: |
| 2675 | return _escape_latex(s.replace(r"rt8§=§7wz", r"\$")) |
| 2676 | if s[mode[0]] == r"$": |
| 2677 | return _math_mode_with_dollar(s.replace(r"rt8§=§7wz", r"\$")) |
| 2678 | if s[mode[0] - 1 : mode[0] + 1] == r"\(": |
| 2679 | return _math_mode_with_parentheses(s.replace(r"rt8§=§7wz", r"\$")) |
| 2680 | else: |
| 2681 | return _escape_latex(s.replace(r"rt8§=§7wz", r"\$")) |
no test coverage detected