Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert strings $foo$ to \(foo\) and $$bar$$ to \[bar\]

Tags:

vim

latex

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

like image 756
Niels Avatar asked Feb 20 '23 21:02

Niels


1 Answers

Use

:%s/\V$$\v(\_.{-})\V$$/\\[\1\\]/g
:%s/\v\$([^$]+)\$/\\(\1\\)/g

Differences from @kirilloid answer are the following:

  1. Using very magic (\v, disables need to escape most meta-characters) and very nomagic (everything but backslash have their literal meanings) modes for readability
  2. 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 *.
  3. 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 :%.
like image 173
ZyX Avatar answered Feb 23 '23 09:02

ZyX