I am trying to write a python script that converts normal text into latex code. So I have to replace $, &, and % by \$, \&, and \% respectively. Also, I have to replace contents inside a square bracket by a blank string since the text was copied from Wikipedia. Here is my code :
import re
square_bracket = r"\[[^\]]*\]"
def formatting(text):
no_brackets = re.sub(square_bracket, '', text)
and_ = re.sub('&', '\\&', no_brackets)
percentage_ = re.sub('%', '\\%', and_)
dollar_ = re.sub('$', '\\$', percentage_)
return dollar_
print(formatting("aca & akhil[3255d] 56 $ 72 %"))
Output is:
[Command: python3 -u /home/aca/Documents/python/basics/test.py]
aca \& akhil 56 $ 72 \$
[Finished in 0.131s]
I am ok with square bracket, & and %. But dollar sign makes the things messy as it converts normal text into mathematical argument in latex. How do I fix this?