# replace often used Tex-Literals ($and $$) for math regions into $$or$$ and $or$ Is there an editor or IDE (or a script) for substituting the TeX-Commands  and $$ for opening and closing math regions into the corresponding LaTeX-Commands $$,$$ and $,$? I'm sure there is a tool - and I'm searching for. I think, this will result in better readable code. A simple search and replace wont do that - but with regular expressions, that should not be problem. I simply do not want to invent the wheel a second time. - How about: sed -r 's/\$\$([^\$]+)\$\$/\$\1\$/g' | sed -r 's/\$([^\$]+)\$/\$$\1\$$/g' It can probably be done more compactly. But that should work for most cases. – Roelof Spijker Feb 28 '12 at 8:28 @RoelofSpijker this is a fine idea - but ones has to exclude matchings in comments - and further only the first match gots replaced. Sorry - I'm not that firm with sed. I understand the rule - but for me it is not replacing all $ and $$s. – Bastian Ebeling Feb 28 '12 at 9:13 That sed script might break if you do funny stuff like x=y\ \text{and of course also z} – Seamus Feb 28 '12 at 10:23 Other edge cases: a$$b$ (I think the sed script would be okay on this one) and $\$a$ (which I don't think it would get). –  Loop Space Feb 28 '12 at 10:56
To be honest, I think that this is one of those cases where you use a script to get most of the way and then fix the rest yourself. The only real way to do this would be with TeX itself since TeX is the only thing that truly understands about mathmode and catcodes. –  Loop Space Feb 28 '12 at 10:58

Although I like Andrew's answer since that script apparently handles a little more than (my invocation of) good old sed, I will add my comment as an answer.
sed -r 's/\$\$([^\$]+)\$\$/\$\1\$/g' | sed -r 's/\$([^\$]+)\$/\$$\1\$$/g'
Basically, it replaces each occurence of $$...$$ with $...$ first and then replaces each occurence of $...$ with $$...$$. As Seamus pointed out, it breaks when you use alternative mathmode constructs and as the OP pointed out, unbalanced $'s in the comments will also break it. To take care of things like \text will be extremely difficult. I think you will pretty much have to implement half of the TeX compiler to cover those cases. - A search led me to this website where I found: DeBuck - Remove Dollars from a LaTeX Document This is a Perl script which replaces dollars in a LaTeX document by $$...$$ or $...$. Providing there are no sneaky macros and the file LaTeX's correctly it does a reasonable job. I also found an explanation of the name buried in an obscure answer on this site. - Before this gets too many votes, I'd like to point out that it doesn't do much more than Roelof's sed script - it tries to keep track of whether the maths is inline or display to avoid the $a$$b trap and it can distinguish between  and \ (I think). But it wouldn't work with Seamus' example. That would require a bit more work. – Loop Space Feb 28 '12 at 10:55 the a$$b\$ trap can be side stepped using non-greedy regexs, Seamus' example is still tricky though –  cmhughes Feb 28 '12 at 19:14