Recently I had to change notation in a LaTeX document: a graph I had named had now to be named . The post Replace something in math mode only by Ralf Angeli (from January 2007), helps with that. However the function
query-replace-regexp-eval mentioned there, is obsoleted since Emacs 22.1, (or so says
C-h f). The following works for me in Emacs 23.2 (requires the function
texmathp from AUCTeX):
M-x query-replace-regexp RET H RET \,(if (texmathp) "L" "H") RET
The idea behind it, is that the regular expression
H has to be replaced with the result of evaluating
(if (texmathp) "L" "H"), which is
L inside math mode and
H not inside math mode.
Note that after the last
RET, the first question can be answered
! to make all replacements without further questions.