* [PATCH] [need test]Restruct `org-create-formula-image' function
@ 2013-07-20 9:32 feng shu
2013-07-20 14:33 ` Nick Dokos
0 siblings, 1 reply; 3+ messages in thread
From: feng shu @ 2013-07-20 9:32 UTC (permalink / raw)
To: emacs-orgmode@gnu.org
[-- Attachment #1.1: Type: text/plain, Size: 146 bytes --]
`org-create-formula-image-with-dvipng' and
`org-create-formula-image-with-imagemagick'
share a good deal of logic, so combine them to a function.
[-- Attachment #1.2: Type: text/html, Size: 185 bytes --]
[-- Attachment #2: 0001-Restruct-org-create-formula-image-function.patch --]
[-- Type: application/octet-stream, Size: 11324 bytes --]
From 8c980da62b8f2438de3f3136cb9096e419f81fbb Mon Sep 17 00:00:00 2001
From: Feng Shu <tumashu@gmail.com>
Date: Wed, 17 Jul 2013 08:16:45 +0800
Subject: [PATCH] Restruct `org-create-formula-image' function
* org.el (org-create-formula-image-processes): New variable,
define `dvipng' and `imagemagick' processes.
(org-create-formula-image): Combine
`org-create-formula-image-with-dvipng' and
`org-create-formula-image-with-imagemagick'.
(org-create-formula-image-with-dvipng): Removed.
(org-create-formula-image-with-imagemagick): Removed.
`org-create-formula-image-with-dvipng' and
`org-create-formula-image-with-imagemagick'
share a good deal of logic, so combine them to a function.
---
lisp/org.el | 208 +++++++++++++++++++++++------------------------------------
1 个文件被修改,插入 82 行(+),删除 126 行(-)
diff --git a/lisp/org.el b/lisp/org.el
index fb5099e..99cbb92 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18483,30 +18483,6 @@ inspection."
;; Failed conversion. Return the LaTeX fragment verbatim
latex-frag)))
-(defun org-create-formula-image (string tofile options buffer &optional type)
- "Create an image from LaTeX source using dvipng or convert.
-This function calls either `org-create-formula-image-with-dvipng'
-or `org-create-formula-image-with-imagemagick' depending on the
-value of `org-latex-create-formula-image-program' or on the value
-of the optional TYPE variable.
-
-Note: ultimately these two function should be combined as they
-share a good deal of logic."
- (org-check-external-command
- "latex" "needed to convert LaTeX fragments to images")
- (funcall
- (case (or type org-latex-create-formula-image-program)
- ('dvipng
- (org-check-external-command
- "dvipng" "needed to convert LaTeX fragments to images")
- #'org-create-formula-image-with-dvipng)
- ('imagemagick
- (org-check-external-command
- "convert" "you need to install imagemagick")
- #'org-create-formula-image-with-imagemagick)
- (t (error
- "Invalid value of `org-latex-create-formula-image-program'")))
- string tofile options buffer))
(declare-function org-export-get-backend "ox" (name))
(declare-function org-export--get-global-options "ox" (&optional backend))
@@ -18528,88 +18504,55 @@ share a good deal of logic."
(plist-get info :latex-header)))
info)))
-;; This function borrows from Ganesh Swami's latex2png.el
-(defun org-create-formula-image-with-dvipng (string tofile options buffer)
- "This calls dvipng."
- (require 'ox-latex)
- (let* ((tmpdir (if (featurep 'xemacs)
- (temp-directory)
- temporary-file-directory))
- (texfilebase (make-temp-name
- (expand-file-name "orgtex" tmpdir)))
- (texfile (concat texfilebase ".tex"))
- (dvifile (concat texfilebase ".dvi"))
- (pngfile (concat texfilebase ".png"))
- (fnh (if (featurep 'xemacs)
- (font-height (face-font 'default))
- (face-attribute 'default :height nil)))
- (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
- (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
- (fg (or (plist-get options (if buffer :foreground :html-foreground))
- "Black"))
- (bg (or (plist-get options (if buffer :background :html-background))
- "Transparent")))
- (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
- (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
- (if (eq bg 'default) (setq bg (org-dvipng-color :background))
- (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
- (let ((latex-header (org-create-formula--latex-header)))
- (with-temp-file texfile
- (insert latex-header)
- (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
- (let ((dir default-directory))
- (condition-case nil
- (progn
- (cd tmpdir)
- (call-process "latex" nil nil nil texfile))
- (error nil))
- (cd dir))
- (if (not (file-exists-p dvifile))
- (progn (message "Failed to create dvi file from %s" texfile) nil)
- (condition-case nil
- (if (featurep 'xemacs)
- (call-process "dvipng" nil nil nil
- "-fg" fg "-bg" bg
- "-T" "tight"
- "-o" pngfile
- dvifile)
- (call-process "dvipng" nil nil nil
- "-fg" fg "-bg" bg
- "-D" dpi
- ;;"-x" scale "-y" scale
- "-T" "tight"
- "-o" pngfile
- dvifile))
- (error nil))
- (if (not (file-exists-p pngfile))
- (if org-format-latex-signal-error
- (error "Failed to create png file from %s" texfile)
- (message "Failed to create png file from %s" texfile)
- nil)
- ;; Use the requested file name and clean up
- (copy-file pngfile tofile 'replace)
- (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
- (if (file-exists-p (concat texfilebase e))
- (delete-file (concat texfilebase e))))
- pngfile))))
-
(declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
-(defun org-create-formula-image-with-imagemagick (string tofile options buffer)
- "This calls convert, which is included into imagemagick."
+
+(defvar org-create-formula-image-processes
+ '((dvipng
+ ("latex" "dvipng")
+ ("latex %TEXFILE" "dvipng -T tight -D 96 -o %PNGFILE %DVIFILE"))
+ (imagemagick
+ ("latex" "convert")
+ ((lambda (texfile) (org-latex-compile texfile t))
+ "convert -density 96 -trim -antialias %PDFFILE -quality 100 %PNGFILE")))
+ "Commands to process a LaTeX snippet to a png file.
+This is an alist. The first element of its association is the name of process,
+the second is a list of prerequired system commands, the third is a list of elisp
+functions or shell commands, which will be launched when compile latex snippet file
+and convert the pdf or dvi file to png.
+
+Before the shell commands launched, %TEXFILE, %DVIFILE, %PDFFILE and %PNGFILE will
+be replaced by the corresponding files.
+
+NOTE: Elisp functions should accept a file name as its single argument.")
+
+(defun org-create-formula-image (string tofile options buffer &optional type)
+ "Create an image from LaTeX source using a process predefined in
+`org-create-formula-image-processes' depending on the value of
+`org-latex-create-formula-image-program' or on the value of the
+optional TYPE variable."
(require 'ox-latex)
- (let* ((tmpdir (if (featurep 'xemacs)
+ (let* ((processes (assoc
+ (or type org-latex-create-formula-image-program)
+ org-create-formula-image-processes))
+ (commands-need-checked (cadr processes))
+ (latex-process (caar (cddr processes)))
+ (png-process (cadar (cddr processes)))
+ (default-dir default-directory)
+ (tmpdir (if (featurep 'xemacs)
(temp-directory)
temporary-file-directory))
(texfilebase (make-temp-name
(expand-file-name "orgtex" tmpdir)))
(texfile (concat texfilebase ".tex"))
(pdffile (concat texfilebase ".pdf"))
+ (dvifile (concat texfilebase ".dvi"))
(pngfile (concat texfilebase ".png"))
(fnh (if (featurep 'xemacs)
(font-height (face-font 'default))
(face-attribute 'default :height nil)))
(scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
- (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
+ (fontsize (* scale (if buffer (* 0.1 fnh) 12.)))
+ (baselineskip (* 1.2 fontsize))
(fg (or (plist-get options (if buffer :foreground :html-foreground))
"black"))
(bg (or (plist-get options (if buffer :background :html-background))
@@ -18619,50 +18562,63 @@ share a good deal of logic."
(if (eq bg 'default) (setq bg (org-latex-color :background))
(setq bg (org-latex-color-format
(if (string= bg "Transparent") "white" bg))))
+ ;; Build a tex file for latex formula.
(let ((latex-header (org-create-formula--latex-header)))
(with-temp-file texfile
(insert latex-header)
(insert "\n\\begin{document}\n"
"\\definecolor{fg}{rgb}{" fg "}\n"
"\\definecolor{bg}{rgb}{" bg "}\n"
+ "\\fontsize{" (format "%0.1f" fontsize) "pt}{" (format "%0.1f" baselineskip) "pt}\n"
+ "\\selectfont\n"
"\n\\pagecolor{bg}\n"
"\n{\\color{fg}\n"
string
"\n}\n"
"\n\\end{document}\n")))
- (org-latex-compile texfile t)
- (if (not (file-exists-p pdffile))
- (progn (message "Failed to create pdf file from %s" texfile) nil)
- (condition-case nil
- (if (featurep 'xemacs)
- (call-process "convert" nil nil nil
- "-density" "96"
- "-trim"
- "-antialias"
- pdffile
- "-quality" "100"
- ;; "-sharpen" "0x1.0"
- pngfile)
- (call-process "convert" nil nil nil
- "-density" dpi
- "-trim"
- "-antialias"
- pdffile
- "-quality" "100"
- ;; "-sharpen" "0x1.0"
- pngfile))
- (error nil))
- (if (not (file-exists-p pngfile))
- (if org-format-latex-signal-error
- (error "Failed to create png file from %s" texfile)
- (message "Failed to create png file from %s" texfile)
- nil)
- ;; Use the requested file name and clean up
- (copy-file pngfile tofile 'replace)
- (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
- (if (file-exists-p (concat texfilebase e))
- (delete-file (concat texfilebase e))))
- pngfile))))
+ (save-window-excursion
+ ;; Check required commands.
+ (if (some 'null (mapcar (lambda (cmd) (org-check-external-command cmd nil t)) commands-need-checked))
+ (error (format "You need to install commands: %s" (mapconcat 'concat commands-need-checked ", ")))
+ ;; Compile tex file to pdf or dvi file.
+ (cd tmpdir)
+ (condition-case nil
+ (if (stringp latex-process)
+ (shell-command
+ (replace-regexp-in-string
+ "%TEXFILE" (shell-quote-argument texfile)
+ (replace-regexp-in-string
+ "%DVIFILE" (shell-quote-argument dvifile)
+ (replace-regexp-in-string
+ "%PDFFILE" (shell-quote-argument pdffile) latex-process t t) t t) t t)
+ (get-buffer-create "*Org Preview LaTeX Snippet*"))
+ (funcall latex-process (shell-quote-argument texfile)))
+ (error nil))
+ (cd default-dir)
+ (if (and (not (file-exists-p pdffile))
+ (not (file-exists-p dvifile)))
+ (error "Failed to create dvi file or pdf file from %s" texfile)
+ ;; Convert pdf or dvi file to png file.
+ (if (stringp png-process)
+ (shell-command
+ (replace-regexp-in-string
+ "%PNGFILE" (shell-quote-argument pngfile)
+ (replace-regexp-in-string
+ "%DVIFILE" (shell-quote-argument dvifile)
+ (replace-regexp-in-string
+ "%PDFFILE" (shell-quote-argument pdffile) png-process t t) t t) t t) nil)
+ (funcall png-process (shell-quote-argument (if (file-exists-p pdffile) pdffile dvifile))))
+ (if (not (file-exists-p pngfile))
+ (if org-format-latex-signal-error
+ (error "Failed to create png file from %s" texfile)
+ (message "Failed to create png file from %s" texfile)
+ nil)
+ ;; Use the requested file name and clean up
+ (copy-file pngfile tofile 'replace)
+ (loop for e in '(".dvi" ".pdf" ".tex" ".aux" ".log" ".png") do
+ (if (file-exists-p (concat texfilebase e))
+ (delete-file (concat texfilebase e))))
+ pngfile))))))
(defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
"Fill a LaTeX header template TPL.
--
1.7.10.4
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] [need test]Restruct `org-create-formula-image' function
2013-07-20 9:32 [PATCH] [need test]Restruct `org-create-formula-image' function feng shu
@ 2013-07-20 14:33 ` Nick Dokos
2013-07-20 22:20 ` Feng Shu
0 siblings, 1 reply; 3+ messages in thread
From: Nick Dokos @ 2013-07-20 14:33 UTC (permalink / raw)
To: emacs-orgmode
feng shu <tumashu@gmail.com> writes:
> `org-create-formula-image-with-dvipng' and
> `org-create-formula-image-with-imagemagick'
> share a good deal of logic, so combine them to a function.
>
>
I have wondered aloud in the past whether it is necessary to have two
methods for this. They are pretty much equivalent although the
imagemagick method is IMO slightly less brittle than the dvipng method.
So instead of spending time to tie them even more closely together, we
should investigate whether one of them should go.
Maybe we can have a poll...
--
Nick
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] [need test]Restruct `org-create-formula-image' function
2013-07-20 14:33 ` Nick Dokos
@ 2013-07-20 22:20 ` Feng Shu
0 siblings, 0 replies; 3+ messages in thread
From: Feng Shu @ 2013-07-20 22:20 UTC (permalink / raw)
To: emacs-orgmode
Nick Dokos <ndokos@gmail.com> writes:
> feng shu <tumashu@gmail.com> writes:
>
>> `org-create-formula-image-with-dvipng' and
>> `org-create-formula-image-with-imagemagick'
>> share a good deal of logic, so combine them to a function.
>>
>>
>
> I have wondered aloud in the past whether it is necessary to have two
> methods for this. They are pretty much equivalent although the
> imagemagick method is IMO slightly less brittle than the dvipng method.
> So instead of spending time to tie them even more closely together, we
> should investigate whether one of them should go.
>
> Maybe we can have a poll...
imagemagick method is useful for xelatex users, but it compile a snippet
more slower than dvipng method.
We don't need remove one of them. we can do like this:
1. conbine tex:dvipng and tex:imagemagick to tex:image
2. remove `org-latex-create-formula-image-program'
3. defcustom `org-create-formula-image-processes' in my patch.
4. update document.
It is very similar `org-latex-pdf-process'
--
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2013-07-20 22:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-20 9:32 [PATCH] [need test]Restruct `org-create-formula-image' function feng shu
2013-07-20 14:33 ` Nick Dokos
2013-07-20 22:20 ` Feng Shu
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.