GNU bug report logs - #78006
Improving `TeX-electric-math' behavior

Previous Next

Package: auctex;

Reported by: Arash Esbati <arash <at> gnu.org>

Date: Wed, 23 Apr 2025 07:27:01 UTC

Severity: normal

Done: Arash Esbati <arash <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 78006 in the body.
You can then email your comments to 78006 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-auctex <at> gnu.org:
bug#78006; Package auctex. (Wed, 23 Apr 2025 07:27:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Arash Esbati <arash <at> gnu.org>:
New bug report received and forwarded. Copy sent to bug-auctex <at> gnu.org. (Wed, 23 Apr 2025 07:27:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: "auctex-bugs" <bug-auctex <at> gnu.org>
Subject: Improving `TeX-electric-math' behavior
Date: Wed, 23 Apr 2025 09:25:25 +0200
Hi all,

currently, we have this in AUCTeX manual[1]:

  User Option: TeX-electric-math

  In addition, when the variable is non-nil and there is an active
  region outside math mode, typing $ will put around the active region
  symbols for opening and closing inline equation and keep the region
  active, leaving point after the closing symbol.  By pressing
  repeatedly $ while the region is active you can toggle between an
  inline equation, a display equation, and no equation.  To be precise,
  ‘$...$’ is replaced by ‘$$...$$’, whereas ‘\(...\)’ is replaced by
  ‘\[...\]’.

Using $$...$$ in LaTeX is discouraged, so I think AUCTeX shouldn't
promote this as well, so I suggest this change to
`TeX-insert-dollar-electric-region':

--8<---------------cut here---------------start------------->8---
diff --git a/tex.el b/tex.el
index 3d28b2c6..5d184942 100644
--- a/tex.el
+++ b/tex.el
@@ -6188,10 +6188,13 @@ See `TeX-electric-math'."
              (re-search-forward "\\=\\$\\$\\([^z-a]*\\)\\$\\$" (mark) t)))
     (replace-match "\\1" t)
     (set-mark (match-beginning 0)))
-   ;; $...$ to $$...$$
+   ;; $...$ to $$...$$ or \[...\] dep. on mode:
    ((and (eq last-command #'TeX-insert-dollar)
          (re-search-forward "\\=\\$\\([^z-a]*\\)\\$" (mark) t))
-    (replace-match "$$\\1$$" t)
+    (replace-match (if (memq major-mode '(LaTeX-mode docTeX-mode))
+                       "\\\\[\\1\\\\]"
+                     "$$\\1$$")
+                   t)
     (set-mark (match-beginning 0)))
    ;; \(...\) to \[...\]
    ((and (eq last-command #'TeX-insert-dollar)
--8<---------------cut here---------------end--------------->8---

I think this needs some more massaging in order to make it work for
ConTeXt users as well, but this should get us going for other modes.

What do people think?  If Ok, I would install it and update the manual
accordingly.

Best, Arash

Footnotes:
[1]  https://elpa.gnu.org/packages/doc/auctex.html#index-TeX_002delectric_002dmath




Information forwarded to bug-auctex <at> gnu.org:
bug#78006; Package auctex. (Thu, 24 Apr 2025 09:56:01 GMT) Full text and rfc822 format available.

Message #8 received at 78006 <at> debbugs.gnu.org (full text, mbox):

From: "Paul D. Nelson" <ultrono <at> gmail.com>
To: Arash Esbati <arash <at> gnu.org>
Cc: 78006 <at> debbugs.gnu.org
Subject: Re: bug#78006: Improving `TeX-electric-math' behavior
Date: Thu, 24 Apr 2025 11:55:44 +0200
>   User Option: TeX-electric-math
>
>   In addition, when the variable is non-nil and there is an active
>   region outside math mode, typing $ will put around the active region
>   symbols for opening and closing inline equation and keep the region
>   active, leaving point after the closing symbol.  By pressing
>   repeatedly $ while the region is active you can toggle between an
>   inline equation, a display equation, and no equation.  To be precise,
>   ‘$...$’ is replaced by ‘$$...$$’, whereas ‘\(...\)’ is replaced by
>   ‘\[...\]’.
>
> Using $$...$$ in LaTeX is discouraged, so I think AUCTeX shouldn't
> promote this as well, so I suggest this change to
> `TeX-insert-dollar-electric-region':

Looks good to me overall.

One counterintuitive edge case: if I mark x and hit $ repeatedly, then I
get $x$ -> \[x\] -> x -> etc.  On the other hand, if I mark $x$ and hit
$, then I get $$x$$ -> $x$ -> etc.

I also wonder then whether TeX-insert-dollar (x2) should then similarly
insert $$|$$ or \[|\] according to mode?

Tangentially, I wonder whether this feature might be more useful if it
worked not just when operating on a region for the first time, but more
generally on any math region, as a way to cycle between inline and
displayed math.  I've had "make-inline" and "make-displayed" functions
in my config for a long time now that I've tried unsuccessfully to think
of an elegant way to package and contribute.




Information forwarded to bug-auctex <at> gnu.org:
bug#78006; Package auctex. (Fri, 25 Apr 2025 10:39:02 GMT) Full text and rfc822 format available.

Message #11 received at 78006 <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: "Paul D. Nelson" <ultrono <at> gmail.com>
Cc: 78006 <at> debbugs.gnu.org
Subject: Re: bug#78006: Improving `TeX-electric-math' behavior
Date: Fri, 25 Apr 2025 12:37:49 +0200
"Paul D. Nelson" <ultrono <at> gmail.com> writes:

> Looks good to me overall.

Thanks for checking

> One counterintuitive edge case: if I mark x and hit $ repeatedly, then I
> get $x$ -> \[x\] -> x -> etc.  On the other hand, if I mark $x$ and hit
> $, then I get $$x$$ -> $x$ -> etc.

I think this is by design.  In `TeX-insert-dollar-electric-region', we
have this test:

 ((and (eq last-command #'TeX-insert-dollar)
         (re-search-forward ...))
  ...

> I also wonder then whether TeX-insert-dollar (x2) should then similarly
> insert $$|$$ or \[|\] according to mode?

AUCTeX currently enters \[|\] when you hit \[ so I think the above would
be redundant.

> Tangentially, I wonder whether this feature might be more useful if it
> worked not just when operating on a region for the first time, but
> more generally on any math region, as a way to cycle between inline
> and displayed math.  I've had "make-inline" and "make-displayed"
> functions in my config for a long time now that I've tried
> unsuccessfully to think of an elegant way to package and contribute.

Hmm, maybe we can add them to AUCTeX itself.  Do you want to show the
code?

Best, Arash




Information forwarded to bug-auctex <at> gnu.org:
bug#78006; Package auctex. (Fri, 25 Apr 2025 11:41:01 GMT) Full text and rfc822 format available.

Message #14 received at 78006 <at> debbugs.gnu.org (full text, mbox):

From: "Paul D. Nelson" <ultrono <at> gmail.com>
To: Arash Esbati <arash <at> gnu.org>
Cc: 78006 <at> debbugs.gnu.org
Subject: Re: bug#78006: Improving `TeX-electric-math' behavior
Date: Fri, 25 Apr 2025 13:40:29 +0200
>> One counterintuitive edge case: if I mark x and hit $ repeatedly, then I
>> get $x$ -> \[x\] -> x -> etc.  On the other hand, if I mark $x$ and hit
>> $, then I get $$x$$ -> $x$ -> etc.
>
> I think this is by design.  In `TeX-insert-dollar-electric-region', we
> have this test:
>
>  ((and (eq last-command #'TeX-insert-dollar)
>          (re-search-forward ...))
>   ...
>

Right, I guess I was asking if it might make sense to eliminate the
"last-command" test, so that the command operates in a broader set of
scenarios.  I don't have strong feelings on this, since I haven't used
this cycling feature before.

>> Tangentially, I wonder whether this feature might be more useful if it
>> worked not just when operating on a region for the first time, but
>> more generally on any math region, as a way to cycle between inline
>> and displayed math.  I've had "make-inline" and "make-displayed"
>> functions in my config for a long time now that I've tried
>> unsuccessfully to think of an elegant way to package and contribute.
>
> Hmm, maybe we can add them to AUCTeX itself.  Do you want to show the
> code?


Sure.  There's a command for making a displayed equation inline, and
another command that, among other things, makes a displayed equation
inline.  I bind these to "C-c i" and "C-c e".  The "among other things"
should probably be stripped out before sending to AUCTeX, but OK,
resisting the urge to polish for now:

(defcustom czm-tex-edit-punctuation-string
  "[\\.|,|;|!|?]"
  "Regexp for matching punctuation characters."
  :group 'czm-tex-edit
  :type 'regexp)

;;;###autoload
(defun czm-tex-edit-make-equation-inline ()
  "Convert LaTeX equation environment at point to inlined math.
Format LaTeX environment at point by surrounding the math
environment with dollar signs, removing any leading or trailing
text."
  (interactive)
  (when (texmathp)
    (preview-clearout-at-point)
    (let ((cur (point-marker)) beg end)
      (save-excursion
        (LaTeX-find-matching-end)
        (setq end (line-beginning-position 2))
        (goto-char cur)
        (LaTeX-find-matching-begin)
        (setq beg (point)))
      (save-restriction
        (narrow-to-region beg end)
        (goto-char (point-min))
        (kill-line 1)
        (beginning-of-line-text)
        (delete-region (point-min) (point))
        (goto-char (point-max))
        (forward-line -2)
        (end-of-line)
        (delete-region (point) (point-max))
        (whitespace-cleanup)
        (goto-char (point-min))
        (insert "$")
        (goto-char (point-max))
        (insert "\n")
        (backward-char)
        (while (looking-back czm-tex-edit-punctuation-string 5)
          (backward-char))
        (insert "$")
        (while (> (count-lines (point-min) (point-max)) 1)
          (join-line))
        (current-buffer))
      (join-line)
      (goto-char cur))))

(defun czm-tex-edit-add-label ()
  "Add LaTeX label to matching \\begin{} at environment beginning.
Also, save label to kill ring as an \\eqref{} command."
  (interactive)
  (save-excursion
    (LaTeX-find-matching-begin)
    (end-of-line)
    (funcall czm-tex-edit-label-function)
    (let ((end (point)))
      (search-backward "{")
      (let ((beg (point)))
        (kill-new (concat "\\eqref" (buffer-substring beg end)))))))

(defun czm-tex-edit--handle-env (env pos num-chars search-str numbered)
  "Helper function for `czm-tex-edit-make-equation-numbered'.

Remove part of the environment string and insert a numbered or
unnumbered LaTeX equation environment at POS.  ENV specifies the
name of the environment to match, NUM-CHARS specifies the number
of characters to delete, SEARCH-STR specifies the string to
search for and NUMBERED determines if the equation environment
should be numbered."
  (goto-char pos)
  (delete-char num-chars)
  (when (or (equal env "equation") (equal env "equation*"))
    (kill-line))
  (push-mark (save-excursion
               (search-forward search-str)
               (delete-region (match-beginning 0) (match-end 0))
               (when (equal env "$")
                 (while (looking-at-p czm-tex-edit-punctuation-string)
                   (forward-char)))
               (point)))
  (activate-mark)
  (LaTeX-insert-environment (if numbered "equation" "equation*"))
  (when numbered
    (czm-tex-edit-add-label)))

(defun czm-tex-edit-make-equation-numbered ()
  "Toggle whether an equation is numbered."
  (interactive)
  (save-excursion
    (when (texmathp)
      (preview-clearout-at-point)
      (let ((env (car texmathp-why))
            (pos (cdr texmathp-why)))
        (cond
         ((or
           (equal env '"\\[") (equal env '"$$"))
          (czm-tex-edit--handle-env env pos 2 (if (equal env "$$") "$$" "\\]") nil))
         ((equal env "$")
          (czm-tex-edit--handle-env env pos 1 "$" nil))
         ((equal env "equation*")
          (czm-tex-edit--handle-env env pos 0 "\\end{equation*}" t))
         ((equal env "equation")
          (czm-tex-edit--handle-env env pos 0 "\\end{equation}" nil)))))))

[This last function is poorly named and documented -- apologies.]

These are part of https://github.com/ultronozm/czm-tex-edit.el.

One thought: AUCTeX has "C-u C-c C-e" for switching the type of an
environment.  Perhaps the same command could be made to work in
top-level $...$ regions, using something like the above
"make-equation-numbered" as the implementation.  Conversely, perhaps
$...$ could be a hard-coded option for environment type after "C-u C-c
C-e", with something like the above "make-inline" as implementation.
The point of this suggestion is just to get around the scarcity of
keybinds and integrate with existing commands.

Any thoughts welcome!




Information forwarded to bug-auctex <at> gnu.org:
bug#78006; Package auctex. (Fri, 02 May 2025 07:15:02 GMT) Full text and rfc822 format available.

Message #17 received at 78006 <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: "Paul D. Nelson" <ultrono <at> gmail.com>
Cc: 78006 <at> debbugs.gnu.org
Subject: Re: bug#78006: Improving `TeX-electric-math' behavior
Date: Fri, 02 May 2025 09:14:08 +0200
"Paul D. Nelson" <ultrono <at> gmail.com> writes:

> Right, I guess I was asking if it might make sense to eliminate the
> "last-command" test, so that the command operates in a broader set of
> scenarios.  I don't have strong feelings on this, since I haven't used
> this cycling feature before.

I don't use it either, but I'm reluctant to make this change since it
was there forever.  I think I install the change I proposed and we'll
wait and see what real users say about it.

> Sure.  There's a command for making a displayed equation inline, and
> another command that, among other things, makes a displayed equation
> inline.  I bind these to "C-c i" and "C-c e".

Thanks for sharing.  I think we should move your code to another bug
report and discuss it there.  I will have to take a closer look, but I
have 2 minor comments below:

> The "among other things" should probably be stripped out before
> sending to AUCTeX, but OK, resisting the urge to polish for now:
>
> (defcustom czm-tex-edit-punctuation-string
>   "[\\.|,|;|!|?]"

Isn't "[.,;!?]" enough, presuming you don't want to search for a literal
backslash?

>   "Regexp for matching punctuation characters."
>   :group 'czm-tex-edit
>   :type 'regexp)
>
> ;;;###autoload
> (defun czm-tex-edit-make-equation-inline ()
>   "Convert LaTeX equation environment at point to inlined math.
> Format LaTeX environment at point by surrounding the math
> environment with dollar signs, removing any leading or trailing
> text."

I think this will break if the equation environment has a \label{eq:x}?

> One thought: AUCTeX has "C-u C-c C-e" for switching the type of an
> environment.  Perhaps the same command could be made to work in
> top-level $...$ regions, using something like the above
> "make-equation-numbered" as the implementation.  Conversely, perhaps
> $...$ could be a hard-coded option for environment type after "C-u C-c
> C-e", with something like the above "make-inline" as implementation.
> The point of this suggestion is just to get around the scarcity of
> keybinds and integrate with existing commands.
>
> Any thoughts welcome!

I see your point, but OTOH, do we have to bind every command to a key
binding?  I think for commands not often used people can do 'M-x foo' or
bind the command to a sequence of their choice, as you did.

Best, Arash




Information forwarded to bug-auctex <at> gnu.org:
bug#78006; Package auctex. (Sat, 03 May 2025 04:28:02 GMT) Full text and rfc822 format available.

Message #20 received at 78006 <at> debbugs.gnu.org (full text, mbox):

From: Ikumi Keita <ikumi <at> ikumi.que.jp>
To: Arash Esbati <arash <at> gnu.org>
Cc: 78006 <at> debbugs.gnu.org
Subject: Re: bug#78006: Improving `TeX-electric-math' behavior
Date: Sat, 03 May 2025 13:27:13 +0900
Hi Arash,

>>>>> Arash Esbati <arash <at> gnu.org> writes:
> Using $$...$$ in LaTeX is discouraged, so I think AUCTeX shouldn't
> promote this as well, so I suggest this change to
> `TeX-insert-dollar-electric-region':

I have one minor comment about your proposal.

> +    (replace-match (if (memq major-mode '(LaTeX-mode docTeX-mode))

I think we should use `derived-mode-p' here instead of comparing
`major-mode' directly.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine
#Gaza #StopMassiveKilling #CeasefireNOW




Information forwarded to bug-auctex <at> gnu.org:
bug#78006; Package auctex. (Sat, 03 May 2025 08:32:02 GMT) Full text and rfc822 format available.

Message #23 received at 78006 <at> debbugs.gnu.org (full text, mbox):

From: "Paul D. Nelson" <ultrono <at> gmail.com>
To: Arash Esbati <arash <at> gnu.org>
Cc: 78006 <at> debbugs.gnu.org
Subject: Re: bug#78006: Improving `TeX-electric-math' behavior
Date: Sat, 03 May 2025 10:31:38 +0200
Hi Arash,

Thanks, sounds good.  I'll plan to send these commands as a separate bug
(and have no further comments on the current bug).  Some responses:

>> ;;;###autoload
>> (defun czm-tex-edit-make-equation-inline ()
>>   "Convert LaTeX equation environment at point to inlined math.
>> Format LaTeX environment at point by surrounding the math
>> environment with dollar signs, removing any leading or trailing
>> text."
>
> I think this will break if the equation environment has a \label{eq:x}?

It will indeed break (i.e., put the \label{...} inside the $...$) if the
\label{...} does not occur on the same line as \begin{...}.  Perhaps we
could kill any labels first.

A related issue is ampersands alignment characters (e.g., in align
environments) that should not exist when we convert to inline, but
ampersands occur in other ways (e.g., matrices), so it seems tricky to
get this right.  Personally, I use the command only when the displayed
expression is simple enough, which restricts the possible edge cases.

Thanks, best,

Paul




Information forwarded to bug-auctex <at> gnu.org:
bug#78006; Package auctex. (Sat, 03 May 2025 08:39:02 GMT) Full text and rfc822 format available.

Message #26 received at 78006 <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: Ikumi Keita <ikumi <at> ikumi.que.jp>
Cc: 78006 <at> debbugs.gnu.org
Subject: Re: bug#78006: Improving `TeX-electric-math' behavior
Date: Sat, 03 May 2025 10:37:47 +0200
Hi Keita,

Ikumi Keita <ikumi <at> ikumi.que.jp> writes:

> I think we should use `derived-mode-p' here instead of comparing
> `major-mode' directly.

Thanks for your response.  Something like this?

--8<---------------cut here---------------start------------->8---
diff --git a/tex.el b/tex.el
index 3d28b2c6..0d47988c 100644
--- a/tex.el
+++ b/tex.el
@@ -6188,10 +6188,13 @@ See `TeX-electric-math'."
              (re-search-forward "\\=\\$\\$\\([^z-a]*\\)\\$\\$" (mark) t)))
     (replace-match "\\1" t)
     (set-mark (match-beginning 0)))
-   ;; $...$ to $$...$$
+   ;; $...$ to $$...$$ or \[...\] dep. on mode:
    ((and (eq last-command #'TeX-insert-dollar)
          (re-search-forward "\\=\\$\\([^z-a]*\\)\\$" (mark) t))
-    (replace-match "$$\\1$$" t)
+    (replace-match (if (derived-mode-p '(LaTeX-mode docTeX-mode))
+                       "\\\\[\\1\\\\]"
+                     "$$\\1$$")
+                   t)
     (set-mark (match-beginning 0)))
    ;; \(...\) to \[...\]
    ((and (eq last-command #'TeX-insert-dollar)
--8<---------------cut here---------------end--------------->8---

Best, Arash




Information forwarded to bug-auctex <at> gnu.org:
bug#78006; Package auctex. (Sat, 03 May 2025 13:14:01 GMT) Full text and rfc822 format available.

Message #29 received at 78006 <at> debbugs.gnu.org (full text, mbox):

From: Keita Ikumi <ikumikeita <at> jcom.home.ne.jp>
To: Arash Esbati <arash <at> gnu.org>, Ikumi Keita <ikumi <at> ikumi.que.jp>
Cc: 78006 <at> debbugs.gnu.org
Subject: Re: bug#78006: Improving `TeX-electric-math' behavior
Date: Sat, 3 May 2025 22:13:00 +0900 (JST)
[Message part 1 (text/plain, inline)]
>
>
> ------ Original Message ------
>  土曜日, 2025/5/3  17:37, Arash Esbati<arash <at> gnu.org> によって書かれました:
>
> Hi Keita,
>
> Ikumi Keita <ikumi <at> ikumi.que.jp> writes:
>
>>  I think we should use `derived-mode-p' here instead of comparing
>>  `major-mode' directly.
>
> Thanks for your response.  Something like this?
>
> --8<---------------cut here---------------start------------->8---
> diff --git a/tex.el b/tex.el
> index 3d28b2c6..0d47988c 100644
> --- a/tex.el
> +++ b/tex.el
> @@ -6188,10 +6188,13 @@ See `TeX-electric-math'."
> 
>               (re-search-forward "\\=\\$\\$\\([^z-a]*\\)\\$\\$" (mark) t)))
>      (replace-match "\\1" t)
>      (set-mark (match-beginning 0)))
> -   ;; $...$ to $$...$$
> +   ;; $...$ to $$...$$ or \[...\] dep. on mode:
>     ((and (eq last-command #'TeX-insert-dollar)
>           (re-search-forward "\\=\\$\\([^z-a]*\\)\\$" (mark) t))
> -    (replace-match "$$\\1$$" t)
> +    (replace-match (if (derived-mode-p '(LaTeX-mode docTeX-mode))
Well, since docTeX-mode is already derived from 
LaTeX-mode, we can just do
(derived-mode-p 'LaTeX-mode)
:-)
> +                       "\\\\[\\1\\\\]"
> +                     "$$\\1$$")
> +                   t)
>      (set-mark (match-beginning 0)))
>     ;; \(...\) to \[...\]
>     ((and (eq last-command #'TeX-insert-dollar)
> --8<---------------cut here---------------end--------------->8---
>
> Best, Arash
>
[Message part 2 (text/html, inline)]

Reply sent to Arash Esbati <arash <at> gnu.org>:
You have taken responsibility. (Sat, 03 May 2025 20:44:01 GMT) Full text and rfc822 format available.

Notification sent to Arash Esbati <arash <at> gnu.org>:
bug acknowledged by developer. (Sat, 03 May 2025 20:44:02 GMT) Full text and rfc822 format available.

Message #34 received at 78006-done <at> debbugs.gnu.org (full text, mbox):

From: Arash Esbati <arash <at> gnu.org>
To: Ikumi Keita <ikumi <at> ikumi.que.jp>
Cc: 78006-done <at> debbugs.gnu.org
Subject: Re: bug#78006: Improving `TeX-electric-math' behavior
Date: Sat, 03 May 2025 22:43:22 +0200
Keita Ikumi <ikumikeita <at> jcom.home.ne.jp> writes:

> Well, since docTeX-mode is already derived from 
> LaTeX-mode, we can just do
> (derived-mode-p 'LaTeX-mode)
> :-)

Ah, thanks.  I installed that change and therefore closing this report.
Thanks to you all for your comments.

Best, Arash




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 01 Jun 2025 11:24:15 GMT) Full text and rfc822 format available.

This bug report was last modified 3 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.