1

So according to Why is \[ ... \] preferable to $$ ... $$? and Are \( and \) preferable to dollar signs for math mode? I should be using \(...\) instead of $...$ and \[...\] instead of $$...$$.

But what if I have a document where I've already used dollar signs a bit? How do I convert them? A simple find and replace won't work since:

  1. It won't care whether $$ is a beginning or ending; and
  2. I use the dollar sign in the text (to refer to the currency).

Does anyone have a neat tip for this?

Alex
  • 295
  • 1
  • 3
  • 12
  • 2
    I don't think you should worry about \( .. \) right now, you can live with $..$. About the display math with a basic regular expressions it could work, search \$\$([\s\S]+?)\$\$ and replace \[\1\]. – Manuel Feb 16 '16 at 23:40
  • @Manuel Not with sed ... – cfr Feb 17 '16 at 01:09
  • 3
    If you do try using regular expressions *back up first and do not destroy the back up at all soon*. Don't assume that if it mostly looks right, it is all right or that if this document looks right, that one probably is as well. I wouldn't do this at all except as I was editing unless I was absolutely confident I was not going to accidentally delete crucial bits of my document and turn others into garbage. $$...$$ has been deprecated for 20+ years. If nothing terrible has happened by now, it isn't likely to do so tomorrow. Just start using the new syntax as you edit ($...$ is OK.) – cfr Feb 17 '16 at 01:14
  • @cfr Yes, you are right. I just wrote something that has worked for me on an online regex and on my text editor. But of course there are more “versions”. – Manuel Feb 17 '16 at 01:18
  • 1
    @Manuel It is just that I can imagine somebody unawares copying the magic incantation and... poof! Who know? I don't think the results of running that with sed would be at all happy. – cfr Feb 17 '16 at 01:25
  • for easier solution checkout https://tex.stackexchange.com/a/697738/201795 – claws Oct 06 '23 at 03:38

2 Answers2

8

Single $ are fine and supported syntax for inline math and even $$ are mostly OK for display math. They are bad style but most of the time (if you are not using fleqn) they will give the same output as \[..\] so you can just change $$ to \[ as part of general editing as you edit the formulae, if you are not confident with regular expression replaces there is no pressing need to do a global edit of the document which may do more harm than good if you get the regular expression wrong. So try to use \[ going forward but don't lose sleep over $$ already in your document:-)

David Carlisle
  • 757,742
  • could you add your answer to the original, unduplicated question? this is really the right approach, imnsho. – jarnosc Feb 17 '16 at 20:49
6

This will depend a lot on which editor you use and the features it provides so I doubt there will be a single answer; however, if you editor has good regex support then this should be pretty easy.

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.

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.

JP-Ellis
  • 8,929
  • 2
  • 33
  • 49
  • Rather crucially, some regex implementations require \(...\) for grouping. Not escaping those could have disastrous results. (sed, for example.) – cfr Feb 17 '16 at 01:26
  • 1
    @cfr Yeah, I'm aware and I mention that in the answer (though I probably should make it clearer). There are so many regex implementation that I can't address them all – JP-Ellis Feb 17 '16 at 02:53
  • @JP-Ellis : you should copy your answer in this post. – Clément Feb 17 '16 at 03:26
  • @Clément Thanks for the heads up. I just copied the answer over. – JP-Ellis Feb 17 '16 at 03:36