# Change escape behavior when inside LaTeX equations

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 Jul 14 '15 at 23:16 • Is the environment\text{} the sole exception inside? – Name Jul 14 '15 at 23:19 • @Name I believe that it is. – GJStein Jul 14 '15 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 Jul 15 '15 at 7:55 • @giordano It does return nil when it's inside \text, that was the edge case I wanted to avoid. – GJStein Jul 15 '15 at 13:04 ## 1 Answer 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 "\$$") (replace-match "\\[" t t) (search-forward "\$$") (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 "\$$" t t) (search-forward "\$$") (replace-match "\$$" 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 Jul 15 '15 at 0:16