My professor asked me to change all p,q variables to i,j variables in my paper. If I just search and replace it will have too many false positives, because it will highlight every word with letter p in it. I want to search only within math mode, but apparently my editor does not support this. Is there some lightweight editor, or maybe even web tool that can search and replace within only math mode? I want to do just this one task and get back to using my old editor.

I am looking for point-and-click solutions, not writing macros etc.

  • 2
    This will highly dependent on how you change to math-mode. Only with $ and $$ or every possible amsmath environment combination? RegExp could help. Related: Using Regexp to replace math expression inside Latex File – Qrrbrbirlbel Jan 10 '13 at 12:43
  • 1
    You should use an editor with regular expression capacity, like vim or Texmaker – Lionel MANSUY Jan 10 '13 at 12:43
  • or emacs :), which also has regexp's – zeroth Jan 10 '13 at 12:46
  • i only used $, $$, and \begin{} - \end{}. Do I really have to write regular expressions? Isn't there an editor that can recognize math mode itself? – Sunny88 Jan 10 '13 at 12:48
  • 1
    @zeroth Comparison of TeX editors lists quite a few editors with RegExp capacity. You won’t even need an only-TeX editor. Even Notepad++ would suffice. – Qrrbrbirlbel Jan 10 '13 at 12:48

An emacs+auctex solution found here :

M-x query-replace-regexp RET p RET \,(if (texmathp) "i" "p") RET
M-x query-replace-regexp RET q RET \,(if (texmathp) "j" "q") RET

It works in any environment AUCTeX will know to be in math-mode, for example $...$ or \[...\], or, if you have \usepackage{amsmath} in your preamble, \begin{align*}...\end{align*}, etc.

  • 2
    The answer is very nice, but I can't think to Emacs as a lightweight editor. ;-) – egreg Jan 10 '13 at 13:34
  • @egreg : Good point, my fanatism made me forget about that. I tend to consider it amongst "lightweight" options when I need to do something, because it is sooo reactive (especially here when compared to graphical editors). Also, it is sometimes pre-installed with the OS (not windows). – T. Verron Jan 10 '13 at 13:48
  • I have the same problem; I always use Emacs so this would be perfect, but this solution doesn't work for me. It simply tries to replace every occurrence of the letter, whether in math mode or not. – Liam Jun 24 '14 at 12:52
  • @Liam: This is normal: the command tries to replace all p's in the text. If the letter is in a math construct, it replaces it with i, otherwise it "replaces" it with a p (effectively not changing it). – T. Verron Jun 24 '14 at 13:11
  • 1
    This version only prompts for replacement in math mode: stackoverflow.com/questions/19845598/… – pavel Apr 22 '17 at 22:52

I once wrote a perl script to do just this sort of thing. Unfortunately, dollars are the main thing that it doesn't do. But I also wrote a perl script to convert dollars to their LaTeX counterparts. So you could run it through that script first, and then the replacement program.

  • debuck for converting dollars to \( ... \) and \[ ... \].
  • mathgrep for search-and-replace in mathematics.

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