Rename a math variable in LaTeX with vim regex -
i need change name of math variable across big latex document, of vim regular expressions, struggling learn.
for example, want change variable labelled 't' 's', example
\begin{equation} f(x) \leq \sqrt{t(t+1)} \end{equation}
should turn into
\begin{equation} f(x) \leq \sqrt{s(s+1)} \end{equation}
for need search character 't' somewhere between \begin{equation}
, matching \end{equation}
possibly several line breaks away, character 't' should not part of keyword such \sqrt
i have tried
%s/\\begin{equation}\_.\{-}\(\\\a*\)\@!\zst\ze\_.\{-}\\end{equation}/s/g
but doesn't work should. besides, pattern matches keywords \sqrt
, don't understand why. however, am aware sandwiching match between 2 \_.\{-}
doing, won't yield desired result, since won't match multiple occurrences of character 't'.
i prefer pure vim regexp solution, if possible.
note:
since use different environments besides equation
, such align
, eqnarray
, multline
, etc., better insert or pattern such as
/\\begin{\(equation\|align\|eqnarray\|multline\)}
and find matching \end{…}
. how accomplish that?
let's build pieces. first, @rbernabe suggests, use start-of-word , end-of-word patterns find "t" entire word:
/\<t\>
next, replace such on current line, use
:s/\<t\>/s/g
now, position cursor on \begin{equation}
line. range .,/\\end{equation}/
specifies lines , including end of equation environment, so
:.,/\\end{equation}/s/\<t\>/s/g
will replace single-word "t"s "s" in range. in order repeat command equation environments in file, use :g/\\begin{equation}/{command}
:
:g/\\begin{equation}/.,/\\end{equation}/s/\<t\>/s/g
if want play vimgolf, . optional. finally, if want handle multiple environments, afraid there no way tie variable environment name in \begin{...}
closing \end{...}
. if none of equation-like environments nested, can use
:g/\\begin{\(equation\|align\|eqnarray\|multline\)}/.,/\\end{\(equation\|align\|eqnarray\|multline\)}/s/\<t\>/s/g
you can use explicit loop, might if there nested environments:
:let envs = ['equation', 'align', 'eqnarray', 'multline'] :for env in envs : execute 'g/\\begin{' . env . '}/.,/\\end{' . env . '}/s/\<t\>/s/g' :endfor
of course, regular expressions not substitute real parsing. long actual latex code similar sample, should work, run trouble if ever have like
\end{equation} ordinary text t in it.
Comments
Post a Comment