Better support for latex

This commit is contained in:
louie 2018-01-08 18:20:11 +08:00
parent ed80e1c25f
commit f23cf0994d
3 changed files with 63 additions and 27 deletions

View file

@ -15,8 +15,8 @@ anki-editor -- an Emacs package that helps you create Anki cards in Org-mode
type and tags type and tags
- Subheadings of a note heading are fields of its note type - Subheadings of a note heading are fields of its note type
- The contents of field headings will be converted to html by - The contents of field headings will be converted to html by
org-mode's html backend, with the latex fragment translated to org-mode's html backend, with the latex syntax translated to
the Anki syntax the Anki style
4. Command Cheat Sheet 4. Command Cheat Sheet
| Command | Default Keybinding | Description | | Command | Default Keybinding | Description |
|-----------------------------------------------+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |-----------------------------------------------+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

View file

@ -208,19 +208,19 @@ note type."
(org-html-convert-region-to-html)))) (org-html-convert-region-to-html))))
(defun anki-editor--replace-latex () (defun anki-editor--replace-latex ()
(let (object) (let (object type memo)
(while (setq object (org-element-map (while (setq object (org-element-map
(org-element-parse-buffer) (org-element-parse-buffer)
'latex-fragment 'identity nil t)) '(latex-fragment latex-environment) 'identity nil t))
(let (begin end latex hash)
(setq begin (org-element-property :begin object) (setq type (org-element-type object)
end (- (org-element-property :end object) (org-element-property :post-blank object)) memo (anki-editor--replace-node object
latex (delete-and-extract-region begin end)) (lambda (original)
(goto-char begin) (anki-editor--hash type
(insert (setq hash (anki-editor--hash 'latex-fragment latex))) original))))
(add-to-list 'anki-editor--replacement-records (add-to-list 'anki-editor--replacement-records
`(,hash . ((type . latex-fragment) `(,(cdr memo) . ((type . ,type)
(original . ,latex)))))))) (original . ,(car memo))))))))
(setq anki-editor--anki-latex-syntax-map (setq anki-editor--anki-latex-syntax-map
`((,(format "^%s" (regexp-quote "$$")) . "[$$]") `((,(format "^%s" (regexp-quote "$$")) . "[$$]")
@ -232,21 +232,28 @@ note type."
(,(format "^%s" (regexp-quote "\\[")) . "[$$]") (,(format "^%s" (regexp-quote "\\[")) . "[$$]")
(,(format "%s$" (regexp-quote "\\]")) . "[/$$]"))) (,(format "%s$" (regexp-quote "\\]")) . "[/$$]")))
(defun anki-editor--translate-latex-to-anki-syntax (latex) (defun anki-editor--wrap-latex (content)
(format "[latex]%s[/latex]" content))
(defun anki-editor--convert-latex-fragment (frag)
(let ((copy frag))
(dolist (map anki-editor--anki-latex-syntax-map) (dolist (map anki-editor--anki-latex-syntax-map)
(setq latex (replace-regexp-in-string (car map) (cdr map) latex t t))) (setq frag (replace-regexp-in-string (car map) (cdr map) frag t t)))
latex) (if (equal copy frag)
(anki-editor--wrap-latex frag)
frag)))
(defun anki-editor--translate-latex () (defun anki-editor--translate-latex ()
(dolist (stash anki-editor--replacement-records) (let (ele-data translated)
(dolist (record anki-editor--replacement-records)
(setq ele-data (cdr record))
(goto-char (point-min)) (goto-char (point-min))
(let ((hash (car stash)) (when (search-forward (car record) nil t)
(value (cdr stash))) (pcase (alist-get 'type ele-data)
(when (eq 'latex-fragment (alist-get 'type value)) ('latex-fragment (replace-match (anki-editor--convert-latex-fragment (alist-get 'original ele-data)) t t))
(when (search-forward hash nil t) ('latex-environment (replace-match (anki-editor--wrap-latex (alist-get 'original ele-data)) t t)))
(replace-match (anki-editor--translate-latex-to-anki-syntax (add-to-list 'translated record)))
(alist-get 'original value)) (setq anki-editor--replacement-records (cl-set-difference anki-editor--replacement-records translated))))
t t))))))
;; Utilities ;; Utilities
@ -264,6 +271,15 @@ note type."
(org-set-tags-to tags) (org-set-tags-to tags)
(org-fix-tags-on-the-fly)) (org-fix-tags-on-the-fly))
(defun anki-editor--replace-node (node replacer)
(let* ((begin (org-element-property :begin node))
(end (- (org-element-property :end node) (org-element-property :post-blank node)))
(original (delete-and-extract-region begin end))
(replacement (funcall replacer original)))
(goto-char begin)
(insert replacement)
(cons original replacement)))
;; anki-connect ;; anki-connect
(defun anki-editor--anki-connect-invoke (action version &optional params success) (defun anki-editor--anki-connect-invoke (action version &optional params success)

View file

@ -51,7 +51,7 @@
* Mathematics :deck: * Mathematics :deck:
** Item :note: ** Item1 :note:
:PROPERTIES: :PROPERTIES:
:ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_TYPE: Cloze
:END: :END:
@ -61,3 +61,23 @@
The dot product of two vectors is {{c1::$|\alpha| \cdot |\beta| \cos{\varphi}$}} The dot product of two vectors is {{c1::$|\alpha| \cdot |\beta| \cos{\varphi}$}}
*** Extra *** Extra
** Item2 :note:
:PROPERTIES:
:ANKI_NOTE_TYPE: Basic
:END:
*** Front
Given two vectors:
\begin{equation*}
\alpha = \{a_1, a_2, a_3\}, \beta = \{b_1, b_2, b_3\}
\end{equation*}
What's the result of $\alpha \cdot \beta$ ?
*** Back
\[a_1b_1 + a_2b_2 + a_3b_3\]