cleaners.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """
  2. Postprocessing functions for cleaning up latex equations in linear format which don't give valid LaTeX.
  3. """
  4. import re
  5. clean_exps = {
  6. r"\\degf": "°F",
  7. r"\\degc": "°C",
  8. r"(\\cbrt)(\w+)": r"\\sqrt[3]{\2}",
  9. r"(\\qdrt)(\w+)": r"\\sqrt[4]{\2}",
  10. r"\\sfrac": r"\\frac",
  11. r"(\\o[i]+nt)(\w+)": r"\1{\2}",
  12. r"\\bullet(\w+)": r"\\bullet \1",
  13. r"\\sum([a-zA-Z0-9]+)": r"\\sum{\1}",
  14. r"\\prod([a-zA-Z0-9]+)": r"\\prod{\1}",
  15. r"\\amalg([a-zA-Z0-9]+)": r"\\amalg{\1}",
  16. r"\\bigcup([a-zA-Z0-9]+)": r"\\bigcup{\1}",
  17. r"\\bigcap([a-zA-Z0-9]+)": r"\\bigcap{\1}",
  18. r"\\bigvee([a-zA-Z0-9]+)": r"\\bigvee{\1}",
  19. r"\\bigwedge([a-zA-Z0-9]+)": r"\\bigwedge{\1}",
  20. r"\\lfloor([a-zA-Z0-9]+)": r"\\lfloor{\1}",
  21. r"\\lceil([a-zA-Z0-9]+)": r"\\lceil{\1}",
  22. r"\\lim\\below\{(.+)\}\{(.+)\}": r"\\lim_{\1}{\2}",
  23. r"\\min\\below\{(.+)\}\{(.+)\}": r"\\min_{\1}{\2}",
  24. r"\\max\\below\{(.+)\}\{(.+)\}": r"\\max_{\1}{\2}",
  25. }
  26. def clean_exp(exp):
  27. """
  28. Takes in a linear expression and converts known invalid LaTeX equations to valid LaTeX
  29. :param exp:str - An equation in invalid syntax
  30. :return :str - A valid equation
  31. """
  32. for e in clean_exps:
  33. exp = re.sub(e, clean_exps[e], exp)
  34. return exp