Hi,
texmathp misreports math mode when a math-opening "$" is immediately
preceded by "\\" (a TeX line break). This is valid LaTeX and common in
tabular/array cells and multi-line TikZ node labels.
Tested against current git master (texmathp.el, copyright 1998-2024).
How to reproduce (emacs -Q, AUCTeX loaded):
Put point on the "b" of the second math group and run M-x texmathp.
\begin{tabular}{c}$a$\\$b$\end{tabular}
^ point here
Expected: math-mode is on (point is inside $...$).
Actual: texmathp returns nil ("math-mode is off").
Minimal trigger: in a buffer containing $a$\\$b$ , point on "b"
returns nil. The first group $a$ is detected correctly; everything
after the "\\$" has inverted parity.
Root cause:
texmathp counts unescaped "$" toggles via `texmathp-toggle-regexp',
built in `texmathp-compile' as
(concat "\\([^\\$]\\|\\`\\)" (regexp-opt togglers t))
The "[^\\$]" prefix is meant to skip an escaped dollar "\$", by
requiring the char before a counted "$" to be neither "\" nor "$".
But it also rejects a *genuine* math "$" that follows "\\": in
"$a$\\$b$" the opening "$" of the second group is preceded by the
second backslash of "\\", so it is never counted. The dollar parity
is then wrong for the rest of the paragraph, and texmathp returns the
inverted result on alternating math groups.
The correct rule is backslash *parity*: a "$" is a real toggle iff it
is preceded by an even number of backslashes ("\$" = literal, skip;
"\\$" = line break + math open, count).
Proposed fix (texmathp-compile):
- (concat "\\([^\\$]\\|\\`\\)"
+ (concat
"\\(\\(?:\\(?:[^\\$]\\|\\`\\)\\(?:\\\\\\\\\\)*\\)\\|\\(?:\\\\\\\\\\)+\\)"
(regexp-opt togglers t))
i.e. group 1 becomes "(anchor char or BOL) followed by zero or more
backslash pairs" OR "one or more backslash pairs" (the second branch
handles the case where the toggling "$" directly follows a previously
consumed "$", as in "$...$\\$..."). Group numbering is unchanged, so
the (match-string 2)/(match-beginning 2) references in `texmathp'
still refer to the dollar(s).
Verified with the patch:
$a$\\$b$ point on b -> t (was nil) FIXED
tabular cell above point on b -> t (was nil) FIXED
$x+y$ -> t (unchanged)
$$a+b$$ -> t (unchanged)
\$5 e testo -> nil (literal $, unchanged)
\$5 \$6 testo -> nil (between two \$, unchanged)
For full transparency: the root-cause analysis and the suggested fix
were developed with the help of an AI assistant (Claude Code). I have
independently reproduced the issue with "emacs -Q" and checked the
patch against the regression cases listed above. Please review the
patch on its own merits; feel free to adopt a different formulation if
you prefer.
Thanks,
Gabriele Nicolardi
Emacs : GNU Emacs 29.3 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.41, cairo version 1.18.0)
of 2026-02-03, modified by Debian
Package: 14.1.0
current state:
==============
(setq
window-system 'x
LaTeX-version "2e"
TeX-style-path '("~/.emacs.d/auctex/"
"/home/gabriele/.emacs.d/elpa/auctex-14.1.0/style"
"/home/gabriele/.emacs.d/auctex/auto"
"/home/gabriele/.emacs.d/auctex/style" "auto" "style")
TeX-auto-save nil
TeX-parse-self nil
TeX-master t
TeX-command-list '(("TeX" "%(PDF)%(tex) %(file-line-error)
%`%(extraopts) %S%(PDFout)%(mode)%' %(output-dir) %t"
TeX-run-TeX nil (plain-TeX-mode AmSTeX-mode Texinfo-mode)
:help "Run plain TeX")
("LaTeX" "%`%l%(mode)%' %T" TeX-run-TeX nil (LaTeX-mode
docTeX-mode) :help "Run LaTeX")
("Makeinfo" "makeinfo %(extraopts) %(o-dir) %t"
TeX-run-compile nil (Texinfo-mode) :help
"Run Makeinfo with Info output")
("Makeinfo HTML" "makeinfo %(extraopts) %(o-dir) --html %t"
TeX-run-compile nil
(Texinfo-mode) :help "Run Makeinfo with HTML output")
("AmSTeX" "amstex %(PDFout) %`%(extraopts) %S%(mode)%'
%(output-dir) %t" TeX-run-TeX nil
(AmSTeX-mode) :help "Run AMSTeX")
("ConTeXt" "%(cntxcom) --once %(extraopts) %(execopts)%t"
TeX-run-TeX nil (ConTeXt-mode)
:help "Run ConTeXt once")
("ConTeXt Full" "%(cntxcom) %(extraopts) %(execopts)%t"
TeX-run-TeX nil (ConTeXt-mode) :help
"Run ConTeXt until completion")
("BibTeX" "bibtex %(O?aux)" TeX-run-BibTeX nil
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode ConTeXt-mode) :help
"Run BibTeX")
("Biber" "biber %(output-dir) %s" TeX-run-Biber nil
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode) :help "Run Biber")
("Texindex" "texindex %s.??" TeX-run-command nil
(Texinfo-mode) :help "Run Texindex")
("Texi2dvi" "%(PDF)texi2dvi %t" TeX-run-command nil
(Texinfo-mode) :help
"Run Texi2dvi or Texi2pdf")
("View" "%V" TeX-run-discard-or-function t t :help "Run
Viewer")
("Print" "%p" TeX-run-command t t :help "Print the file")
("Queue" "%q" TeX-run-background nil t :help "View the
printer queue" :visible
TeX-queue-command)
("File" "%(o?)dvips %d -o %f " TeX-run-dvips t
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode) :help
"Generate PostScript file")
("Dvips" "%(o?)dvips %d -o %f " TeX-run-dvips nil
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode) :help
"Convert DVI file to PostScript")
("Dvipdfmx" "dvipdfmx -o %(O?pdf) %d" TeX-run-dvipdfmx nil
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode) :help
"Convert DVI file to PDF with dvipdfmx")
("Ps2pdf" "ps2pdf %f %(O?pdf)" TeX-run-ps2pdf nil
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode) :help
"Convert PostScript file to PDF")
("LaTeXMk"
"latexmk %(latexmk-out) %(file-line-error) %(output-dir)
%`%(extraopts) %S%(mode)%' %t"
TeX-run-TeX nil (LaTeX-mode docTeX-mode) :help "Run LaTeXMk")
("Glossaries" "makeglossaries %(d-dir) %s" TeX-run-command nil
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode) :help
"Run makeglossaries to create glossary file")
("Index" "makeindex %(O?idx)" TeX-run-index nil
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode) :help
"Run makeindex to create index file")
("upMendex" "upmendex %(O?idx)" TeX-run-index t
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode) :help
"Run upmendex to create index file")
("Xindy" "texindy %s" TeX-run-command nil
(plain-TeX-mode LaTeX-mode docTeX-mode AmSTeX-mode
Texinfo-mode) :help
"Run xindy to create index file")
("Check" "lacheck %s" TeX-run-compile nil (LaTeX-mode) :help
"Check LaTeX file for correctness")
("ChkTeX" "chktex -v6 %s" TeX-run-compile nil (LaTeX-mode)
:help
"Check LaTeX file for common mistakes")
("Spell" "(TeX-ispell-document \"\")" TeX-run-function nil
t :help "Spell-check the document")
("Clean" "TeX-clean" TeX-run-function nil t :help "Delete
generated intermediate files")
("Clean All" "(TeX-clean t)" TeX-run-function nil t :help
"Delete generated intermediate and output files")
("Other" "" TeX-run-command t t :help "Run an arbitrary
command"))
)--
SISSA Medialab
* Publishing *
*Gabriele Nicolardi*
*Production specialist*
*[email protected]*
------------------------------------------------------------------------
*Via Bonomea, 265 - 34136 Trieste, Italy - medialab.sissa.it
<http://medialab.sissa.it/en>*<http://medialab.sissa.it/en>
_______________________________________________
bug-auctex mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/bug-auctex