# Editor that replaces only text inside math environments

I'm writing a document where I had to change all instances of the variable i to x and the document is already considerably long. Is there a editor that will allow me to do this, or in general, replace text inside math environments?

-

emacs can do this sort of thing fairly easily

(defun change-mathvar (a b)
(interactive "sfrom: \nsto: ")
(beginning-of-buffer)
(while (re-search-forward
"\\\\\(\\|\\\\\$\\|[^\\\$\\?\\|\\\\\|\\\\begin{align}\" nil 1) (query-replace-regexp a b t (point) (progn (re-search-forward "\$$\\\$$\\|\\\\\\]\\|[^\\\\]\\?\\|\\\\\|\\\\end{align}\\)" nil 1) (point)))))


this looks for    \begin{align} as math-start. Other environments can be added. Starting from a document such as \documentclass{article} \begin{document} i i aib \[i i aib i i aib $$i i aib$$ \end{document}  then executing M-x change-mathvar the editor will prompt for the old and new names then do a query-replace of the variable names to produce: \documentclass{article} \begin{document} i i aib $x x aib$ i i aib $$x x aib$$ \end{document}  Note it hasn't changed anything out of math and it only changes i where it appears as a complete word, not aib. If you want aib to change as well change the t in the code to nil to make a non-delimited match. - I like this, but one should be aware that it fails on unusual nested constructs such as \( \sum_\text{\( p such that $$2p+1$$ is prime} i^p \) when wishing to change p to q. – Andrew Swann Jul 31 '12 at 15:21 Yes a tradeoff between being smarter and being more complicated. In particular the fact that it doesn't choose the closing delimiter based on which opening delimiter it found makes it easier to trip it up in this way, but simplifies the emacs lisp quite a bit. When doing this though I usually try to make sure the outer loop is "good enough" to cut out the main blocks, and then rely on the fact that the inner-loop is a query-replace so you get to choose and weed out bad cases. (which is why I'd rather do it in emacs than construct a sed expression and edit the whole file without intervention – David Carlisle Jul 31 '12 at 15:26 I completely agree. Note however in the example I gave, the emacs function only detects the first math p; so you don't get the chance to query replace the other math p's even on a second run. – Andrew Swann Jul 31 '12 at 15:45 yes to make that work you'd need a version that found the outer environment (say $$and then query-replaced all the way to a$$ the version I had would stop at the first \) – David Carlisle Jul 31 '12 at 15:55 @DavidCarlisle To get a more robust code (with only false positives, no false negatives), there is no need to distinguish the various opening or closing delimiters: the regexp in the (progn ...) construction should count opening and closing delimiters and stop when it finds one extra close. How hard would it be? – Bruno Le Floch Aug 12 '13 at 18:12 I once wrote a perl script to do just that. It's called MathGrep and can be obtained from http://www.math.ntnu.no/~stacey/HowDidIDoThat/LaTeX/mathgrep.html. The biggest caveat is that it doesn't recognise dollars (see Are $$and$$ preferable to?), but then I also wrote a script to convert all dollars to $$...$$ and $...$ as well which is at http://www.math.ntnu.no/~stacey/HowDidIDoThat/LaTeX/debuck.html.

I'm struggling to think of a use-case where this would beat David's Emacs script though ...

-