# cycling through latex math mode and equation

Is there a way to cycle through $$x=1$$, $x=1$ and \begin{equation} x=1 \end{equation}

in any latex package? I find this function practical but I didn't know how to implement using elisp or maybe someone has already done that?

• I imagine I could write something to do this, but how would the function differentiate between math and some text that's simply in parens? Jul 13, 2015 at 10:15
• @GJStein sorry i forget to use double backslash to display backslash, maybe that is your question, because I never use dollar sign to start a math environment
– seb
Jul 13, 2015 at 10:27
• Are you using AucTeX? If not, I'd recommend it. If so, there are some built in commands which would help out here. Jul 13, 2015 at 13:58

This function will do what you want:

(defun cycle-texmath ()

(if (texmathp)
(save-excursion
(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)))
)))

'(progn
(define-key LaTeX-mode-map (kbd "C-c t") (lambda () (interactive) (cycle-texmath)))
))


It relies on the texmathp and texmathp-why functions which are built into AucTeX to determine if the point is within a math mode and which indicators are used to invoke it. I've bound it to C-c t, but feel free to replace that with whatever you'd like.

### EDIT:

After posing this as another question to handle the edge case of being within a text{} environment, the updated version of the code is here:

(defun cycle-texmath ()
(save-excursion
(while (member (TeX-current-macro) '("text"))
(backward-char))
(when (texmathp)
(let ((env (car texmathp-why)))
(cond
((string= env "equation")
(search-backward "\\begin{equation}")
(replace-match "\$" t t) (search-forward "\\end{equation}") (replace-match "\$" t t))
((string= env "\$") (search-backward "\\[") (replace-match "\$$" t t) (search-forward "\$") (replace-match "\$$" t t))
((string= env "\$$") (search-backward "\\(") (replace-match "\\begin{equation}" t t) (search-forward "\$$")
(replace-match "\\end{equation}" t t)))))))

• Thanks you very much, it works exactly the way I want, and beside I learn some basic elisp buffer manip by looking at the code.
– seb
Jul 13, 2015 at 14:53
• Well there is an issue with things like [xy=1 \text{ where (y=2)} ], but it is rare.
– seb
Jul 14, 2015 at 19:18
• I posed this edge case as a (SO question)[emacs.stackexchange.com/questions/13988/…, and I've modified the code to handle it in accordance with the response. Be sure to upvote the correct answer there. Jul 15, 2015 at 0:19
• thanks a lot, in fact, I didn't state the edge case correctly. Actually when I am writing latex, frequently I need to change an inline math environment into display math, since it looks better; or sometimes I need to make a ref to some displayed equation, that's why changing  into \begin{eq}etc is handy. So the question is, in a display math style like this $xy=1 \text{ where $$y=2$$}$, invoking cycle-math will produce $$xy=1 \text{ where \( y=2$$ \) then comes to \begin{eq} \text{ where $$y=2 \end{eq} }$$.
– seb
Jul 15, 2015 at 7:39