8

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

4 Answers 4

5

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}
2
  • You may want to automate the renumbering using
    – M. Toya
    Apr 16, 2015 at 13:49
  • 1
    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))))) )
    – M. Toya
    Apr 16, 2015 at 13:49
5

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)))))
  )
1
  • 2
    Thanks Alfred! This is fantastic. I wonder if this can be added as a hook that runs each time org latex preview is run? Also it seems like this could be refined further to search for any line number within an equation that does not have the \nonumber tag? This way, the user does not have to manually add a \tag{1} at all...?
    – Adam
    Apr 19, 2015 at 13:07
1

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.

1
  • Just to add that as written here and on Kitchin's page, this function uses some common-lisp features, and resulted in me getting Symbols function definition is void messages. To solve this, I changed loop to cl-loop and incf to cl-incf, and this appears to give the desired results. See this answer for some more detail on this. Apr 28, 2022 at 7:37
0

Here's a slight variation on Alfred's answer that makes the update section-local.

(defun update-eqn-numbers-in-section ()
  (interactive)
  (let ((beg (if (org-before-first-heading-p) (point-min)
           (save-excursion
         (org-with-limited-levels (org-back-to-heading t) (point)))))
    (end (org-with-limited-levels (org-entry-end-position)))
        (count 1))
    (save-excursion
      (goto-char beg)
      (while (re-search-forward "\\tag{\\([0-9]+\\)}" end t)
        (replace-match (format "%d" count) nil nil nil 1)
        (setq count (1+ count))))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.