This is my first post here. Pardon for any ignorance. Similar question have been asked on this website but this is not a duplicate.
I have this tex file. As some of you might know that wordpress.com support latex but for that one has to write the post in following manner:
$latex your-latex-code-here$
I use overleaf to write code. Now I have a tex file in hand but replacing every $ by $latex is very tedious. So I was thinking of using python to do the dirty work for me.
I know how replace function works.How to search and replace text in a file using Python?
import fileinput
with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(text_to_search, replacement_text), end='')
or
# Read in the file
with open('file.txt', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('ram', 'abcd')
# Write the file out again
with open('file.txt', 'w') as file:
file.write(filedata)
But the problem is that it will replace both the $ signs. For example if I have some thing like this:
Let $x$ be a real number. Then we define square of $x$ as $x^2$.
If I run this code it will return the output as :
Let $latex x$latex be a real number. Then we define square of $latex x$latex as $latex x^2$latex.
which is meaningless. I just want first dollar sign to be replaced. I tried to think but I am stuck.