[OS X TeX] Find and replace with regular expressions

Matthew Leingang leingang at math.harvard.edu
Mon Nov 19 11:24:14 EST 2007

On Nov 19, 2007, at 8:39 AM, Eric Jablow wrote:

> On Nov 19, 2007, at 8:18 AM, Matthew Leingang wrote:
>> On Nov 19, 2007, at 7:20 AM, Alessandro Andretta wrote:
>>> I would like to replace the $ signs in a LaTeX file with \( and \)
>>> thus I would have to search for $...$ and replace it with \( ... \)
>>> where ... is any string of symbols (letters, numbers, \ , {} &,etc)
>>> EXCEPT $. I tried the help menu of TeXShop, TextMate, Textwrangler,
>>> but their general treatment seems be a bit too technical for my  
>>> taste.
>>> Is there a simple way to archive this with any of the editors above?
>> Dear Alessandro,
>> [snip]
>> TeXShop has regular expression support built in.  So you can open  
>> up a find window,
>> put this in the search box
>> 	\$([^$]+)\$
>> and this in the replace box
>> 	\\(\1\\)
>> make sure you click "Regular Expressions," and go.
> This has some flaws.  You need negative look-behind assertions in  
> the search, because you don't want to pick up the \$ of
>     Acme sells widgets for \$5.00 a unit. The cost to produce $x$  
> widgets is $2x-0.03 x^2$ dollars.

Indeed, my original response pointed out that very flaw, and others.   
But I wanted to give an answer which would probably work for a lot of  
the document, and if he wanted to (learn enough to) tweak the regex  
he could, or just find the rest manually, he'd still save a lot of work.

Is there any regex that's going to work 100% of the time?.  How would  
you fix the text

	Acme sells widgets for \$5.00 a unit.  The cost to produce $x$  
widgets is $\$(2x-0.03 x^2)$.

--Matthew Leingang

Matthew Leingang
Preceptor in Mathematics
Harvard University


More information about the MacOSX-TeX mailing list