auctex-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: change $$ to equations with automatic labels


From: Uwe Brauer
Subject: Re: change $$ to equations with automatic labels
Date: Thu, 13 Jan 2022 11:23:28 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

>>> "AE" == Arash Esbati <arash@gnu.org> writes:

Hi Arash, 
> 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:

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

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

Thanks, I just started using it and it seems to work flawlessly, and
that version is more than enough, I would not, at the moment, complicate
it more with questions to be asked. 
This is simply great. Thanks so much.

I will continue testing it and report back later. If it works as
expected I think it is worth including it. [1]

Uwe 

Footnotes:
[1]  oh maybe I am the only one who receives Latex documents with $$ $$ ☹️

Attachment: smime.p7s
Description: S/MIME cryptographic signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]