Emacs org-mode: number of latex extension numbers with latex preview

I noticed that the numbers of equations created with a latex preview in Org mode do not increase above (1). Is there any way to fix this?

Here is my code:

A numbered display equation: \begin{equation} y=\int_{-\infty}^{\infty}\frac{1}{1+x}dx\label{eq1} \end{equation} A second numbered equation: \begin{equation} z=qr^2-2\label{eq2} \end{equation} 

Thanks!

-Adam

+5
source share
3 answers

The simplest is obviously the \tag equation. The disadvantage is that there is no automatic numbering, the advantage is that it also works for exporting html using MathJax.

Even if there is no automatic numbering, you can easily correct the numbering with query-replace-regexp by replacing \\tag{[0-9]+} with \\tag{\,(1+ \#)} .

Your example will look like

 A numbered display equation: \begin{equation} y=\int_{-\infty}^{\infty}\frac{1}{1+x}dx\label{eq1}\tag{1} \end{equation} A second numbered equation: \begin{equation} z=qr^2-2\label{eq2}\tag{2} \end{equation} 
+4
source

I put my comment as an answer so that the code is correctly generated. This is just a complement to Tobias answer.

You can automate renumbering with

 (defun update-tag () (interactive) (save-excursion (goto-char (point-min)) (let ((count 1)) (while (re-search-forward "\\tag{\\([0-9]+\\)}" nil t) (replace-match (format "%d" count) nil nil nil 1) (setq count (1+ count))))) ) 
+2
source

I found an effective solution here .

John Kitchen did the trick in org-mode 9.0 on \setcounter{equation}{<num>} as a workaround for the problem. Thus, the number of equations will be automatically increased with preview fragments. It also works well in org-mode 8.2.10, which is being tested by me.

At the same time, while there is a small error. If the equation is exactly the same as the previous one, there will be a number of repetitions.

All in all, this is a really good solution.

The source code for the solution is as follows. If you want to use this function in a future session, you can add a block to the emacs configuration file, such as init.el

 (defun org-renumber-environment (orig-func &rest args) (let ((results '()) (counter -1) (numberp)) (setq results (loop for (begin . env) in (org-element-map (org-element-parse-buffer) 'latex-environment (lambda (env) (cons (org-element-property :begin env) (org-element-property :value env)))) collect (cond ((and (string-match "\\\\begin{equation}" env) (not (string-match "\\\\tag{" env))) (incf counter) (cons begin counter)) ((string-match "\\\\begin{align}" env) (prog2 (incf counter) (cons begin counter) (with-temp-buffer (insert env) (goto-char (point-min)) ;; \\ is used for a new line. Each one leads to a number (incf counter (count-matches "\\\\$")) ;; unless there are nonumbers. (goto-char (point-min)) (decf counter (count-matches "\\nonumber"))))) (t (cons begin nil))))) (when (setq numberp (cdr (assoc (point) results))) (setf (car args) (concat (format "\\setcounter{equation}{%s}\n" numberp) (car args))))) (apply orig-func args)) 

If you do not need a function in the current session, you can remove advice using the following code. If you do not want this in a future session, just comment or delete the above block in the emacs configuration file.

 (advice-remove 'org-create-formula-image #'org-renumber-environment) 

I did not make any changes to the source code, just give a few instructions. John Kitchin's original work is licensed under a Creative Commons Attribution-ShareAlike 4.0 license.

+1
source

Source: https://habr.com/ru/post/1203573/


All Articles