# 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. • Thanks! In find-replace tool of Overleaf: for$...$to $$...$$ fill find field with (\$)([^$]+)(\$) and replace field with $$2$$. For $$...$$ to $...$ fill find with (\$\$)([^$]+)(\$\$) and replace with $2$. – Noir Jun 16 '18 at 6:38 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 If you editor has good regex support then, then you can quite easily use that to replace$...$to $$...$$ (and similarly with $$...$$)/ Here's how you can match for$...$/\s\$(.|\n)+?\$/g and similarly, matching for $$...$$: /\s\$\$(.|\n)+?\$\$/g This will not match the dollar signs as they have to be escaped with \$ and the above pattern requires any of the whitespace characters before the dollar sign (space, tab or newline). You can see a nice visualization for that here and here respectively.

The combination +? does what you would usually expect from + (that is, match one or more instance of the previous character) but the ? modifies the behaviour from being by greedy to being lazy. The difference is that the greedy method will find the longest matching string, whilst the lazy method will match the smallest string. This is probably clearest with an example:

Input string:       | Greedy match:       | Lazy match:
$lorem$ipsum$dolor$ | $lorem$ipsum$dolor$ | $lorem$, $dolor$

If your editor supports the s flag from Perl, then you can simplify (.|\n)+? to just .+? as . will match new lines too.

Finally, putting it together with a substitution, you get:

s/(\s)\$(.+?)\$/\1$$\2$$/sg

Note that I'm not escaping certain characters because it will depend on the particular implementation of regex. In particular, the group parentheses may require $$...$$ and the +, ? and the backslashes in the replacement may also need to be escaped depending on which regex implementation you are using.

If you are in the command line and have Perl, then the following two commands should do the trick:

perl -077pi.bak -e 's/(\s)\$\$(.+?)\$\$/\1\$\2\$/sg' document.tex
perl -077pi.bak -e 's/(\s)\$(.+?)\$/\1\$$\2\$$/sg' document.tex

Note that this will not match anything at the very start of the file; however, I don't think this will be a big issue as the start of the file will typically be a comment or at least \documentclass.

Michael Sharpe developed a python script called "dedollar". An archive with the script, an Apple Script and the documentation can be found at

https://dl.dropboxusercontent.com/u/3825336/TeX/Files/dedollar.zip

According to the author, this software is in version 1.0 and released under LPPL.

To quote the manual :

This is a python script whose purpose is to convert a .tex file using $..$ for inline math and $$..$$ for display math into the respective forms $$..$$, $..$. There are most likely more capable programs out there to do this, but I could not find one.

Some caveats:

• No program is ever going to be able to handle an arbitrary .tex file—there are just too many clever tricks (and clever people) for it to be able to anticipate.
• This programs handles the verbatim settings verb, verbatim, Verbatim and lstlisting. Other such environments would not be difficult to add.
• It is assumed that the environments verbatim, Verbatim and lstlisting are not ended on the same line on which they were begun, and that there is only one such environment begun on any one line.
• The only comments it understands are those following an unescaped %. Other comment environments such as comment and the primitive \iffalse..\fi are not handled, and will lead to problems if the material they contain disturbs the parity of $or $$. • The program is not yet suitable for encodings other than 8 bit extensions of ascii. Remark that used to calculate the coordinates (thanks to the calc library) in a TikZ picture should not be replaced by $$...$$. However, all the solutions provided in this page mess the following example : \documentclass{standalone} \usepackage{tikz} \usetikzlibrary{calc} \begin{document} \begin{tikzpicture} \draw (0, 0) node (y1) {\bullet}; \draw [->] (y1) to ((y1)-(3, 0)); \end{tikzpicture} \end{document} It presumably illustrate the point made by Sharpe, that No program is ever going to be able to handle an arbitrary .tex file—there are just too many clever tricks (and clever people) for it to be able to anticipate. The accepted answer does not seem to handle common multiline examples such as:$$ 1+1=2 $$The following sed command will resolve this problem: sed '/\\/{:x;N;/.*\\ */!bx;s/\\$$.*$$\\ */\$\1\$/}' (This command is slightly complicated by the fact that , [, and ] are all special characters in sed.) Explanation: • /\\/ If a line contains$$, then • N; append the next line, and then • /.*\$\$*$/!bx; continue appending lines until we don't end in $$• s/\\$$.*$$\\ */\$\1\$/ then replace the first$$ and the ending $$with $and$. Spaces after the ending$$ are allowed, and are irrelevant for the first $$. It goes without saying, be sure to backup your files before trying this. And as noted by Sharpe, quoted by Clément, No program is ever going to be able to handle an arbitrary .tex file--there are just too many clever tricks (and clever people) for it to be able to anticipate. • Edit: To avoid the edge case a$$b$, in the sed command replace the initial /\$\$/ with /^\$\$*$/. – Joseph Quinsey Mar 2 '19 at 19:14