1

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?

6
  • @Name The question involved rotating through the "commands" which signify that one is inside a math environment: \[, \( and \begin{equation'. Commented Jul 14, 2015 at 23:16
  • Is the environment\text{} the sole exception inside? Commented Jul 14, 2015 at 23:19
  • @Name I believe that it is. 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). 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. Commented Jul 15, 2015 at 13:04

1 Answer 1

2

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))) ))))) 
1
  • I've moved the save-excursion function to include the while loop, but otherwise, this code is exactly what I was looking for. Thanks! Commented Jul 15, 2015 at 0:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.