Quantcast
Channel: User Mark - Stack Overflow
Viewing all articles
Browse latest Browse all 44

Answer by Mark for How can I escape LaTeX special characters inside django templates?

$
0
0

Alex's answer including suggestions in code, if you want to copy-paste:

import redef tex_escape(text):"""        :param text: a plain text message        :return: the message escaped to appear correctly in LaTeX"""    conv = {'&': r'\&','%': r'\%','$': r'\$','#': r'\#','_': r'\_','{': r'\{','}': r'\}','~': r'\textasciitilde{}','^': r'\^{}','\\': r'\textbackslash{}','<': r'\textless{}','>': r'\textgreater{}',    }    regex = re.compile('|'.join(re.escape(str(key)) for key in sorted(conv.keys(), key = lambda item: - len(item))))    return regex.sub(lambda match: conv[match.group()], text)

See Easiest way to replace a string using a dictionary of replacements? for replacement approach.


Viewing all articles
Browse latest Browse all 44

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>