1

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
Shweta Aggrawal
  • 137
  • 1
  • 5
  • What should be the output for this: `$x$ be a real number. Then we define square of $x$ as $x^2$.`? I guess this: `$latex x$ be a real number. Then we define square of $latex x$ as $x^2$` ? – DirtyBit Jan 30 '19 at 09:42
  • The program will search for $ and will replace it by $latex. I have written the output in OP @user5173426 Problem is that I want to replace only the first dollar sign. – Shweta Aggrawal Jan 30 '19 at 09:44
  • 1
    Two ideas come to mind. You can look at regular expressions (which allow you to replace at the beginning of a word) or you could split up each line by the space character and then replace $ within each word only once. – 576i Jan 30 '19 at 09:45

2 Answers2

4

Try using this regular expression in your code:

import re

s = 'Let $x$ be a real number. Then we define square of $x$ as $x^2$.'
re.sub(r'\$(.+?)\$', r'$latex \1$', s)

There's no need to split/join the original string (that will mess the Latex text!), and the result will look like this:

'Let $latex x$ be a real number. Then we define square of $latex x$ as $latex x^2$.'
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

You can do this by splitting the original string and replacing only the first instance of $ in each word.

s = "Let $x$ be a real number. Then we define square of $x$ as $x^2$."
r = [i.replace("$", "$latex ", 1) for i in s.split()]
print(" ".join(r))
# Let $latex x$ be a real number. Then we define square of $latex x$ as $latex x^2$.
Jebby
  • 1,845
  • 1
  • 12
  • 25
  • this will remove double space and mess up the latex text. – 576i Jan 30 '19 at 09:48
  • Thanks a lot. If you don't mind where can I learn this type of coding. I am a mathematics student and learning python side by side. Your code is so beautifully compact. Any recommendations.Most of the book I have seen like LPTHW does not introduce anything like this. Thanks. – Shweta Aggrawal Jan 30 '19 at 09:51
  • 1
    I believe the 'compact' form you are referring to is line 2? If so, that is what is known as a `list comprehension` in python. You can learn how to use them in many guides around the internet (not to mention explained in depth much better than I can in comments.) – Jebby Jan 30 '19 at 09:56