[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: change $$ to equations with automatic labels
From: |
Arash Esbati |
Subject: |
Re: change $$ to equations with automatic labels |
Date: |
Thu, 13 Jan 2022 09:25:37 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 |
Hi Uwe,
Uwe Brauer <oub@mat.ucm.es> writes:
> I am using the following code
>
> (defun my-change-dollar-to-equation ()
> (interactive)
> (save-excursion
> (replace-regexp "\\$\\$\\(\\(.\\|\n\\)*?\\)\\$\\$"
> "\\\\begin{equation}\\1\\\\end{equation}")))
>
> But realized it would be nice to have for every changed equation, a
> label. I tend to use reftex-label for that purpose. But to include the
> automatic labeling in that simple function is beyond me, I am afraid.
Besides what Tassilo said, I also think that using regexp's here isn't
the best solution. My suggestion looks like this:
--8<---------------cut here---------------start------------->8---
(defun foo ()
(interactive)
(let ((p (point-marker))
(s (make-marker))
(e (make-marker)))
(goto-char (point-min))
(while (re-search-forward (concat "^" (regexp-quote "$$")) nil t)
;; Start doing something if we are in a math evn:
(when (texmathp)
(set-marker s (point))
;; Search for the closing $$ pair:
(re-search-forward (concat "^" (regexp-quote "$$")))
(set-marker e (point))
(delete-backward-char 2)
(insert "\\end{equation}")
(delete-horizontal-space)
(goto-char s)
(delete-backward-char 2)
(insert "\\begin{equation}")
(delete-horizontal-space)
;; Indent the environment:
(goto-char e)
(indent-according-to-mode)
(goto-char s)
(indent-according-to-mode)
;; Check if there is already a \label:
(unless (re-search-forward "\\\\label{" e t)
(goto-char s)
(forward-line)
(indent-according-to-mode)
(reftex-label)
(LaTeX-newline)
(indent-according-to-mode))))
(goto-char p)
(set-marker p nil)
(set-marker s nil)
(set-marker e nil)))
--8<---------------cut here---------------end--------------->8---
It can be polished to ask which environment to insert (with equation as
default) and ask to insert a label etc., but you can try it and see if
it works.
Best, Arash