Subject: bug#61410: texmathp and literal $

From: Arash Esbati <[email-masked]>
Date: Mon, 13 Feb 2023 15:11:55 +0100

Arash Esbati <[email-masked]> writes:

> My simple idea was to have an extra check at the end of `texmathp'
> itself in terms of: Pass the possible point for math-on to
> `LaTeX-verbatim-p' and see what it returs.

Following up myself, I was thinking about something like in the attached
patch.  Any comments welcome.

Best, Arash
diff --git a/texmathp.el b/texmathp.el
index ad008986..d4b0b007 100644
--- a/texmathp.el
+++ b/texmathp.el
@@ -283,7 +283,7 @@ See the variable `texmathp-tex-commands' about which commands are checked."
                        (if (eq major-mode 'doctex-mode)
                            "[\n\r]%*[ \t]*[\n\r]"
                          "[\n\r][ \t]*[\n\r]")
-                                          nil 1 texmathp-search-n-paragraphs)
+                       nil 1 texmathp-search-n-paragraphs)
                       (match-beginning 0)
                     (point-min))))
          (mac-match (texmathp-match-macro bound))
@@ -321,12 +321,24 @@ See the variable `texmathp-tex-commands' about which commands are checked."
 
     ;; Store info, show as message when interactive, and return
     (setq texmathp-why match)
-    (and (called-interactively-p 'any)
-         (message "math-mode is %s: %s begins at buffer position %d"
-                  (if math-on "on" "off")
-                  (or (car match) "new paragraph")
-                  (cdr match)))
-    (and math-on t)))
+    ;; Check also if the is match inside a verbatim construct and
+    ;; return immediately nil.  This relies on the function
+    ;; `LaTeX-verbatim-p'.  We add a check here in case this library
+    ;; is used stand-alone without latex.el provided by AUCTeX:
+    (if (and (fboundp 'LaTeX-verbatim-p)
+             (save-excursion (LaTeX-verbatim-p (cdr match))))
+        (progn
+          (setq texmathp-why `(nil . ,(cdr match)))
+          (when (called-interactively-p 'any)
+            (message "math-mode is off: Math command in verbatim construct at buffer position %d"
+                     (cdr match)))
+          nil)
+      (and (called-interactively-p 'any)
+           (message "math-mode is %s: %s begins at buffer position %d"
+                    (if math-on "on" "off")
+                    (or (car match) "new paragraph")
+                    (cdr match)))
+      (and math-on t))))
 
 (defun texmathp-match-environment (bound)
   "Find out if point is inside any of the math environments.