How to convert strings $foo$ to \(foo\) and $$bar$$ to \[bar\]
latex, vim
Solution
Use
:%s/\V$$\v(\_.{-})\V$$/\\[\1\\]/g
:%s/\v\$([^$]+)\$/\\(\1\\)/g
Differences from @kirilloid answer are the following:
- Using very magic (`\v`, disables need to escape most meta-characters) and very nomagic (everything but backslash have their literal meanings) modes for readability
- Ability to cope with multi-line `$$` strings (`\_` adds newline to `.`, `\_.` is the only construct that really means any character, `.` does not include newline). `{-}` (`\{-}` in magic, nomagic and very nomagic modes) is the non-greedy variant of `*`.
- There may be any character between these strings (`$`: any but newline), but it requires for you to ensure that there `$$` always starts outline formula and `$` always starts inline one. You may want to restrict the replace to the lines where this is true by either replacing `%` in front of command with `{first_line_number},{last_line_number}` or selecting them visually, typing `:` (`'<,'>` will appear at the start of command line) and proceeding to type `s/...` command without leading `:%`.
Problem
Everything is in the title. I have many LaTeX files written with the rather obsolete syntax `$foo$` and `$$bar$$` that I wish to convert respectively in `\\(foo\\)` and `\\[bar\\]`. I am using vim so I guess a regular expression will do but a script would also be perfectly fine. I have been looking around but without success. Edit: following kirilloid's useful reply, I wish to mention to I would like to use this also in the case `foo` and `bar` are not only words, but expressions containing spaces (but no `$`s obviously).