Subject: bug#78586: TeX-make-inline

From: "Paul D. Nelson" <[email-masked]>
Date: Tue, 27 May 2025 16:58:58 +0200

Hi Arash, thanks for your feedback.  I put the updated draft at the end
of this message (to be converted into a patch once one of us thinks of a
good place to put it), and attach some tests.  Paul

--8<---------------cut here---------------start------------->8---
(defun LaTeX-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 surrounded by `TeX-electric-math' if non-nil, or \"$..$\",
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 (or TeX-electric-math '("$" . "$"))))
        (cond
         ((member env '("\\(" "$")))
         ((member env '("\\[" "$$"))
          (goto-char pos)
          (when (looking-back "\n[[:space:]]*")
            (forward-char 2)
            (save-excursion (join-line))
            (forward-char -2))
          (delete-char 2)
          (let ((start (point))
                (end-delim (if (equal env "\\[") "\\]" "$$")))
            (search-forward end-delim)
            (delete-char -2)
            (if (looking-back "\n[[:space:]]*")
                (goto-char (match-beginning 0)))
            (LaTeX--make-inline-finalize-region start (point) delims)))
         (t
          (goto-char pos)
          (kill-whole-line)
          (let ((start (point)))
            (search-forward (concat "\\end{" env "}"))
            (beginning-of-line)
            (kill-whole-line)
            (backward-char)
            (LaTeX--make-inline-finalize-region start (point) delims))))))))

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

    (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))

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

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