Subject: Re: bug#78586: TeX-make-inline

From: Ikumi Keita <[email-masked]>
Date: Thu, 05 Jun 2025 23:51:42 +0900

Hi Paul,

>>>>> "Paul D. Nelson" <[email-masked]> writes:
> Suppose the buffer contains:
> We have
> \begin{equation*}
>   x + y = z.
> \end{equation*}

> With point on line 2, join-line yields a buffer with first line:
> We have \begin{equation*}
> The motivation for the slightly convoluted code is that, when point is
> at beginning of line, (save-excursion (join-line)) places point just
> before " \begin" (rather than just before "\begin", as one might have
> expected).

Thanks for explanation. Hmm, it's indeed annoying. :-(

> I'll trust you on this one -- I'm regrettably ignorant of TeX outside a
> limited practical subset of LaTeX.

To have a minimum acquaintance with docTeX document, visit
latex/preview.dtx in AUCTeX repository. It's basically full of
"commented" text with leading "%" sign at the beginning of line.

> I've added some FIXME comments, hopefully as intended.

Thank you, those are enough.

+(defun LaTeX-modify-math (new-type)
+  "Modify the current math construct to NEW-TYPE.
+
+Interactively, prompt for NEW-TYPE from a list of inline math
+delimiters (\"$\", \"\\(\"), display math delimiters (\"$$\",
+\"\\=\\[\") and valid LaTeX environments (\"equation\", ...).
+
+Non-interactively, NEW-TYPE must be either
+- a string specifying the target delimiter or environment name, or
+- a cons cell ((OPEN . CLOSE) . INLINE), where OPEN and CLOSE are
+  delimiters and INLINE is non-nil if the math construct is to be
+  understood as inline.
+
+The function converts the math construct at point (inline, display, or
+environment) to the specified NEW-TYPE, preserving the content.  If
+point is not in a math construct, signal an error.  Clears any active
+previews at point before modification.
+
+Does not support modifying macro-based constructs such as \\ensuremath."
+  ;; FIXME: this function may not work correctly in docTeX
+  (interactive
+   (let* ((type (progn (texmathp) (car texmathp-why)))
+          (tbl (append '("$" "\\(" "$$" "\\[")
+                       (LaTeX--math-environment-list))))

It seems `let' is enough. (I don't know the C implementation of `let'
and `let*' of elisp; maybe is there any guideline which recommends
`let*' over `let', due to the implementation detail, when both can do
the job equally?)

+     (barf-if-buffer-read-only)
+     (unless type (user-error "Not inside math"))
+     (LaTeX--closing type) ;; Check for errors.
+     (list (completing-read
+            (format "Convert %s → " type) tbl nil t nil nil
+            type))))
+  (let ((new-open (if (stringp new-type)
+                      new-type
+                    (caar new-type)))
+        (new-close (if (stringp new-type)
+                       (LaTeX--closing new-type)
+                     (cdar new-type)))
+        (new-inline (if (stringp new-type)
+                        (member new-type '("$" "\\("))
+                      (cdr new-type))))
+    (when (fboundp 'preview-clearout-at-point)
+      (preview-clearout-at-point))
+    (unless (called-interactively-p 'any)
+      (unless (texmathp) (error "Not inside math")))
+    (let ((type (car texmathp-why))
+          (math-start (cdr texmathp-why))
+          (pos (point-marker)))
+      (set-marker-insertion-type pos
+                                 (not
+                                  (and
+                                   (< (point) (point-max))
+                                   (save-excursion
+                                     (forward-char)
+                                     (not (texmathp))))))
+      (goto-char math-start)
+      (let* ((open (if (member type '("\\(" "$" "\\[" "$$"))
+                       type
+                     (concat TeX-esc "begin" TeX-grop type TeX-grcl)))
+             (close (LaTeX--closing type)))

Same comment with respect to `let' and `let*'.

+        (if (or (not (stringp new-type))
+                (member new-open '("$" "\\(" "\\[" "$$")))
+            ;; Conversion to inline or non-environment display.
+            (let* ((inline (member type '("$" "\\("))))

Same comment.

> There remains the question of where in the manual to document
> LaTeX-modify-math and whether to illustrate it via examples like in my
> earlier email.

AUCTeX manual already has "Mathematics" node, so I think you should add
explanation there.
Examples are welcomed in the manual. Users who don't know elisp well
will know how to use the prepaired function.

> (defun my-LaTeX-make-brackets ()
>   "Convert math construct at point to \"\\=\\[..\\=\\]\"."
                                                  ^^^
I'd suggest to remove quotation for \]. In contrast to \[, \] will be
displayed literally without \=, though extra \= is harmless.

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