Fix preview latex

* lisp/org.el (org-toggle-latex-fragment): Fix previewing nested
  environments.
(org-format-latex): Change signature.  Rewrite using Elements.

* lisp/ox-html (org-html-format-latex):
* lisp/ox-odt (org-odt--translate-latex-fragments): Apply signature
  change.

Reported-by: Andreas Leha <andreas.leha@med.uni-goettingen.de>

<http://permalink.gmane.org/gmane.emacs.orgmode/92142>
This commit is contained in:
Nicolas Goaziou 2014-10-28 23:51:38 +01:00
parent b8d8078368
commit 8daf4a89f1
3 changed files with 152 additions and 146 deletions

View File

@ -18642,162 +18642,168 @@ the cursor is before the first headline,
display all fragments in the buffer. display all fragments in the buffer.
The images can be removed again with \\[org-toggle-latex-fragment]." The images can be removed again with \\[org-toggle-latex-fragment]."
(interactive "P") (interactive "P")
(unless buffer-file-name (unless (buffer-file-name (buffer-base-buffer))
(user-error "Can't preview LaTeX fragment in a non-file buffer")) (user-error "Can't preview LaTeX fragment in a non-file buffer"))
(if org-latex-fragment-image-overlays (if org-latex-fragment-image-overlays
(progn (org-remove-latex-fragment-image-overlays) (progn (org-remove-latex-fragment-image-overlays)
(message "LaTeX fragments images removed")) (message "LaTeX fragments images removed"))
(when (display-graphic-p) (when (display-graphic-p)
(org-remove-latex-fragment-image-overlays) (org-remove-latex-fragment-image-overlays)
(save-excursion (org-with-wide-buffer
(save-restriction (let (beg end msg)
(let (beg end at msg) (cond
(cond ((equal subtree '(16))
((or (equal subtree '(16)) (setq beg (point-min) end (point-max)
(not (save-excursion msg "Creating images for buffer...%s"))
(re-search-backward org-outline-regexp-bol nil t)))) ((equal subtree '(4))
(setq beg (point-min) end (point-max) (org-back-to-heading)
msg "Creating images for buffer...%s")) (setq beg (point) end (org-end-of-subtree t)
((equal subtree '(4)) msg "Creating images for subtree...%s"))
(org-back-to-heading) ((let ((context (org-element-context)))
(setq beg (point) end (org-end-of-subtree t) (when (memq (org-element-type context)
msg "Creating images for subtree...%s")) '(latex-environment latex-fragment))
(t (setq beg (org-element-property :begin context)
(if (setq at (org-inside-LaTeX-fragment-p)) end (org-element-property :end context)
(goto-char (max (point-min) (- (cdr at) 2))) msg "Creating image...%s"))))
(org-back-to-heading)) ((org-before-first-heading-p)
(setq beg (point) end (progn (outline-next-heading) (point)) (setq beg (point-min) end (point-max)
msg (if at "Creating image...%s" msg "Creating images for buffer...%s"))
"Creating images for entry...%s")))) (t
(message msg "") (org-back-to-heading)
(narrow-to-region beg end) (setq beg (point) end (progn (outline-next-heading) (point))
(goto-char beg) msg "Creating images for entry...%s")))
(org-format-latex (message msg "")
(concat org-latex-preview-ltxpng-directory (narrow-to-region beg end)
(file-name-sans-extension (goto-char beg)
(file-name-nondirectory buffer-file-name))) (org-format-latex
default-directory 'overlays msg at 'forbuffer (concat org-latex-preview-ltxpng-directory
org-latex-create-formula-image-program) (file-name-sans-extension
(message msg "done. Use `C-c C-x C-l' to remove images."))))))) (file-name-nondirectory
(buffer-file-name (buffer-base-buffer)))))
default-directory 'overlays msg 'forbuffer
org-latex-create-formula-image-program)
(message msg "done. Use `C-c C-x C-l' to remove images."))))))
(defun org-format-latex (prefix &optional dir overlays msg at (defun org-format-latex
forbuffer processing-type) (prefix &optional dir overlays msg forbuffer processing-type)
"Replace LaTeX fragments with links to an image, and produce images. "Replace LaTeX fragments with links to an image, and produce images.
Some of the options can be changed using the variable Some of the options can be changed using the variable
`org-format-latex-options'." `org-format-latex-options'."
(if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache)) (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
(let* ((prefixnodir (file-name-nondirectory prefix)) (unless (eq processing-type 'verbatim)
(absprefix (expand-file-name prefix dir)) (let* ((math-regexp "\\$\\|\\\\[(]]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
(todir (file-name-directory absprefix)) (cnt 0)
(opt org-format-latex-options) checkdir-flag)
(optnew org-format-latex-options) (goto-char (point-min))
(matchers (plist-get opt :matchers)) (while (re-search-forward math-regexp nil t)
(re-list org-latex-regexps) (unless (and overlays
(cnt 0) txt hash link beg end re e checkdir (eq (get-char-property (point) 'org-overlay-type)
string 'org-latex-overlay))
m n block-type block linkfile movefile ov) (let* ((context (org-element-context))
;; Check the different regular expressions (type (org-element-type context)))
(while (setq e (pop re-list)) (when (memq type '(latex-environment latex-fragment))
(setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e) (let ((block-type (eq type 'latex-environment))
block (if block-type "\n\n" "")) (value (org-element-property :value context))
(when (member m matchers) (beg (org-element-property :begin context))
(goto-char (point-min)) (end (save-excursion
(while (re-search-forward re nil t) (goto-char (org-element-property :end context))
(when (and (or (not at) (equal (cdr at) (match-beginning n))) (skip-chars-backward " \r\t\n")
(or (not overlays) (point))))
(not (eq (get-char-property (match-beginning n) (case processing-type
'org-overlay-type) (mathjax
'org-latex-overlay)))) ;; Prepare for MathJax processing.
(cond (if (eq (char-after beg) ?$)
((eq processing-type 'verbatim)) (save-excursion
((eq processing-type 'mathjax) (delete-region beg end)
;; Prepare for MathJax processing. (insert "\\(" (substring value 1 -1) "\\)"))
(setq string (match-string n)) (goto-char end)))
(when (member m '("$" "$1")) ((dvipng imagemagick)
(save-excursion ;; Process to an image.
(delete-region (match-beginning n) (match-end n)) (incf cnt)
(goto-char (match-beginning n)) (goto-char beg)
(insert (concat "\\(" (substring string 1 -1) "\\)"))))) (let* ((face (face-at-point))
((or (eq processing-type 'dvipng) ;; Get the colors from the face at point.
(eq processing-type 'imagemagick)) (fg
;; Process to an image. (let ((color (plist-get org-format-latex-options
(setq txt (match-string n) :foreground)))
beg (match-beginning n) end (match-end n) (if (and forbuffer (eq color 'auto))
cnt (1+ cnt)) (face-attribute face :foreground nil 'default)
(let ((face (face-at-point)) color)))
(fg (plist-get opt :foreground)) (bg
(bg (plist-get opt :background)) (let ((color (plist-get org-format-latex-options
;; Ensure full list is printed. :background)))
print-length print-level) (if (and forbuffer (eq color 'auto))
(when forbuffer (face-attribute face :background nil 'default)
;; Get the colors from the face at point. color)))
(goto-char beg) (hash (sha1 (prin1-to-string
(when (eq fg 'auto) (list org-format-latex-header
(setq fg (face-attribute face :foreground nil 'default))) org-latex-default-packages-alist
(when (eq bg 'auto) org-latex-packages-alist
(setq bg (face-attribute face :background nil 'default))) org-format-latex-options
(setq optnew (copy-sequence opt)) forbuffer value fg bg))))
(plist-put optnew :foreground fg) (absprefix (expand-file-name prefix dir))
(plist-put optnew :background bg)) (linkfile (format "%s_%s.png" prefix hash))
(setq hash (sha1 (prin1-to-string (movefile (format "%s_%s.png" absprefix hash))
(list org-format-latex-header (sep (and block-type "\n\n"))
org-latex-default-packages-alist (link (concat sep "[[file:" linkfile "]]" sep))
org-latex-packages-alist (options
org-format-latex-options (org-combine-plists
forbuffer txt fg bg))) org-format-latex-options
linkfile (format "%s_%s.png" prefix hash) `(:foreground ,fg :background ,bg))))
movefile (format "%s_%s.png" absprefix hash))) (when msg (message msg cnt))
(setq link (concat block "[[file:" linkfile "]]" block)) (unless checkdir-flag ; Ensure the directory exists.
(if msg (message msg cnt)) (setq checkdir-flag t)
(goto-char beg) (let ((todir (file-name-directory absprefix)))
(unless checkdir ; Ensure the directory exists. (unless (file-directory-p todir)
(setq checkdir t) (make-directory todir t))))
(or (file-directory-p todir) (make-directory todir t))) (unless (file-exists-p movefile)
(unless (file-exists-p movefile) (org-create-formula-image
(org-create-formula-image value movefile options forbuffer processing-type))
txt movefile optnew forbuffer processing-type)) (if overlays
(if overlays (progn
(progn (dolist (o (overlays-in beg end))
(mapc (lambda (o) (when (eq (overlay-get o 'org-overlay-type)
(if (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay)
'org-latex-overlay) (delete-overlay o)))
(delete-overlay o))) (let ((ov (make-overlay beg end)))
(overlays-in beg end)) (overlay-put ov
(setq ov (make-overlay beg end)) 'org-overlay-type
(overlay-put ov 'org-overlay-type 'org-latex-overlay) 'org-latex-overlay)
(if (featurep 'xemacs) (if (featurep 'xemacs)
(progn (progn
(overlay-put ov 'invisible t) (overlay-put ov 'invisible t)
(overlay-put (overlay-put
ov 'end-glyph ov 'end-glyph
(make-glyph (vector 'png :file movefile)))) (make-glyph
(overlay-put (vector 'png :file movefile))))
ov 'display (overlay-put
(list 'image :type 'png :file movefile :ascent 'center))) ov 'display
(push ov org-latex-fragment-image-overlays) (list 'image
(goto-char end)) :type 'png
(delete-region beg end) :file movefile
(insert (org-add-props link :ascent 'center)))
(push ov org-latex-fragment-image-overlays))
(goto-char end))
(delete-region beg end)
(insert
(org-add-props link
(list 'org-latex-src (list 'org-latex-src
(replace-regexp-in-string (replace-regexp-in-string "\"" "" value)
"\"" "" txt)
'org-latex-src-embed-type 'org-latex-src-embed-type
(if block-type 'paragraph 'character)))))) (if block-type 'paragraph 'character)))))))
((eq processing-type 'mathml) (mathml
;; Process to MathML ;; Process to MathML.
(unless (save-match-data (org-format-latex-mathml-available-p)) (unless (org-format-latex-mathml-available-p)
(user-error "LaTeX to MathML converter not configured")) (user-error "LaTeX to MathML converter not configured"))
(setq txt (match-string n) (incf cnt)
beg (match-beginning n) end (match-end n) (when msg (message msg cnt))
cnt (1+ cnt)) (goto-char beg)
(if msg (message msg cnt)) (delete-region beg end)
(goto-char beg) (insert (org-format-latex-as-mathml
(delete-region beg end) value block-type prefix dir)))
(insert (org-format-latex-as-mathml (otherwise
txt block-type prefix dir))) (error "Unknown conversion type %s for LaTeX fragments"
(t processing-type)))))))))))
(error "Unknown conversion type %s for LaTeX fragments"
processing-type)))))))))
(defun org-create-math-formula (latex-frag &optional mathml-file) (defun org-create-math-formula (latex-frag &optional mathml-file)
"Convert LATEX-FRAG to MathML and store it in MATHML-FILE. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.

View File

@ -2582,7 +2582,7 @@ a plist containing export properties."
(with-temp-buffer (with-temp-buffer
(insert latex-frag) (insert latex-frag)
(org-format-latex cache-relpath cache-dir nil "Creating LaTeX Image..." (org-format-latex cache-relpath cache-dir nil "Creating LaTeX Image..."
nil nil processing-type) nil processing-type)
(buffer-string)))) (buffer-string))))
(defun org-html-latex-environment (latex-environment contents info) (defun org-html-latex-environment (latex-environment contents info)

View File

@ -3804,7 +3804,7 @@ contextual information."
(insert latex-frag) (insert latex-frag)
(org-format-latex cache-subdir cache-dir (org-format-latex cache-subdir cache-dir
nil display-msg nil display-msg
nil nil processing-type) nil processing-type)
(buffer-substring-no-properties (buffer-substring-no-properties
(point-min) (point-max))))) (point-min) (point-max)))))
(if (not (string-match "file:\\([^]]*\\)" link)) (if (not (string-match "file:\\([^]]*\\)" link))