Subject: bug#78586: TeX-make-inline

From: "Paul D. Nelson" <[email-masked]>
Date: Sun, 25 May 2025 16:01:50 +0200

Hi Arash and all,

We had discussed in an earlier thread (bug#78006) the possibility of
adding some of my editing commands to AUCTeX (as standalone commands,
without default binds).  We could start with this one, discussed a bit
in that thread:

--8<---------------cut here---------------start------------->8---
(defcustom TeX-make-inline-delims '("$" . "$")
  "Delimiters for `TeX-make-inline' to surround inline LaTeX math."
  :type '(radio (const :tag "Dollar ($...$)" ("$" . "$"))
                (const :tag "Paren (\\(...\\))" ("\\(" . "\\)"))
                (cons :tag "Custom delimiters"
                      (string :tag "Left") (string :tag "Right"))))

(defun TeX-make-inline ()
  "Convert LaTeX display math environment at point to inline math.
Removes the enclosing math environment (such as \\[...\\] or
\\begin{equation}...\\end{equation}).  Replaces it with inline math
surrounded by `TeX-make-inline-delims', fitting the result onto one
line.  Leaves any trailing punctuation outside the math delimiters."
  (interactive)
  (when (texmathp)
    (when (fboundp 'preview-clearout-at-point)
      (preview-clearout-at-point))
    (save-excursion
      (let* ((env (car texmathp-why))
             (pos (cdr texmathp-why))
             (delims TeX-make-inline-delims))
        (cond
         ((member env '("\\(" "$")))
         ((member env '("\\[" "$$"))
          (goto-char pos)
          (delete-char 2)
          (let ((start (point))
                (end-delim (if (equal env "\\[") "\\]" "$$")))
            (search-forward end-delim)
            (delete-char -2)
            (TeX-make-inline--finalize-region start (point) delims)))
         (t
          (goto-char pos)
          (kill-line)
          (let ((start (point)))
            (search-forward (concat "\\end{" env "}"))
            (beginning-of-line)
            (kill-line)
            (TeX-make-inline--finalize-region start (point) delims))))))))

(defun TeX-make-inline--finalize-region (start end delims)
  "Finalize the inline conversion from START to END using DELIMS."
  (save-restriction
    (narrow-to-region start end)
    (whitespace-cleanup)

    (goto-char (point-min))
    (let ((re
           (concat "\\(?:"
                   (mapconcat #'identity
                              (if (boundp 'reftex-label-regexps)
                                  reftex-label-regexps
                                '("\\\\label{[^}]*"))
                              "\\|")
                   "\\)")))
      (while (re-search-forward re nil t)
        (replace-match "")))

    (goto-char (point-min))
    (while (looking-at "\\s-*$")
      (delete-line))
    (beginning-of-line-text)
    (delete-region (point-min) (point))

    (goto-char (point-max))
    (while (and (> (point) (point-min))
                (progn (forward-line -1)
                       (looking-at "\\s-*$")))
      (delete-line))
    (end-of-line)
    (skip-chars-backward " \t")
    (delete-region (point) (point-max))

    (goto-char (point-min))
    (insert (car delims))
    (goto-char (point-max))
    (insert "\n")
    (backward-char)

    (while (looking-back "[.,;:!?]" 5)
      (backward-char))
    (insert (cdr delims))

    (while (> (count-lines (point-min) (point-max)) 1)
      (join-line))))
--8<---------------cut here---------------end--------------->8---

I haven't prepared this as a patch because I'm not sure where to put it
or where to document it.  I looked around, and couldn't find a natural
place.  If no better idea comes to mind, then perhaps some new
"miscellaneous commands" section of latex.el, with a corresponding
section in the manual?  For what it's worth, there are a couple of other
editing-related commands I'd be inclined to propose.

Any feedback welcome.

Thanks, best,

Paul