PMM
PMM

Reputation: 43

Latex/VIM : change all $$ .. $$ to \begin{equation} .... \end{equation}

I received a 100 page Latex document with all equations between $$...$$ and I need to change them all to \begin{equation}...\end{equation}

Is there some clever way to do this in Latex (some built in functionality) or in Vim?

In Vim I am thinking

Either: a way to replace abc with dbf where all letters stand for patterns.

Or: replace all $$ with \begin{equation}, then replace every second \begin{equation} with \end{equation}

I'm not sure how to accomplish either though.

Upvotes: 4

Views: 732

Answers (2)

rkta
rkta

Reputation: 4589

Using Vim:

Use a recording(:help q), e.g. qe:

  • go to the first $$ you want to change
  • replace with \begin{equation}
  • search for the next $$
  • replace with \end{equation}
  • search for next $$
  • stop recording

Reapply recording with @, @e in my example.
@ takes a count, see :help @

Upvotes: 1

Fabius Wiesner
Fabius Wiesner

Reputation: 926

:%s/\$\$\(\_.\{-}\)\$\$/\\begin{equation}\1\\end{equation}/g

It searches $$ followed by everything (.) including newline (\_), non greedy (\{-}), followed by another $$. On the right part, \1 means "put the backreference" number 1, i.e. the first \(...\) on the left.

This works with equations on single or multiple lines.

Upvotes: 4

Related Questions