[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: change $$ to equations with automatic labels
From: |
Tassilo Horn |
Subject: |
Re: change $$ to equations with automatic labels |
Date: |
Wed, 12 Jan 2022 10:13:48 +0100 |
User-agent: |
mu4e 1.7.5; emacs 29.0.50 |
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}")))
I just want to mention that this command will shoot into your foot when
you execute it when point is inside a $$...$$ block.
> 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.
>
> Can any of the elips gurus help?
The `replace-regexp' docstring says:
--8<---------------cut here---------------start------------->8---
This function is usually the wrong thing to use in a Lisp program.
What you probably want is a loop like this:
(while (re-search-forward REGEXP nil t)
(replace-match TO-STRING nil nil))
which will run faster and will not set the mark or print anything.
--8<---------------cut here---------------end--------------->8---
That will also give you the chance to compose a custom TO-STRING
programmatically, e.g., something like
(concat "\\label{eqn:" my-num "}\n" (match-string 1))
where you'd have to increment my-num in every iteration.
Getting the initial value of my-num based on the labels already in the
document and ensuring that no duplicate labels are added is left as an
exercise to the reader. ;-)
You could skip that complication by using labels of the form
\label{eqn:dollar-converted:<NUM>} in the hope that "dollar-converted"
is very unlikely to appear in the wild.
Bye,
Tassilo