replace often used Tex-Literals ($ and $$) for math regions into \( or \) and \[ or \]
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.
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.
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
.