File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616
1717
1818def _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
2739def make_risk_report (
You can’t perform that action at this time.
0 commit comments