Skip to content

Instantly share code, notes, and snippets.

@xtornasol512
Created October 20, 2022 03:17
Show Gist options
  • Save xtornasol512/0e9b791919d90f83605cd257f0f757e6 to your computer and use it in GitHub Desktop.
Save xtornasol512/0e9b791919d90f83605cd257f0f757e6 to your computer and use it in GitHub Desktop.
Scape functions for latex, compatible only with python 3+
import re
import unicodedata
def escape_text(s):
# Convert from python escape text to latex format.
"""^*¨ÑLIOPL:_:(/&%&()=(/&WQ·W$ERTY +´^*Ñ´plqj"""
# This block is omited
s = s.replace("·", "")
s = s.replace("¨", "")
s = s.replace("$", "")
s = s.replace("\"", "``")
s = s.replace("<", "\\textless ")
s = s.replace(">", "\\textgreater ")
s = s.replace("&", "\\& ")
s = s.replace("%", "\\% ")
s = s.replace("#", "\\# ")
s = s.replace("\n", "\\" + "\\ ")
s = s.replace("_", "\\textunderscore ")
s = s.replace("{", "\\{ ")
s = s.replace("}", "\\} ")
s = s.replace("^", "\\^{} ")
s = s.replace("\'", "\\textsc{\\char13} ")
s = s.replace("´", "\\textsc{\\char13} ")
s = s.replace("°", " (grados)")
s = s.replace("º", " (grados)")
s = s.strip()
# If no length, return something useful
if len(s) == 0:
s = "NA"
return s
def clean_text(text):
""" Method to clean characters """
text = text.replace("\r", "")
text = text.replace("\t", "")
text = unicodedata.normalize('NFKD', text).encode('ascii', 'ignore')
# Allowed characters for RegEx function:
# Lower/Mayus characters (a-z) (A-Z) [ascii encoding]
# Numbers (0-9)
# Special (.) (/) (() ()) (-) (=) (@)
# Blank space
decoded_text = text.decode()
regex = re.compile(r'[A-Za-z0-9. ^/\-\(\)\=@#&%\+\n]', re.IGNORECASE)
text = ''.join(char for char in decoded_text if regex.match(char)) # noqa: W605
text = text.strip()
# Add a post space in order to avoid latex errors ie '\\ string'
text = text.replace("#", "\\# ")
text = text.replace("&", "\\& ")
text = text.replace("%", "\\% ")
text = text.replace("^", "\\^{} ")
text = text.replace("\n", "\\" + "\\ ")
return text if len(text) != 0 else "N/A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment