Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs org-mode: increment equation numbers with latex preview

I've noticed that equation numbers, generated with latex preview in Org Mode do not increment above (1). Is there some 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

like image 313
Adam Avatar asked Sep 28 '14 23:09

Adam


3 Answers

The easiest thing is to explicitely \tag the equations. The disadvantage is that there is no automatic numbering the advantage is that this also works for html export with 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 would 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}
like image 55
Tobias Avatar answered Nov 15 '22 08:11

Tobias


I put my comment as an answer so that the code is properly formated. This is only a complement to Tobias' answer.

You may want to automate the renumbering using

(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)))))
  )
like image 37
M. Toya Avatar answered Nov 15 '22 10:11

M. Toya


I find an effective solution here.

John Kitchin did a trick on org-mode 9.0 by \setcounter{equation}{<num>} as a workaround for the problem. So the equation numbers will be automatically increased when preview fragments. It also works well in org-mode 8.2.10, which is tested by myself.

Whereas there is also a little bug so far. If an equation is absolutely same as another above, there will be a repeat number.

Overall, it's really a good solution.

Solution source code is as follows. If you want the feature in the future session, you can add the block to your emacs configuration file like 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))

Once you do not want the feature in the current session, you can remove the advice by the following code. If you do not want it in the future session, just comment or remove the above block in the emacs configuration file.

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

I did not make any change to the original code, just give some guide instructions here. The original work by John Kitchin is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

like image 45
husky Avatar answered Nov 15 '22 10:11

husky