1

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?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
mig001
  • 179
  • 1
  • 18

1 Answers1

2

In regular expression, $ is an end anchor, so you need to escape it:

dollar_ = re.sub('\$', '\\\\$', percentage_)
#                 ^
#                here

If you do that, the output will be what you expect:

aca \& akhil 56 \\$ 72 \%
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thanks. It worked. But '\S' created ```invalid escape sequence '\$' ```. I could fix that by '\\$'. – mig001 May 27 '20 at 02:48