[英]the “org-preview-latex-fragment” command in emacs org-mode
[英]Emacs org-mode: increment equation numbers with latex preview
我注意到在 Org 模式下使用 Latex 預覽生成的等式編號不會增加到(1)以上。 有沒有辦法解決這個問題?
這是我的代碼:
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}
謝謝!
-亞當
最簡單的事情是明確地\\tag
方程。 缺點是沒有自動編號,優點是這也適用於 MathJax 的 html 導出。
即使沒有自動編號,您也可以通過將\\\\tag{[0-9]+}
替換為\\\\tag{\\,(1+ \\#)}
輕松地使用query-replace-regexp
更正編號。
你的例子看起來像
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}
我把我的評論作為答案,以便代碼格式正確。 這只是對托比亞斯回答的補充。
您可能希望使用自動重新編號
(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)))))
)
我在這里找到了一個有效的解決方案。
John Kitchin 通過\\setcounter{equation}{<num>}
在 org-mode 9.0 上做了一個技巧來解決這個問題。 所以在預覽片段時方程數會自動增加。 它在 org-mode 8.2.10 中也運行良好,這是我自己測試過的。
而到目前為止也有一個小錯誤。 如果一個方程與上面的另一個完全相同,就會有一個重復數。
總的來說,這確實是一個很好的解決方案。
解決方案源碼如下。 如果您希望在以后的會話中使用該功能,您可以將該塊添加到您的 emacs 配置文件中,例如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))
一旦您不想在當前會話中使用該功能,您可以通過以下代碼刪除advice
。 如果您不希望在以后的會話中使用它,只需在 emacs 配置文件中注釋或刪除上述塊即可。
(advice-remove 'org-create-formula-image #'org-renumber-environment)
我沒有對原始代碼做任何更改,只是在這里給出一些指導說明。 約翰·基欽 (John Kitchin) 的原創作品已根據知識共享署名-相同方式共享 4.0 國際許可協議獲得許可。
這是Alfred 的答案的一個細微變化,使更新部分成為本地。
(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))))))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.