Replacing characters in math mode only

Recently I had to change notation in a LaTeX document: a graph I had named H had now to be named L. 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.

This entry was posted in Emacs and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s