# Emacs and latex math delimiters

The forward-sexp command in EMACS can be used to find the $ that closes an inline equation: if the point is before the $ that opens the equation, then forward-sexp moves the point after the closing $. This does the correct thing with nested equations: e.g., if the point is before $a + \text{b $c$}$, then forward-sexp moves the point past the entire equation. Is there a way to find the \) or \] that closes a $$ and $ with auctex, perhaps with texmathp or font-latex-match-math-env? This seems to be a bit tricky in EMACS as there is no way to store multi-char delimiters in a syntax table. - I would not bother: ..., as opposed to \(...$$, is reliably robust in macro arguments and when written to files (i.e., table of contents), and \begin{equation*}...\end{equation*} (using amsmath package) is quite better visible. – user9588 Apr 19 '12 at 16:28 – N.N. Apr 21 '12 at 11:37 As I already said: I would not bother with $$ $$ \[ $. The theoretic gain of matched delimiters is not worth the price in editing support and loss of robustness in LaTeX. – user9588 Apr 22 '12 at 15:41 @DavidKastrup But many people use fixltx2e which redefines them as robust. – N.N. Apr 22 '12 at 16:25 @N.N. I finally saw this discussion today; it is precisely the lack of support for matching these delimiters in EMACS that has kept me using $ in LaTeX. I have some macros to quickly switch equations between inline and displayed that make critical use of forward-sexp. –  Samuel Isaacson May 13 '12 at 18:05

I have put together a function that might do what you want. It searches for the next LaTeX math delimiter – $$, $$, $ or $ – while ignoring comments and if is an opening delimiter it tries to find its matching delimiter.

(defun forward-latex-math ()
"Move forward across the next LaTeX equation. It is meant work like forward-sexp' but for LaTeX math delimiters."
(interactive)
(let ((count 1))
;; Search for either of the following  
(re-search-forward-ignore-TeX-comments "\\\$$\\|\\\$$\\|\\\\\$\\|\\\$")
(cond
;; If the search hits $$((looking-back "\\\\(" (- (point) 2)) (while (< 0 count) ;; Search for delimiters inside the equation (re-search-forward-ignore-TeX-comments "\\\\(\\|\\\$$")
(if (looking-back "\\\$$" (- (point) 2)) (setq count (1+ count)) ; If start of a nested level (setq count (1- count)))) ; If end of a nested level ;; Find the matching$$
(re-search-forward "\\\\)" (eobp) t count))
;; If the search hits $((looking-back "\\\\\\[" (- (point) 2)) (while (< 0 count) ;; Search for delimiters inside the equation (re-search-forward-ignore-TeX-comments "\\\\\\[\\|\\\$")
(if (looking-back "\\\\\$" (- (point) 2)) (setq count (1+ count)) ; If start of a nested level (setq count (1- count)))) ; If end of a nested level ;; Find the matching$
(re-search-forward "\\\\]" (eobp) t count)))))

"Search for REGEXP and ignore TeX comments. Used by forward-latex-math'."
(re-search-forward regexp (eobp) t)
;; If in comment search to after it
(while (TeX-in-comment)
(forward-line)
(re-search-forward regexp (eobp) t)))


To use it place it in your .emacs and run it by M-xforward-latex-math. If you want to use it often you might want to bind it to key.

Since I have just started to learn Lisp I am sure this code can improved in many ways. Please comment if you have any suggestions.

-
This doesn't deal with commented-out delimiters correctly. It's not clear to me that these delimiters can be properly matched without reimplementing LaTeX. –  Samuel Isaacson May 13 '12 at 17:52
@SamuelIsaacson How should it deal with commented out delimiters? –  N.N. May 13 '12 at 17:59
This code matches $$%$$ as a single unit, but it shouldn't; it should continue searching on the next line to find the closing \). –  Samuel Isaacson May 13 '12 at 18:12
@SamuelIsaacson I have updated the function to ignore comments. Let me know if it does not work as you want. –  N.N. May 15 '12 at 15:44