I recently wrote a function which could cycle through LaTeX math delimiters. It relies on the texmathp function, built into AucTeX, which will return t if the point is inside an equation region. In addition, I use the texmathp-why function in order to determine what the math environment is (which was important to answering the posed question).

However, it was pointed out by the asker that the function does not work when the point is inside \text{} inside the equation (which I assume is intentional behavior within AucTeX). Is there a simple way to avoid this escape, or should I write a work around in the function to account for this?

  • @Name The question involved rotating through the "commands" which signify that one is inside a math environment: \[, \( and \begin{equation'.
    – GJStein
    Commented Jul 14, 2015 at 23:16
  • Is the environment\text{} the sole exception inside?
    – Name
    Commented Jul 14, 2015 at 23:19
  • @Name I believe that it is.
    – GJStein
    Commented Jul 14, 2015 at 23:21
  • Which version of AUCTeX are you using? For me (using AUCTeX 11.88), texmathp returns nil if point is inside the argument of \text and other similar macros (see texmathp-tex-commands-default variable).
    – giordano
    Commented Jul 15, 2015 at 7:55
  • @giordano It does return nil when it's inside \text, that was the edge case I wanted to avoid.
    – GJStein
    Commented Jul 15, 2015 at 13:04

1 Answer 1


I think adding the line (while (member (TeX-current-macro) '("text")) (backward-char)) to your code does the job:

(defun cycle-texmath ()
(save-excursion (progn
(while (member (TeX-current-macro) '("text")) (backward-char))
(if (texmathp) (progn
      (setq env (car texmathp-why))
      (if (string= env "equation") (progn 
        (search-backward "\\begin{equation}") (replace-match "\\[" t t)
        (search-forward "\\end{equation}") (replace-match "\\]" t t)))
      (if (string= env "\\[") (progn 
        (search-backward "\\[") (replace-match "\\(" t t)
        (search-forward "\\]") (replace-match "\\)" t t)))
      (if (string= env "\\(") (progn 
        (search-backward "\\(") (replace-match "\\begin{equation}" t t)
        (search-forward "\\)") (replace-match "\\end{equation}" t t)))
  • I've moved the save-excursion function to include the while loop, but otherwise, this code is exactly what I was looking for. Thanks!
    – GJStein
    Commented Jul 15, 2015 at 0:16

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.