Skip to content

Commit 0b13022

Browse files
wenh06Copilot
andauthored
Update diff_binom_confint/_applications.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 14b5b67 commit 0b13022

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

diff_binom_confint/_applications.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,24 @@
1616

1717

1818
def _latex_escape(line: str) -> str:
19-
"""Escape special characters in LaTeX."""
20-
# Escape % (not preceded by \)
21-
line = re.sub(r"(?<!\\)%", r"\\%", line)
22-
# Escape _ (not preceded by \)
23-
line = re.sub(r"(?<!\\)_", r"\\_", line)
24-
return line
19+
"""Escape LaTeX special characters in a string."""
20+
# LaTeX special characters that need escaping:
21+
# \ & % $ # _ { } ~ ^
22+
# We replace each occurrence with its escaped form.
23+
specials = {
24+
"\\": r"\textbackslash{}",
25+
"&": r"\&",
26+
"%": r"\%",
27+
"$": r"\$",
28+
"#": r"\#",
29+
"_": r"\_",
30+
"{": r"\{",
31+
"}": r"\}",
32+
"~": r"\textasciitilde{}",
33+
"^": r"\textasciicircum{}",
34+
}
35+
pattern = re.compile(r"([\\&%$#_{}~^])")
36+
return pattern.sub(lambda m: specials[m.group(1)], line)
2537

2638

2739
def make_risk_report(

0 commit comments

Comments
 (0)