-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_clean_latex_string.py
66 lines (56 loc) · 2.45 KB
/
lib_clean_latex_string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
def remove_latex_presention_markings(latex_str: str) -> str:
"""
This function is from
https://github.com/allofphysicsgraph/ui_v8_website_flask_neo4j/blob/gh-pages/webserver/library/compute.py
clean the latex string
based on the struggle with spacing,
https://github.com/sympy/sympy/issues/19075#issuecomment-633643570
BHP realized removing the presentation-related aspects would make the task for Sympy easier
>>> remove_latex_presention_markings('a\\ b = c')
'a b = c'
"""
# trace_id = str(random.randint(1000000, 9999999))
# print("[TRACE] func: compute/remove_latex_presention_markings start " + trace_id)
# print("latex to be cleaned: " + latex_str)
if "\\left." in latex_str:
latex_str = latex_str.replace("\\left.", "")
if "\\right." in latex_str:
latex_str = latex_str.replace("\\right.", "")
if "\\left|" in latex_str:
latex_str = latex_str.replace("\\left|", "|")
if "\\right|" in latex_str:
latex_str = latex_str.replace("\\right|", "|")
if "\\left(" in latex_str:
latex_str = latex_str.replace("\\left(", "(")
if "\\right)" in latex_str:
latex_str = latex_str.replace("\\right)", ")")
if "\\," in latex_str:
# logger.debug("found space \\,")
latex_str = latex_str.replace("\\,", " ") # thinspace
if "\\ " in latex_str:
# logger.debug("found space \\ ")
latex_str = latex_str.replace("\\ ", " ")
if "\\;" in latex_str:
# logger.debug("found space \\;")
latex_str = latex_str.replace("\\;", " ") # thick space
if "\\:" in latex_str:
# logger.debug("found space \\:")
latex_str = latex_str.replace("\\:", " ") # medium space
if "\\!" in latex_str:
# logger.debug("found space \\!")
latex_str = latex_str.replace("\\!", " ") # negative space
if "\\;" in latex_str:
# logger.debug("found space \\ ")
latex_str = latex_str.replace("\\ ", " ")
if "\\quad" in latex_str:
# logger.debug("found space \\quad")
latex_str = latex_str.replace("\\quad", " ")
if "\\qquad" in latex_str:
# logger.debug("found space \\qquad")
latex_str = latex_str.replace("\\qquad", " ")
# print("latex after cleaning: " + latex_str)
# print("[TRACE] func: compute/remove_latex_presention_markings end " + trace_id)
return latex_str
if __name__ == "__main__":
print("this is just a library")