I have a long tex document and I would like to perform a search for finding some string inside math environments. Is there a search function already provided by auctex to do this? Other solutions are welcomed.

(I found https://stackoverflow.com/questions/19845598/emacs-regex-replacing-a-string-inside-a-latex-equation on a similar topic for replacing a string inside math environments).

  • I'm not sure I understood the question, and probably "forward search" in the context of LaTeX means something different from what you mean. Do you want to search for a particular string and want to be sure it is inside math mode?
    – giordano
    Commented Sep 27, 2016 at 9:40
  • I edited the question, 'forward search' may be confused with inverse/forward search between output and tex source. I just mean an ordinary search as you wrote.
    – Name
    Commented Sep 27, 2016 at 10:01
  • I'd have suggested to use texmathp (to test whether the string is inside math mode) together with standard Emacs searching facilities, but this possibility is already mentioned in the question on stackoverflow you linked. What's wrong with it?
    – giordano
    Commented Sep 27, 2016 at 10:09
  • I thought since I am interested to perform only a search (and not find-replace as in the link), there exists perhaps an easier way to do it. Anyway I am happy with the solution given in the link.
    – Name
    Commented Sep 27, 2016 at 10:47

1 Answer 1


Bundled with AUCTeX there is the texmathp utility (which, however, is independent from the TeX modes provided by AUCTeX and can be used without it), which allows you to test whether point is inside a mathematical mode or not.

The simplest suggestion I can give you is to combine texmathp with search-forward, here is a possible implementation:

(defun mg-search-in-math (string)
  (interactive "MSearch string: ")
  (while (null (and (search-forward string)
  • Great. Is there an easy way to do this with incremental search (isearch)?
    – Zuza
    Commented Mar 10, 2020 at 19:02
  • 1
    Not sure, but maybe this is worth a new question on its own?
    – giordano
    Commented Mar 11, 2020 at 9:21
  • Thanks for the suggestion. I agree---I created a new question here: emacs.stackexchange.com/questions/56102/…
    – Zuza
    Commented Mar 12, 2020 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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