GNU bug report logs - #78586
TeX-make-inline

Previous Next

Package: auctex;

Reported by: "Paul D. Nelson" <ultrono <at> gmail.com>

Date: Sun, 25 May 2025 14:03:02 UTC

Severity: normal

Done: "Paul D. Nelson" <ultrono <at> gmail.com>

Full log


View this message in rfc822 format

From: "Paul D. Nelson" <ultrono <at> gmail.com>
To: Arash Esbati <arash <at> gnu.org>
Cc: 78586 <at> debbugs.gnu.org
Subject: bug#78586: TeX-make-inline
Date: Tue, 27 May 2025 16:58:58 +0200
[Message part 1 (text/plain, inline)]
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

[latex-make-inline-test.el (application/emacs-lisp, attachment)]
[Message part 3 (text/plain, inline)]

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

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.