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.

  • 6
    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. Commented Feb 28, 2012 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. Commented Feb 28, 2012 at 9:13
  • 2
    That sed script might break if you do funny stuff like $x=y\ \text{and of course also $z$}$
    – Seamus
    Commented Feb 28, 2012 at 10:23
  • 1
    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). Commented Feb 28, 2012 at 10:56
  • 3
    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. Commented Feb 28, 2012 at 10:58

8 Answers 8


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.

  • 5
    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
    Commented Jun 16, 2018 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.

  • 2
    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. Commented Feb 28, 2012 at 10:55
  • the $a$$b$ trap can be side stepped using non-greedy regexs, Seamus' example is still tricky though
    – cmhughes
    Commented Feb 28, 2012 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 $...$


and similarly, matching for $$...$$:


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:


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.


The accepted answer does not seem to handle common multiline examples such as:


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.)


  • /\$\$/ 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.

  • 1
    Edit: To avoid the edge case $a$$b$, in the sed command replace the initial /\$\$/ with /^\$\$ *$/. Commented Mar 2, 2019 at 19:14

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


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 :

  \draw (0, 0) node (y1) {$\bullet$};
  \draw [->] (y1) to ($(y1)-(3, 0)$);

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.


If you want to frequently keep dedollarifying and dollarifying the latex, I've created a script for that:


It allows you to easily switch between the following LaTeX equation formats:

  • $...$ to \( ... \)
  • \( ... \) to $...$
  • $$...$$ to \[ ... \]
  • \[ ... \] to $$...$$


  • To convert LaTeX equations in a single file from $...$ and $$...$$ to \( ... \) and \[ ... \] format:
python converter.py dedollarify input.tex tex
  • To convert LaTeX equations in a single file from\( ... \) and \[ ... \] to $...$ and $$...$$ format:
python converter.py dollarify input.tex tex
  • To convert LaTeX equations in all .tex files within a directory and its subdirectories:
python converter.py dedollarify /path/to/directory md

I am an Emacs user. I can activate the electric-pair-mode (M-x electric-pair-mode), temporarely or for all sessions in the init file. Then when I type \( or \[ Emacs close the environment and place the point inside. It is only two keys to hit, it is very quick. It is a lot of different ways to do it, this one is my favourite. the drawback is when you want to type a single parenthesis, you have to delete the second one. You can do it immediately with C-d.

If it is a text previously entered, you can replace the $ and $$ by applying the command "query-replace-regexp" \(\$\{1,2\}) \([^$]+\)\1 → \,(if(eq (length \1) 1) (format "\\(%s\\)" \2) (format "\\[%s\\]" \2)))

  • I corrected the answer. The ability of emacs to include elisp code in the replacement string is a very powerful and useful feature.
    – gigiair
    Commented Mar 3, 2021 at 5:49

Using VS Code "find and replace"

You can use your editor for this.

To replace $math$ with \(math\):

  • Find: (\$)([^$\r]+)(\$)
  • Replace: \($2\)

To replace $$math$$ with \[math\]

  • Find: (\$\$)([^$\r]+)(\$\$)
  • Replace: \[$2\]

See regex101 for explanation.

The \r is for multiline math in VS Code.

Example usage in VS Code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .