Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 13 down vote accepted

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{equation}\\|\\\\begin{align}\\)" nil 1)
    (query-replace-regexp a  b t  (point) 
              (progn (re-search-forward 
                  "\\(\\\\)\\|\\\\\\]\\|[^\\\\]\$\$?\\|\\\\end{equation}\\|\\\\end{align}\\)" nil 1) (point)))))

this looks for $ \( \[ \begin{equation} \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  


\begin{equation}
i   i   aib
\end{equation}


\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  


\begin{equation}
x   x   aib
\end{equation}


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

share|improve this answer
    
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 \begin{equation} and then query-replaced all the way to a \end{equation} 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
show 2 more comments

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.