emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [patch] A experimental toy which is used to preview latex fragements
@ 2012-03-23  8:22 FengShu
  2012-03-24 13:34 ` Need Help: " FengShu
  0 siblings, 1 reply; 11+ messages in thread
From: FengShu @ 2012-03-23  8:22 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 274 bytes --]

Hi everyone!
    This is a experimental toy, which use 
'(car org-latex-to-pdf-process) to convert latex formula fragements ,the
 converting path is "latex->pdf->png" using imagemagick.
    I'm not a programmer and only know little elisp,so the code quality
is poor...






[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Ihis-is-a-experimental-toy-which-use-car-org-latex-t.patch --]
[-- Type: text/x-diff, Size: 5204 bytes --]

From 6a3fbe47d967f8d234d3aead058148cc46b7d376 Mon Sep 17 00:00:00 2001
From: FengShu <tumashu@gmail.com>
Date: Fri, 23 Mar 2012 16:05:37 +0800
Subject: [PATCH] Ihis is a experimental toy, which use '(car
 org-latex-to-pdf-process) to convert latex formula,the
 converting path is "latex->pdf->png" using imagemagick.

---
 lisp/org.el |  106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 105 insertions(+), 1 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 811b41b..7eea801 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16966,7 +16966,7 @@ Some of the options can be changed using the variable
 		(setq executables-checked t))
 
 	      (unless (file-exists-p movefile)
-		(org-create-formula-image
+		(org-create-formula-image-with-imagemagick
 		 txt movefile opt forbuffer))
 	      (if overlays
 		  (progn
@@ -17168,6 +17168,99 @@ inspection."
 	(loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
 	      (delete-file (concat texfilebase e)))
 	pngfile))))
+;; convert tex file to pdf ,than convert pdf file  to pngfile used imagemagick 
+(defun org-create-formula-image-with-imagemagick (string tofile options buffer)
+  "This calls imagemagick."
+  (require 'org-latex)
+  (let* ((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"))
+	 (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-latex-color :foreground)))
+    (if (eq bg 'default) (setq bg (org-latex-color :background)))
+    (with-temp-file texfile
+      (insert (org-splice-latex-header
+	       org-format-latex-header
+	       org-export-latex-default-packages-alist
+	       org-export-latex-packages-alist t
+	       org-format-latex-header-extra))
+      (insert "\n\\begin{document}\n" 
+	      "\\definecolor{fg}{rgb}{" fg "}\n"
+	      "\\definecolor{bg}{rgb}{" bg "}\n"
+	      "\n\\pagecolor{bg}\n"
+	      "\n{\\color{fg}\n"
+	      string
+	      "\n}\n"
+	      "\n\\end{document}\n" )
+      (require 'org-latex)
+      (org-export-latex-fix-inputenc))
+    (let ((dir default-directory))
+      (condition-case nil
+	  (progn
+	    (cd tmpdir)
+	    (setq cmd (car org-latex-to-pdf-process))
+	    (while (string-match "%b" cmd)
+	      (setq cmd (replace-match
+			 (save-match-data
+			   (shell-quote-argument texfile))
+			 t t cmd)))
+	    (while (string-match "%f" cmd)
+	      (setq cmd (replace-match
+			 (save-match-data
+			   (shell-quote-argument (file-name-nondirectory texfile)))
+			 t t cmd)))
+	    (while (string-match "%o" cmd)
+	      (setq cmd (replace-match
+			 (save-match-data
+			   (shell-quote-argument (file-name-directory texfile)))
+			 t t cmd)))
+	    (shell-command cmd))
+	(error nil))
+      (cd dir))
+    (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
+	      (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.
@@ -17239,6 +17332,17 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML."
 					   ((eq attr :background) 'background))))
 		   (color-values (face-attribute 'default attr nil))))))
 
+(defun org-latex-color (attr)
+  "Return an rgb color specification for latex color package."
+  (apply 'format "%s,%s,%s"
+	 (mapcar 'org-normalize-color
+		 (if (featurep 'xemacs)
+		     (color-rgb-components
+		      (face-property 'default
+				     (cond ((eq attr :foreground) 'foreground)
+					   ((eq attr :background) 'background))))
+		   (color-values (face-attribute 'default attr nil))))))
+
 (defun org-normalize-color (value)
   "Return string to be used as color value for an RGB component."
   (format "%g" (/ value 65535.0)))
-- 
1.7.9.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: Need Help: [patch] A experimental toy which is used to preview latex fragements
  2012-03-23  8:22 [patch] A experimental toy which is used to preview latex fragements FengShu
@ 2012-03-24 13:34 ` FengShu
  2012-03-24 17:32   ` Nick Dokos
  0 siblings, 1 reply; 11+ messages in thread
From: FengShu @ 2012-03-24 13:34 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 842 bytes --]


The default path  by which we can  preview latex fragments 
is like this:  latex -> dvi -> png , and this path works 
very well to the people who mainly use pdflatex.
  
  But,if you mainly use *xelatex* and use some features which is
only available in *xelatex* ,for example using system fonts:
#+latex_header:  \setsansfont{WenQuanYi Zen Hei}


  you will find that previewing latex fragments doesn't work
I find that xelatex can't produce dvi output which can be
converted to png by dvipng. so I want to use this path:
"latex -> pdf ->png" and hack to get this patch, this is 
version 2, and it doesn't affect the default behaver if you
don't set some variables. 

  At this time ,it can't covert all the latex fragments in the
buffer and only convert one, I can't find the problem due my
pool computer language ability. so I need help!


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Ihis-is-a-experimental-toy-which-use-car-org-latex-t.patch --]
[-- Type: text/x-diff, Size: 10751 bytes --]

From 3708a4b1cf4e94e5ce0d87ca8bec6e6367c40bce Mon Sep 17 00:00:00 2001
From: FengShu <tumashu@gmail.com>
Date: Fri, 23 Mar 2012 16:05:37 +0800
Subject: [PATCH] Ihis is a experimental toy, which use '(car
 org-latex-to-pdf-process) to convert latex formula,the
 converting path is "latex->pdf->png" using imagemagick.

---
 lisp/org-exp.el  |   16 ++++--
 lisp/org-html.el |    1 +
 lisp/org.el      |  157 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 154 insertions(+), 20 deletions(-)

diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index eeee1ce..0317539 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -537,12 +537,15 @@ This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
 
 Allowed values are:
 
-nil        Don't do anything.
-verbatim   Keep everything in verbatim
-dvipng     Process the LaTeX fragments to images.
-           This will also include processing of non-math environments.
-t          Do MathJax preprocessing if there is at least on math snippet,
-           and arrange for MathJax.js to be loaded.
+nil             Don't do anything.
+verbatim        Keep everything in verbatim
+dvipng          Process the LaTeX fragments to images.
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf with program:
+                '(car org-latex-to-pdf-process), And then use
+                imagemagick convert pdf to png
+t               Do MathJax preprocessing if there is at least on math snippet,
+                and arrange for MathJax.js to be loaded.
 
 The default is nil, because this option needs the `dvipng' program which
 is not available on all systems."
@@ -552,6 +555,7 @@ is not available on all systems."
 	  (const :tag "Do not process math in any way" nil)
 	  (const :tag "Obsolete, use dvipng setting" t)
 	  (const :tag "Use dvipng to make images" dvipng)
+	  (const :tag "Use imagemagick to make images" imagemagick)
 	  (const :tag "Use MathJax to display math" mathjax)
 	  (const :tag "Leave math verbatim" verbatim)))
 
diff --git a/lisp/org-html.el b/lisp/org-html.el
index 2de2ea9..15a6c3e 100644
--- a/lisp/org-html.el
+++ b/lisp/org-html.el
@@ -685,6 +685,7 @@ The default is an extended format of the ISO 8601 specification."
       ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
       ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
       ((eq (plist-get parameters :LaTeX-fragments) t        ) 'mathjax)
+      ((eq (plist-get parameters :LaTeX-fragments) 'imagemagick  ) 'imagemagick)
       ((eq (plist-get parameters :LaTeX-fragments) 'dvipng  ) 'dvipng)
       (t nil))))
   (goto-char (point-min))
diff --git a/lisp/org.el b/lisp/org.el
index 159bb7f..c8bb3ef 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3389,6 +3389,23 @@ When using MathToWeb as the converter, set this to
   :type '(choice
 	  (const :tag "None" nil)
 	  (string :tag "\nShell command")))
+(defcustom org-create-formula-image-program 'dvipng
+  "When a org file you are editing include some latex fragments,
+you want to preview them,you can convert them into png files and 
+then insert into the org buffers
+
+dvipng          Process the LaTeX fragments to dvi file, then convert
+                dvi files  to png files using dvipng. 
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf with program:
+                '(car org-latex-to-pdf-process), And then use
+                imagemagick convert pdf to png"
+
+  :group 'org-latex
+  :version "24.1"
+  :type '(choice
+	  (const :tag "dvipng" dvipng)
+	  (const :tag "imagemagick" imagemagick)))
 
 (defun org-format-latex-mathml-available-p ()
   "Return t if `org-latex-to-mathml-convert-command' is usable."
@@ -16916,7 +16933,7 @@ The images can be removed again with \\[org-ctrl-c-ctrl-c]."
 	 (concat "ltxpng/" (file-name-sans-extension
 			    (file-name-nondirectory
 			     buffer-file-name)))
-	 default-directory 'overlays msg at 'forbuffer 'dvipng)
+	 default-directory 'overlays msg at 'forbuffer org-create-formula-image-program)
       (message msg "done.  Use `C-c C-c' to remove images.")))))
 
 (defvar org-latex-regexps
@@ -16980,7 +16997,8 @@ Some of the options can be changed using the variable
 				'(org-protected t))))
 		(add-text-properties (match-beginning n) (match-end n)
 				     '(org-protected t))))
-	     ((eq processing-type 'dvipng)
+	     ((or (eq processing-type 'dvipng)
+		  (eq processing-type 'imagemagick))
 	      ;; Process to an image
 	      (setq txt (match-string n)
 		    beg (match-beginning n) end (match-end n)
@@ -17001,17 +17019,25 @@ Some of the options can be changed using the variable
 	      (unless checkdir ; make sure the directory exists
 		(setq checkdir t)
 		(or (file-directory-p todir) (make-directory todir t)))
-
-	      (unless executables-checked
-		(org-check-external-command
-		 "latex" "needed to convert LaTeX fragments to images")
-		(org-check-external-command
-		 "dvipng" "needed to convert LaTeX fragments to images")
-		(setq executables-checked t))
-
-	      (unless (file-exists-p movefile)
-		(org-create-formula-image
-		 txt movefile opt forbuffer))
+	      (cond 
+	       ((eq processing-type 'dvipng)
+		(unless executables-checked
+		  (org-check-external-command
+		   "latex" "needed to convert LaTeX fragments to images")
+		  (org-check-external-command
+		   "dvipng" "needed to convert LaTeX fragments to images")
+		  (setq executables-checked t)
+		  (unless (file-exists-p movefile)
+		    (org-create-formula-image-with-dvipng
+		     txt movefile opt forbuffer))))
+	       ((eq processing-type 'imagemagick)
+		(unless executables-checked
+		  (org-check-external-command
+		   "convert" "needed to convert LaTeX fragments to images")
+		  (setq executables-checked t)
+		  (unless (file-exists-p movefile)
+		    (org-create-formula-image-with-imagemagick
+		     txt movefile opt forbuffer)))))
 	      (if overlays
 		  (progn
 		    (mapc (lambda (o)
@@ -17147,7 +17173,7 @@ inspection."
       latex-frag)))
 
 ;; This function borrows from Ganesh Swami's latex2png.el
-(defun org-create-formula-image (string tofile options buffer)
+(defun org-create-formula-image-with-dvipng (string tofile options buffer)
   "This calls dvipng."
   (require 'org-latex)
   (let* ((tmpdir (if (featurep 'xemacs)
@@ -17212,6 +17238,98 @@ inspection."
 	(loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
 	      (delete-file (concat texfilebase e)))
 	pngfile))))
+(defun org-create-formula-image-with-imagemagick (string tofile options buffer)
+  "This calls convert, which is included into imagemagick."
+  (require 'org-latex)
+  (let* ((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"))
+	 (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-latex-color :foreground)))
+    (if (eq bg 'default) (setq bg (org-latex-color :background)))
+    (with-temp-file texfile
+      (insert (org-splice-latex-header
+	       org-format-latex-header
+	       org-export-latex-default-packages-alist
+	       org-export-latex-packages-alist t
+	       org-format-latex-header-extra))
+      (insert "\n\\begin{document}\n" 
+	      "\\definecolor{fg}{rgb}{" fg "}\n"
+	      "\\definecolor{bg}{rgb}{" bg "}\n"
+	      "\n\\pagecolor{bg}\n"
+	      "\n{\\color{fg}\n"
+	      string
+	      "\n}\n"
+	      "\n\\end{document}\n" )
+      (require 'org-latex)
+      (org-export-latex-fix-inputenc))
+    (let ((dir default-directory))
+      (condition-case nil
+	  (progn
+	    (cd tmpdir)
+	    (setq cmd (car org-latex-to-pdf-process))
+	    (while (string-match "%b" cmd)
+	      (setq cmd (replace-match
+			 (save-match-data
+			   (shell-quote-argument texfile))
+			 t t cmd)))
+	    (while (string-match "%f" cmd)
+	      (setq cmd (replace-match
+			 (save-match-data
+			   (shell-quote-argument (file-name-nondirectory texfile)))
+			 t t cmd)))
+	    (while (string-match "%o" cmd)
+	      (setq cmd (replace-match
+			 (save-match-data
+			   (shell-quote-argument (file-name-directory texfile)))
+			 t t cmd)))
+	    (shell-command cmd))
+	(error nil))
+      (cd dir))
+    (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
+	      (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.
@@ -17283,6 +17401,17 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML."
 					   ((eq attr :background) 'background))))
 		   (color-values (face-attribute 'default attr nil))))))
 
+(defun org-latex-color (attr)
+  "Return an rgb color specification for latex color package."
+  (apply 'format "%s,%s,%s"
+	 (mapcar 'org-normalize-color
+		 (if (featurep 'xemacs)
+		     (color-rgb-components
+		      (face-property 'default
+				     (cond ((eq attr :foreground) 'foreground)
+					   ((eq attr :background) 'background))))
+		   (color-values (face-attribute 'default attr nil))))))
+
 (defun org-normalize-color (value)
   "Return string to be used as color value for an RGB component."
   (format "%g" (/ value 65535.0)))
-- 
1.7.9.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: Need Help: [patch] A experimental toy which is used to preview latex fragements
  2012-03-24 13:34 ` Need Help: " FengShu
@ 2012-03-24 17:32   ` Nick Dokos
  2012-03-24 17:45     ` Nick Dokos
  0 siblings, 1 reply; 11+ messages in thread
From: Nick Dokos @ 2012-03-24 17:32 UTC (permalink / raw)
  To: FengShu; +Cc: nicholas.dokos, emacs-orgmode

FengShu <tumashu@gmail.com> wrote:

> ...
>   At this time ,it can't covert all the latex fragments in the
> buffer and only convert one, I can't find the problem due my
> pool computer language ability. so I need help!
> 

I tried your patch and I can see the problem (I think), but it might not
be a problem with your patch: I run into it even if
org-create-formula-image-program is set to dvipng. Of course, there is a
possibility that your patch broke dvipng, but at this point it doesn't
look that way.

I'll do some more testing and let you know.

Nick

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Need Help: [patch] A experimental toy which is used to preview latex fragements
  2012-03-24 17:32   ` Nick Dokos
@ 2012-03-24 17:45     ` Nick Dokos
  2012-03-24 18:54       ` Nick Dokos
  0 siblings, 1 reply; 11+ messages in thread
From: Nick Dokos @ 2012-03-24 17:45 UTC (permalink / raw)
  To: FengShu, emacs-orgmode; +Cc: nicholas.dokos

Nick Dokos <nicholas.dokos@hp.com> wrote:

> FengShu <tumashu@gmail.com> wrote:
> 
> > ...
> >   At this time ,it can't covert all the latex fragments in the
> > buffer and only convert one, I can't find the problem due my
> > pool computer language ability. so I need help!
> > 
> 
> I tried your patch and I can see the problem (I think), but it might not
> be a problem with your patch: I run into it even if
> org-create-formula-image-program is set to dvipng. Of course, there is a
> possibility that your patch broke dvipng, but at this point it doesn't
> look that way.
> 
> I'll do some more testing and let you know.
> 

No, you are right: it does seem as if your patch causes only the first
fragment to be processed (in both the dvipng and imagemagick cases).
Unpatched org (with dvipng) processes everything.

Nick

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Need Help: [patch] A experimental toy which is used to preview latex fragements
  2012-03-24 17:45     ` Nick Dokos
@ 2012-03-24 18:54       ` Nick Dokos
  2012-03-25 12:36         ` FengShu
  0 siblings, 1 reply; 11+ messages in thread
From: Nick Dokos @ 2012-03-24 18:54 UTC (permalink / raw)
  To: FengShu, emacs-orgmode; +Cc: nicholas.dokos

Nick Dokos <nicholas.dokos@hp.com> wrote:

> Nick Dokos <nicholas.dokos@hp.com> wrote:
> 
> > FengShu <tumashu@gmail.com> wrote:
> > 
> > > ...
> > >   At this time ,it can't covert all the latex fragments in the
> > > buffer and only convert one, I can't find the problem due my
> > > pool computer language ability. so I need help!
> > > 
> > 
> > I tried your patch and I can see the problem (I think), but it might not
> > be a problem with your patch: I run into it even if
> > org-create-formula-image-program is set to dvipng. Of course, there is a
> > possibility that your patch broke dvipng, but at this point it doesn't
> > look that way.
> > 
> > I'll do some more testing and let you know.
> > 
> 
> No, you are right: it does seem as if your patch causes only the first
> fragment to be processed (in both the dvipng and imagemagick cases).
> Unpatched org (with dvipng) processes everything.
> 

Ok - just a misplaced paren - the code (I just show the dvipng case,
but the imagemagick case will need it too) should look like this:


--8<---------------cut here---------------start------------->8---
               ...
	       ((eq processing-type 'dvipng)
		(unless executables-checked
		  (org-check-external-command
		   "latex" "needed to convert LaTeX fragments to images")
		  (org-check-external-command
		   "dvipng" "needed to convert LaTeX fragments to images")
		  (setq executables-checked t))
		(unless (file-exists-p movefile)
		  (org-create-formula-image-with-dvipng
		     txt movefile opt forbuffer)))
	       ...
--8<---------------cut here---------------end--------------->8---

with two independent sexps: one to check for executables and one to check
for files. The misplaced paren caused the file check to be inside the
executable check, so it never produced any files after the first one.

Nick

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Need Help: [patch] A experimental toy which is used to preview latex fragements
  2012-03-24 18:54       ` Nick Dokos
@ 2012-03-25 12:36         ` FengShu
  2012-03-26  6:07           ` [patch] Need test: New path " FengShu
  0 siblings, 1 reply; 11+ messages in thread
From: FengShu @ 2012-03-25 12:36 UTC (permalink / raw)
  To: emacs-orgmode

>
> Ok - just a misplaced paren - the code (I just show the dvipng case,
> but the imagemagick case will need it too) should look like this:
>                ...
> 	       ((eq processing-type 'dvipng)
> 		(unless executables-checked
> 		  (org-check-external-command
> 		   "latex" "needed to convert LaTeX fragments to images")
> 		  (org-check-external-command
> 		   "dvipng" "needed to convert LaTeX fragments to images")
> 		  (setq executables-checked t))
> 		(unless (file-exists-p movefile)
> 		  (org-create-formula-image-with-dvipng
> 		     txt movefile opt forbuffer)))
> 	       ...
>
> with two independent sexps: one to check for executables and one to check
> for files. The misplaced paren caused the file check to be inside the
> executable check, so it never produced any files after the first one.
>
> Nick

I change the patch ,and it seem work well, thanks very much.

Another questions:
1. I use latex color micro to change the pdf and png's  *fg* and *bg*,
   I want to know the compatibility between the color micro and other latex micro
2. I use (car org-latex-to-pdf-process) to convert latex. and my
   question is: is this path reliable? 


The below is v3 patch: 

From 13ce8af1ca7b04a7e69ff1262e5b89131b67b7cd Mon Sep 17 00:00:00 2001
From: FengShu <tumashu@gmail.com>
Date: Fri, 23 Mar 2012 16:05:37 +0800
Subject: [PATCH] This is a experimental toy ,which use '(car
 org-latex-to-pdf-process) and imagemagick to convert latex
 fragements into pngfiles,and then insert them into the
 editing buffer,you can use it to  preview latex  formula
 and so on. the converting path is "latex->pdf->png",which
 is different from the org default path: "latex->dvi->png"

---
 lisp/org-exp.el  |   16 ++++--
 lisp/org-html.el |    1 +
 lisp/org.el      |  157 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 154 insertions(+), 20 deletions(-)

diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index eeee1ce..0317539 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -537,12 +537,15 @@ This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
 
 Allowed values are:
 
-nil        Don't do anything.
-verbatim   Keep everything in verbatim
-dvipng     Process the LaTeX fragments to images.
-           This will also include processing of non-math environments.
-t          Do MathJax preprocessing if there is at least on math snippet,
-           and arrange for MathJax.js to be loaded.
+nil             Don't do anything.
+verbatim        Keep everything in verbatim
+dvipng          Process the LaTeX fragments to images.
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf with program:
+                '(car org-latex-to-pdf-process), And then use
+                imagemagick convert pdf to png
+t               Do MathJax preprocessing if there is at least on math snippet,
+                and arrange for MathJax.js to be loaded.
 
 The default is nil, because this option needs the `dvipng' program which
 is not available on all systems."
@@ -552,6 +555,7 @@ is not available on all systems."
 	  (const :tag "Do not process math in any way" nil)
 	  (const :tag "Obsolete, use dvipng setting" t)
 	  (const :tag "Use dvipng to make images" dvipng)
+	  (const :tag "Use imagemagick to make images" imagemagick)
 	  (const :tag "Use MathJax to display math" mathjax)
 	  (const :tag "Leave math verbatim" verbatim)))
 
diff --git a/lisp/org-html.el b/lisp/org-html.el
index 2de2ea9..15a6c3e 100644
--- a/lisp/org-html.el
+++ b/lisp/org-html.el
@@ -685,6 +685,7 @@ The default is an extended format of the ISO 8601 specification."
       ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
       ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
       ((eq (plist-get parameters :LaTeX-fragments) t        ) 'mathjax)
+      ((eq (plist-get parameters :LaTeX-fragments) 'imagemagick  ) 'imagemagick)
       ((eq (plist-get parameters :LaTeX-fragments) 'dvipng  ) 'dvipng)
       (t nil))))
   (goto-char (point-min))
diff --git a/lisp/org.el b/lisp/org.el
index 159bb7f..3805531 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3389,6 +3389,23 @@ When using MathToWeb as the converter, set this to
   :type '(choice
 	  (const :tag "None" nil)
 	  (string :tag "\nShell command")))
+(defcustom org-create-formula-image-program 'dvipng
+  "When a org file you are editing include some latex fragments,
+you want to preview them,you can convert them into png files and 
+then insert into the org buffers
+
+dvipng          Process the LaTeX fragments to dvi file, then convert
+                dvi files  to png files using dvipng. 
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf with program:
+                '(car org-latex-to-pdf-process), And then use
+                imagemagick convert pdf to png"
+
+  :group 'org-latex
+  :version "24.1"
+  :type '(choice
+	  (const :tag "dvipng" dvipng)
+	  (const :tag "imagemagick" imagemagick)))
 
 (defun org-format-latex-mathml-available-p ()
   "Return t if `org-latex-to-mathml-convert-command' is usable."
@@ -16916,7 +16933,7 @@ The images can be removed again with \\[org-ctrl-c-ctrl-c]."
 	 (concat "ltxpng/" (file-name-sans-extension
 			    (file-name-nondirectory
 			     buffer-file-name)))
-	 default-directory 'overlays msg at 'forbuffer 'dvipng)
+	 default-directory 'overlays msg at 'forbuffer org-create-formula-image-program)
       (message msg "done.  Use `C-c C-c' to remove images.")))))
 
 (defvar org-latex-regexps
@@ -16980,7 +16997,8 @@ Some of the options can be changed using the variable
 				'(org-protected t))))
 		(add-text-properties (match-beginning n) (match-end n)
 				     '(org-protected t))))
-	     ((eq processing-type 'dvipng)
+	     ((or (eq processing-type 'dvipng)
+		  (eq processing-type 'imagemagick))
 	      ;; Process to an image
 	      (setq txt (match-string n)
 		    beg (match-beginning n) end (match-end n)
@@ -17001,17 +17019,25 @@ Some of the options can be changed using the variable
 	      (unless checkdir ; make sure the directory exists
 		(setq checkdir t)
 		(or (file-directory-p todir) (make-directory todir t)))
-
-	      (unless executables-checked
-		(org-check-external-command
-		 "latex" "needed to convert LaTeX fragments to images")
-		(org-check-external-command
-		 "dvipng" "needed to convert LaTeX fragments to images")
-		(setq executables-checked t))
-
-	      (unless (file-exists-p movefile)
-		(org-create-formula-image
-		 txt movefile opt forbuffer))
+	      (cond 
+	       ((eq processing-type 'dvipng)
+		(unless executables-checked
+		  (org-check-external-command
+		   "latex" "needed to convert LaTeX fragments to images")
+		  (org-check-external-command
+		   "dvipng" "needed to convert LaTeX fragments to images")
+		  (setq executables-checked t))
+		(unless (file-exists-p movefile)
+		  (org-create-formula-image-with-dvipng
+		   txt movefile opt forbuffer)))
+	       ((eq processing-type 'imagemagick)
+		(unless executables-checked
+		  (org-check-external-command
+		   "convert" "is needed,please install imagemagick package")
+		  (setq executables-checked t))
+		(unless (file-exists-p movefile)
+		  (org-create-formula-image-with-imagemagick
+		   txt movefile opt forbuffer))))
 	      (if overlays
 		  (progn
 		    (mapc (lambda (o)
@@ -17147,7 +17173,7 @@ inspection."
       latex-frag)))
 
 ;; This function borrows from Ganesh Swami's latex2png.el
-(defun org-create-formula-image (string tofile options buffer)
+(defun org-create-formula-image-with-dvipng (string tofile options buffer)
   "This calls dvipng."
   (require 'org-latex)
   (let* ((tmpdir (if (featurep 'xemacs)
@@ -17212,6 +17238,98 @@ inspection."
 	(loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
 	      (delete-file (concat texfilebase e)))
 	pngfile))))
+(defun org-create-formula-image-with-imagemagick (string tofile options buffer)
+  "This calls convert, which is included into imagemagick."
+  (require 'org-latex)
+  (let* ((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"))
+	 (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-latex-color :foreground)))
+    (if (eq bg 'default) (setq bg (org-latex-color :background)))
+    (with-temp-file texfile
+      (insert (org-splice-latex-header
+	       org-format-latex-header
+	       org-export-latex-default-packages-alist
+	       org-export-latex-packages-alist t
+	       org-format-latex-header-extra))
+      (insert "\n\\begin{document}\n" 
+	      "\\definecolor{fg}{rgb}{" fg "}\n"
+	      "\\definecolor{bg}{rgb}{" bg "}\n"
+	      "\n\\pagecolor{bg}\n"
+	      "\n{\\color{fg}\n"
+	      string
+	      "\n}\n"
+	      "\n\\end{document}\n" )
+      (require 'org-latex)
+      (org-export-latex-fix-inputenc))
+    (let ((dir default-directory))
+      (condition-case nil
+	  (progn
+	    (cd tmpdir)
+	    (setq cmd (car org-latex-to-pdf-process))
+	    (while (string-match "%b" cmd)
+	      (setq cmd (replace-match
+			 (save-match-data
+			   (shell-quote-argument texfile))
+			 t t cmd)))
+	    (while (string-match "%f" cmd)
+	      (setq cmd (replace-match
+			 (save-match-data
+			   (shell-quote-argument (file-name-nondirectory texfile)))
+			 t t cmd)))
+	    (while (string-match "%o" cmd)
+	      (setq cmd (replace-match
+			 (save-match-data
+			   (shell-quote-argument (file-name-directory texfile)))
+			 t t cmd)))
+	    (shell-command cmd))
+	(error nil))
+      (cd dir))
+    (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
+	      (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.
@@ -17283,6 +17401,17 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML."
 					   ((eq attr :background) 'background))))
 		   (color-values (face-attribute 'default attr nil))))))
 
+(defun org-latex-color (attr)
+  "Return an rgb color specification for latex color package."
+  (apply 'format "%s,%s,%s"
+	 (mapcar 'org-normalize-color
+		 (if (featurep 'xemacs)
+		     (color-rgb-components
+		      (face-property 'default
+				     (cond ((eq attr :foreground) 'foreground)
+					   ((eq attr :background) 'background))))
+		   (color-values (face-attribute 'default attr nil))))))
+
 (defun org-normalize-color (value)
   "Return string to be used as color value for an RGB component."
   (format "%g" (/ value 65535.0)))
-- 
1.7.9.1

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [patch] Need test: New path which is used to preview latex fragements
  2012-03-25 12:36         ` FengShu
@ 2012-03-26  6:07           ` FengShu
  2012-03-27  4:23             ` [patch] Need test: New method " FengShu
  0 siblings, 1 reply; 11+ messages in thread
From: FengShu @ 2012-03-26  6:07 UTC (permalink / raw)
  To: emacs-orgmode


this is the patch v4, and I think that  it's not an experimental toy
any more (may be it's a toy :-), for its  using range), if you are
interested in it and have time, please test it. the most user I think
is the CJK people who use xetex...



From 0be0b1d4fed65a5329bf98e6a3cfb0c3d2171e2a Mon Sep 17 00:00:00 2001
From: FengShu <tumashu@gmail.com>
Date: Fri, 23 Mar 2012 16:05:37 +0800
Subject: [PATCH] This is a experimental toy ,which use '(car
 org-latex-to-pdf-process) and imagemagick to convert latex
 fragements into pngfiles,and then insert them into the
 editing buffer,you can use it to  preview latex  formula
 and so on. the converting path is "latex->pdf->png",which
 is different from the org default path: "latex->dvi->png"

---
 lisp/org-exp.el   |   16 +++--
 lisp/org-html.el  |    1 +
 lisp/org-latex.el |   76 +++++++++++++++---------
 lisp/org.el       |  164 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 4 files changed, 208 insertions(+), 49 deletions(-)

diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index eeee1ce..0317539 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -537,12 +537,15 @@ This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
 
 Allowed values are:
 
-nil        Don't do anything.
-verbatim   Keep everything in verbatim
-dvipng     Process the LaTeX fragments to images.
-           This will also include processing of non-math environments.
-t          Do MathJax preprocessing if there is at least on math snippet,
-           and arrange for MathJax.js to be loaded.
+nil             Don't do anything.
+verbatim        Keep everything in verbatim
+dvipng          Process the LaTeX fragments to images.
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf with program:
+                '(car org-latex-to-pdf-process), And then use
+                imagemagick convert pdf to png
+t               Do MathJax preprocessing if there is at least on math snippet,
+                and arrange for MathJax.js to be loaded.
 
 The default is nil, because this option needs the `dvipng' program which
 is not available on all systems."
@@ -552,6 +555,7 @@ is not available on all systems."
 	  (const :tag "Do not process math in any way" nil)
 	  (const :tag "Obsolete, use dvipng setting" t)
 	  (const :tag "Use dvipng to make images" dvipng)
+	  (const :tag "Use imagemagick to make images" imagemagick)
 	  (const :tag "Use MathJax to display math" mathjax)
 	  (const :tag "Leave math verbatim" verbatim)))
 
diff --git a/lisp/org-html.el b/lisp/org-html.el
index 2de2ea9..15a6c3e 100644
--- a/lisp/org-html.el
+++ b/lisp/org-html.el
@@ -685,6 +685,7 @@ The default is an extended format of the ISO 8601 specification."
       ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
       ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
       ((eq (plist-get parameters :LaTeX-fragments) t        ) 'mathjax)
+      ((eq (plist-get parameters :LaTeX-fragments) 'imagemagick  ) 'imagemagick)
       ((eq (plist-get parameters :LaTeX-fragments) 'dvipng  ) 'dvipng)
       (t nil))))
   (goto-char (point-min))
diff --git a/lisp/org-latex.el b/lisp/org-latex.el
index a733f31..5f2258a 100644
--- a/lisp/org-latex.el
+++ b/lisp/org-latex.el
@@ -631,12 +631,21 @@ allowed.  The default we use here encompasses both."
 (defcustom org-latex-to-pdf-process
   '("pdflatex -interaction nonstopmode -output-directory %o %f"
     "pdflatex -interaction nonstopmode -output-directory %o %f"
-    "pdflatex -interaction nonstopmode -output-directory %o %f")
-  "Commands to process a LaTeX file to a PDF file.
-This is a list of strings, each of them will be given to the shell
-as a command.  %f in the command will be replaced by the full file name, %b
-by the file base name (i.e. without extension) and %o by the base directory
-of the file.
+    "pdflatex -interaction nonstopmode -output-directory %o %f"
+    ("pdflatex -interaction nonstopmode -output-directory %o %f"))
+  "Commands to process a LaTeX file to a PDF file and 
+commands to process latex fragments to pdf files 
+This is a list of strings and a sub-list, the strings of the sub-list 
+is used to convert latex fragments in editing org buffer which will 
+be converted to png files and inserted into the editing buffer for 
+previewing. each of strings (list or sublist) will be given to the 
+shell as a command. %f in the command will be replaced by the full 
+file name, %b by the file base name (i.e. without extension) and %o 
+by the base directory of the file.
+
+; If having  no sublist, the default command which is used to convert
+; latex fragments in editing buffer for previewing will be the first
+; string of list (No code available  at this time!) 
 
 The reason why this is a list is that it usually takes several runs of
 `pdflatex', maybe mixed with a call to `bibtex'.  Org does not have a clever
@@ -661,30 +670,37 @@ This function should accept the file name as its single argument."
 		  (string :tag "Shell command"))
 	  (const :tag "2 runs of pdflatex"
 		 ("pdflatex -interaction nonstopmode -output-directory %o %f"
-		   "pdflatex -interaction nonstopmode -output-directory %o %f"))
+		   "pdflatex -interaction nonstopmode -output-directory %o %f"
+		   ("pdflatex -interaction nonstopmode -output-directory %o %f")))
 	  (const :tag "3 runs of pdflatex"
 		 ("pdflatex -interaction nonstopmode -output-directory %o %f"
 		   "pdflatex -interaction nonstopmode -output-directory %o %f"
-		   "pdflatex -interaction nonstopmode -output-directory %o %f"))
+		   "pdflatex -interaction nonstopmode -output-directory %o %f"
+		   ("pdflatex -interaction nonstopmode -output-directory %o %f")))
 	  (const :tag "pdflatex,bibtex,pdflatex,pdflatex"
 		 ("pdflatex -interaction nonstopmode -output-directory %o %f"
 		   "bibtex %b"
 		   "pdflatex -interaction nonstopmode -output-directory %o %f"
-		   "pdflatex -interaction nonstopmode -output-directory %o %f"))
+		   "pdflatex -interaction nonstopmode -output-directory %o %f"
+		   ("pdflatex -interaction nonstopmode -output-directory %o %f")))
 	  (const :tag "2 runs of xelatex"
 		 ("xelatex -interaction nonstopmode -output-directory %o %f"
-		   "xelatex -interaction nonstopmode -output-directory %o %f"))
+		   "xelatex -interaction nonstopmode -output-directory %o %f"
+		   ("xelatex -interaction nonstopmode -output-directory %o %f")))
 	  (const :tag "3 runs of xelatex"
 		 ("xelatex -interaction nonstopmode -output-directory %o %f"
 		   "xelatex -interaction nonstopmode -output-directory %o %f"
-		   "xelatex -interaction nonstopmode -output-directory %o %f"))
+		   "xelatex -interaction nonstopmode -output-directory %o %f"
+		   ("xelatex -interaction nonstopmode -output-directory %o %f")))
 	  (const :tag "xelatex,bibtex,xelatex,xelatex"
 		 ("xelatex -interaction nonstopmode -output-directory %o %f"
 		   "bibtex %b"
 		   "xelatex -interaction nonstopmode -output-directory %o %f"
-		   "xelatex -interaction nonstopmode -output-directory %o %f"))
+		   "xelatex -interaction nonstopmode -output-directory %o %f"
+		   ("xelatex -interaction nonstopmode -output-directory %o %f")))
 	  (const :tag "texi2dvi"
-		 ("texi2dvi -p -b -c -V %f"))
+		 ("texi2dvi -p -b -c -V %f"
+		  ("texi2dvi -p -b -c -V %f")))
 	  (const :tag "rubber"
 		 ("rubber -d --into %o %f"))
 	  (function)))
@@ -1084,22 +1100,24 @@ when PUB-DIR is set, use this as the publishing directory."
 	    (funcall cmds (shell-quote-argument file))
 	  (while cmds
 	    (setq cmd (pop cmds))
-	    (while (string-match "%b" cmd)
-	      (setq cmd (replace-match
-			 (save-match-data
-			   (shell-quote-argument base))
-			 t t cmd)))
-	    (while (string-match "%f" cmd)
-	      (setq cmd (replace-match
-			 (save-match-data
-			   (shell-quote-argument file))
-			 t t cmd)))
-	    (while (string-match "%o" cmd)
-	      (setq cmd (replace-match
-			 (save-match-data
-			   (shell-quote-argument output-dir))
-			 t t cmd)))
-	    (shell-command cmd outbuf)))))
+	    (cond 
+	     ((not (listp cmd))
+	      (while (string-match "%b" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument base))
+			   t t cmd)))
+	      (while (string-match "%f" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument file))
+			   t t cmd)))
+	      (while (string-match "%o" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument output-dir))
+			   t t cmd)))
+	      (shell-command cmd outbuf)))))))
     (message (concat "Processing LaTeX file " file "...done"))
     (setq errors (org-export-latex-get-error outbuf))
     (if (not (file-exists-p pdffile))
diff --git a/lisp/org.el b/lisp/org.el
index 159bb7f..4f60ae7 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3389,6 +3389,23 @@ When using MathToWeb as the converter, set this to
   :type '(choice
 	  (const :tag "None" nil)
 	  (string :tag "\nShell command")))
+(defcustom org-create-formula-image-program 'dvipng
+  "When a org file you are editing include some latex fragments,
+you want to preview them,you can convert them into png files and 
+then insert into the org buffers
+
+dvipng          Process the LaTeX fragments to dvi file, then convert
+                dvi files  to png files using dvipng. 
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf with program:
+                '(car org-latex-to-pdf-process), And then use
+                imagemagick convert pdf to png"
+
+  :group 'org-latex
+  :version "24.1"
+  :type '(choice
+	  (const :tag "dvipng" dvipng)
+	  (const :tag "imagemagick" imagemagick)))
 
 (defun org-format-latex-mathml-available-p ()
   "Return t if `org-latex-to-mathml-convert-command' is usable."
@@ -16916,7 +16933,7 @@ The images can be removed again with \\[org-ctrl-c-ctrl-c]."
 	 (concat "ltxpng/" (file-name-sans-extension
 			    (file-name-nondirectory
 			     buffer-file-name)))
-	 default-directory 'overlays msg at 'forbuffer 'dvipng)
+	 default-directory 'overlays msg at 'forbuffer org-create-formula-image-program)
       (message msg "done.  Use `C-c C-c' to remove images.")))))
 
 (defvar org-latex-regexps
@@ -16980,7 +16997,8 @@ Some of the options can be changed using the variable
 				'(org-protected t))))
 		(add-text-properties (match-beginning n) (match-end n)
 				     '(org-protected t))))
-	     ((eq processing-type 'dvipng)
+	     ((or (eq processing-type 'dvipng)
+		  (eq processing-type 'imagemagick))
 	      ;; Process to an image
 	      (setq txt (match-string n)
 		    beg (match-beginning n) end (match-end n)
@@ -17001,17 +17019,25 @@ Some of the options can be changed using the variable
 	      (unless checkdir ; make sure the directory exists
 		(setq checkdir t)
 		(or (file-directory-p todir) (make-directory todir t)))
-
-	      (unless executables-checked
-		(org-check-external-command
-		 "latex" "needed to convert LaTeX fragments to images")
-		(org-check-external-command
-		 "dvipng" "needed to convert LaTeX fragments to images")
-		(setq executables-checked t))
-
-	      (unless (file-exists-p movefile)
-		(org-create-formula-image
-		 txt movefile opt forbuffer))
+	      (cond 
+	       ((eq processing-type 'dvipng)
+		(unless executables-checked
+		  (org-check-external-command
+		   "latex" "needed to convert LaTeX fragments to images")
+		  (org-check-external-command
+		   "dvipng" "needed to convert LaTeX fragments to images")
+		  (setq executables-checked t))
+		(unless (file-exists-p movefile)
+		  (org-create-formula-image-with-dvipng
+		   txt movefile opt forbuffer)))
+	       ((eq processing-type 'imagemagick)
+		(unless executables-checked
+		  (org-check-external-command
+		   "convert" "is needed,please install imagemagick package")
+		  (setq executables-checked t))
+		(unless (file-exists-p movefile)
+		  (org-create-formula-image-with-imagemagick
+		   txt movefile opt forbuffer))))
 	      (if overlays
 		  (progn
 		    (mapc (lambda (o)
@@ -17147,7 +17173,7 @@ inspection."
       latex-frag)))
 
 ;; This function borrows from Ganesh Swami's latex2png.el
-(defun org-create-formula-image (string tofile options buffer)
+(defun org-create-formula-image-with-dvipng (string tofile options buffer)
   "This calls dvipng."
   (require 'org-latex)
   (let* ((tmpdir (if (featurep 'xemacs)
@@ -17212,6 +17238,105 @@ inspection."
 	(loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
 	      (delete-file (concat texfilebase e)))
 	pngfile))))
+(defun org-create-formula-image-with-imagemagick (string tofile options buffer)
+  "This calls convert, which is included into imagemagick."
+  (require 'org-latex)
+  (let* ((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"))
+	 (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-latex-color :foreground)))
+    (if (eq bg 'default) (setq bg (org-latex-color :background)))
+    (with-temp-file texfile
+      (insert (org-splice-latex-header
+	       org-format-latex-header
+	       org-export-latex-default-packages-alist
+	       org-export-latex-packages-alist t
+	       org-format-latex-header-extra))
+      (insert "\n\\begin{document}\n" 
+	      "\\definecolor{fg}{rgb}{" fg "}\n"
+	      "\\definecolor{bg}{rgb}{" bg "}\n"
+	      "\n\\pagecolor{bg}\n"
+	      "\n{\\color{fg}\n"
+	      string
+	      "\n}\n"
+	      "\n\\end{document}\n" )
+      (require 'org-latex)
+      (org-export-latex-fix-inputenc))
+    (let ((dir default-directory))
+      (condition-case nil
+	  (progn
+	    (cd tmpdir)
+	    (setq cmds org-latex-to-pdf-process)
+	    (while cmds
+	      (setq latex-frags-cmds (pop cmds))
+	      (cond 
+	       ((listp latex-frags-cmds) 
+		(while latex-frags-cmds
+		  (setq cmd (pop latex-frags-cmds))
+		  (while (string-match "%b" cmd)
+		    (setq cmd (replace-match
+			       (save-match-data
+				 (shell-quote-argument texfile))
+			       t t cmd)))
+		  (while (string-match "%f" cmd)
+		    (setq cmd (replace-match
+			       (save-match-data
+				 (shell-quote-argument (file-name-nondirectory texfile)))
+			       t t cmd)))
+		  (while (string-match "%o" cmd)
+		    (setq cmd (replace-match
+			       (save-match-data
+				 (shell-quote-argument (file-name-directory texfile)))
+			       t t cmd)))
+		  (message "%s" cmd)
+		  (shell-command cmd))))))
+	    (error nil))
+	(cd dir))
+    (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
+	      (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.
@@ -17283,6 +17408,17 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML."
 					   ((eq attr :background) 'background))))
 		   (color-values (face-attribute 'default attr nil))))))
 
+(defun org-latex-color (attr)
+  "Return an rgb color specification for latex color package."
+  (apply 'format "%s,%s,%s"
+	 (mapcar 'org-normalize-color
+		 (if (featurep 'xemacs)
+		     (color-rgb-components
+		      (face-property 'default
+				     (cond ((eq attr :foreground) 'foreground)
+					   ((eq attr :background) 'background))))
+		   (color-values (face-attribute 'default attr nil))))))
+
 (defun org-normalize-color (value)
   "Return string to be used as color value for an RGB component."
   (format "%g" (/ value 65535.0)))
-- 
1.7.9.1

  
 

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [patch] Need test: New method which is used to preview latex fragements
  2012-03-26  6:07           ` [patch] Need test: New path " FengShu
@ 2012-03-27  4:23             ` FengShu
  2012-03-27  8:16               ` Bastien
  0 siblings, 1 reply; 11+ messages in thread
From: FengShu @ 2012-03-27  4:23 UTC (permalink / raw)
  To: emacs-orgmode


this is v5 patch, if you want to use it,you can apply the patch and
set like this:

#+begin_src emacs-lisp
  (setq org-create-formula-image-program 'imagemagick)
  (setq org-export-with-LaTeX-fragments 'imagemagick)
#+end_src 


from cdf2a649e4a4583c855b538cac51b6257ca95817 Mon Sep 17 00:00:00 2001
From: FengShu <tumashu@gmail.com>
Date: Fri, 23 Mar 2012 16:05:37 +0800
Subject: [PATCH] Adding new method for previewing latex fragments,which
 convert latex fragments into pdf files,then use imagemagick
 convert the pdf files into png files,and then insert them
 into the editing buffer for the latex fomula previewing.

the converting path is "latex->pdf->png",which is different from the
default path: "latex->dvi->png"
---
 lisp/org-exp.el   |   15 +++--
 lisp/org-html.el  |    1 +
 lisp/org-latex.el |   57 +++++++++++-------
 lisp/org.el       |  174 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 4 files changed, 205 insertions(+), 42 deletions(-)

diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index eeee1ce..ccf2247 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -537,12 +537,14 @@ This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
 
 Allowed values are:
 
-nil        Don't do anything.
-verbatim   Keep everything in verbatim
-dvipng     Process the LaTeX fragments to images.
-           This will also include processing of non-math environments.
-t          Do MathJax preprocessing if there is at least on math snippet,
-           and arrange for MathJax.js to be loaded.
+nil             Don't do anything.
+verbatim        Keep everything in verbatim
+dvipng          Process the LaTeX fragments to images.
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf files and then use imagemagick 
+                convert pdf files to png files
+t               Do MathJax preprocessing if there is at least on math snippet,
+                and arrange for MathJax.js to be loaded.
 
 The default is nil, because this option needs the `dvipng' program which
 is not available on all systems."
@@ -552,6 +554,7 @@ is not available on all systems."
 	  (const :tag "Do not process math in any way" nil)
 	  (const :tag "Obsolete, use dvipng setting" t)
 	  (const :tag "Use dvipng to make images" dvipng)
+	  (const :tag "Use imagemagick to make images" imagemagick)
 	  (const :tag "Use MathJax to display math" mathjax)
 	  (const :tag "Leave math verbatim" verbatim)))
 
diff --git a/lisp/org-html.el b/lisp/org-html.el
index 2de2ea9..15a6c3e 100644
--- a/lisp/org-html.el
+++ b/lisp/org-html.el
@@ -685,6 +685,7 @@ The default is an extended format of the ISO 8601 specification."
       ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
       ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
       ((eq (plist-get parameters :LaTeX-fragments) t        ) 'mathjax)
+      ((eq (plist-get parameters :LaTeX-fragments) 'imagemagick  ) 'imagemagick)
       ((eq (plist-get parameters :LaTeX-fragments) 'dvipng  ) 'dvipng)
       (t nil))))
   (goto-char (point-min))
diff --git a/lisp/org-latex.el b/lisp/org-latex.el
index a733f31..51347b8 100644
--- a/lisp/org-latex.el
+++ b/lisp/org-latex.el
@@ -632,11 +632,24 @@ allowed.  The default we use here encompasses both."
   '("pdflatex -interaction nonstopmode -output-directory %o %f"
     "pdflatex -interaction nonstopmode -output-directory %o %f"
     "pdflatex -interaction nonstopmode -output-directory %o %f")
-  "Commands to process a LaTeX file to a PDF file.
-This is a list of strings, each of them will be given to the shell
-as a command.  %f in the command will be replaced by the full file name, %b
-by the file base name (i.e. without extension) and %o by the base directory
-of the file.
+  "Commands to process a LaTeX file to a PDF file and process latex
+fragments to pdf files.By default,this is a list of strings,and each of
+strings will be given to the shell as a command. %f in the command will
+be replaced by the full file name, %b by the file base name (i.e. without
+extension) and %o by the base directory of the file.
+
+If you set 'org-create-formula-image-program to 'imagemagick or 
+set 'org-export-with-LaTeX-fragments to 'imagemagick,You can add a 
+sub-list which contains your own command(s) for latex fragments 
+previewing,like this:
+
+   '(\"xelatex -interaction nonstopmode -output-directory %o %f\"
+     \"xelatex -interaction nonstopmode -output-directory %o %f\"
+     ;; use below command(s) to convert latex fragments
+     (\"xelatex %f\"))
+
+If not having any sublist ,the default command which is used to convert 
+latex fragments will be the first string of the list. 
 
 The reason why this is a list is that it usually takes several runs of
 `pdflatex', maybe mixed with a call to `bibtex'.  Org does not have a clever
@@ -1084,22 +1097,24 @@ when PUB-DIR is set, use this as the publishing directory."
 	    (funcall cmds (shell-quote-argument file))
 	  (while cmds
 	    (setq cmd (pop cmds))
-	    (while (string-match "%b" cmd)
-	      (setq cmd (replace-match
-			 (save-match-data
-			   (shell-quote-argument base))
-			 t t cmd)))
-	    (while (string-match "%f" cmd)
-	      (setq cmd (replace-match
-			 (save-match-data
-			   (shell-quote-argument file))
-			 t t cmd)))
-	    (while (string-match "%o" cmd)
-	      (setq cmd (replace-match
-			 (save-match-data
-			   (shell-quote-argument output-dir))
-			 t t cmd)))
-	    (shell-command cmd outbuf)))))
+	    (cond 
+	     ((not (listp cmd))
+	      (while (string-match "%b" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument base))
+			   t t cmd)))
+	      (while (string-match "%f" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument file))
+			   t t cmd)))
+	      (while (string-match "%o" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument output-dir))
+			   t t cmd)))
+	      (shell-command cmd outbuf)))))))
     (message (concat "Processing LaTeX file " file "...done"))
     (setq errors (org-export-latex-get-error outbuf))
     (if (not (file-exists-p pdffile))
diff --git a/lisp/org.el b/lisp/org.el
index 159bb7f..67ad5c6 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3389,6 +3389,22 @@ When using MathToWeb as the converter, set this to
   :type '(choice
 	  (const :tag "None" nil)
 	  (string :tag "\nShell command")))
+(defcustom org-create-formula-image-program 'dvipng
+  "When a org file you are editing include some latex fragments,
+you want to preview them,you can convert them into png files and 
+then insert into the org buffers
+
+dvipng          Process the LaTeX fragments to dvi file, then convert
+                dvi files  to png files using dvipng. 
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf files and then use
+                imagemagick convert pdf files to png files"
+
+  :group 'org-latex
+  :version "24.1"
+  :type '(choice
+	  (const :tag "dvipng" dvipng)
+	  (const :tag "imagemagick" imagemagick)))
 
 (defun org-format-latex-mathml-available-p ()
   "Return t if `org-latex-to-mathml-convert-command' is usable."
@@ -16916,7 +16932,7 @@ The images can be removed again with \\[org-ctrl-c-ctrl-c]."
 	 (concat "ltxpng/" (file-name-sans-extension
 			    (file-name-nondirectory
 			     buffer-file-name)))
-	 default-directory 'overlays msg at 'forbuffer 'dvipng)
+	 default-directory 'overlays msg at 'forbuffer org-create-formula-image-program)
       (message msg "done.  Use `C-c C-c' to remove images.")))))
 
 (defvar org-latex-regexps
@@ -16980,7 +16996,8 @@ Some of the options can be changed using the variable
 				'(org-protected t))))
 		(add-text-properties (match-beginning n) (match-end n)
 				     '(org-protected t))))
-	     ((eq processing-type 'dvipng)
+	     ((or (eq processing-type 'dvipng)
+		  (eq processing-type 'imagemagick))
 	      ;; Process to an image
 	      (setq txt (match-string n)
 		    beg (match-beginning n) end (match-end n)
@@ -17001,17 +17018,25 @@ Some of the options can be changed using the variable
 	      (unless checkdir ; make sure the directory exists
 		(setq checkdir t)
 		(or (file-directory-p todir) (make-directory todir t)))
-
-	      (unless executables-checked
-		(org-check-external-command
-		 "latex" "needed to convert LaTeX fragments to images")
-		(org-check-external-command
-		 "dvipng" "needed to convert LaTeX fragments to images")
-		(setq executables-checked t))
-
-	      (unless (file-exists-p movefile)
-		(org-create-formula-image
-		 txt movefile opt forbuffer))
+	      (cond 
+	       ((eq processing-type 'dvipng)
+		(unless executables-checked
+		  (org-check-external-command
+		   "latex" "needed to convert LaTeX fragments to images")
+		  (org-check-external-command
+		   "dvipng" "needed to convert LaTeX fragments to images")
+		  (setq executables-checked t))
+		(unless (file-exists-p movefile)
+		  (org-create-formula-image-with-dvipng
+		   txt movefile opt forbuffer)))
+	       ((eq processing-type 'imagemagick)
+		(unless executables-checked
+		  (org-check-external-command
+		   "convert" "is needed,please install imagemagick package")
+		  (setq executables-checked t))
+		(unless (file-exists-p movefile)
+		  (org-create-formula-image-with-imagemagick
+		   txt movefile opt forbuffer))))
 	      (if overlays
 		  (progn
 		    (mapc (lambda (o)
@@ -17147,7 +17172,7 @@ inspection."
       latex-frag)))
 
 ;; This function borrows from Ganesh Swami's latex2png.el
-(defun org-create-formula-image (string tofile options buffer)
+(defun org-create-formula-image-with-dvipng (string tofile options buffer)
   "This calls dvipng."
   (require 'org-latex)
   (let* ((tmpdir (if (featurep 'xemacs)
@@ -17209,7 +17234,109 @@ inspection."
 	    nil)
 	;; Use the requested file name and clean up
 	(copy-file pngfile tofile 'replace)
-	(loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
+	(loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
+	      (delete-file (concat texfilebase e)))
+	pngfile))))
+(defun org-create-formula-image-with-imagemagick (string tofile options buffer)
+  "This calls convert, which is included into imagemagick."
+  (require 'org-latex)
+  (let* ((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"))
+	 (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))
+		 "white")))
+    (if (eq fg 'default) (setq fg (org-latex-color :foreground))
+      (setq fg (org-latex-color-format fg)))
+    (if (eq bg 'default) (setq bg (org-latex-color :background))
+      (setq bg (org-latex-color-format
+		(if (string= bg "Transparent")(setq bg "white")))))
+    (with-temp-file texfile
+      (insert (org-splice-latex-header
+	       org-format-latex-header
+	       org-export-latex-default-packages-alist
+	       org-export-latex-packages-alist t
+	       org-format-latex-header-extra))
+      (insert "\n\\begin{document}\n" 
+	      "\\definecolor{fg}{rgb}{" fg "}\n"
+	      "\\definecolor{bg}{rgb}{" bg "}\n"
+	      "\n\\pagecolor{bg}\n"
+	      "\n{\\color{fg}\n"
+	      string
+	      "\n}\n"
+	      "\n\\end{document}\n" )
+      (require 'org-latex)
+      (org-export-latex-fix-inputenc))
+    (let ((dir default-directory))
+      (condition-case nil
+	  (progn
+	    (cd tmpdir)
+	    (setq cmds org-latex-to-pdf-process)
+	    (while cmds
+	      (setq latex-frags-cmds (pop cmds))
+	      (if (listp latex-frags-cmds)
+		  (setq cmds nil)
+		(setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
+	    (while latex-frags-cmds
+	      (setq cmd (pop latex-frags-cmds))
+	      (while (string-match "%b" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument texfile))
+			   t t cmd)))
+	      (while (string-match "%f" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument (file-name-nondirectory texfile)))
+			   t t cmd)))
+	      (while (string-match "%o" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument (file-name-directory texfile)))
+			   t t cmd)))
+	      (shell-command cmd)))
+	(error nil))
+      (cd dir))
+    (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" ".out") do
 	      (delete-file (concat texfilebase e)))
 	pngfile))))
 
@@ -17283,6 +17410,23 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML."
 					   ((eq attr :background) 'background))))
 		   (color-values (face-attribute 'default attr nil))))))
 
+(defun org-latex-color (attr)
+  "Return an rgb color specification for latex color package."
+  (apply 'format "%s,%s,%s"
+	 (mapcar 'org-normalize-color
+		 (if (featurep 'xemacs)
+		     (color-rgb-components
+		      (face-property 'default
+				     (cond ((eq attr :foreground) 'foreground)
+					   ((eq attr :background) 'background))))
+		   (color-values (face-attribute 'default attr nil))))))
+
+(defun org-latex-color-format (color-name)
+  "Convert color name to RGB value with format num1,num2,num3"
+  (apply 'format "%s,%s,%s"
+	 (mapcar 'org-normalize-color
+		   (color-values color-name))))
+
 (defun org-normalize-color (value)
   "Return string to be used as color value for an RGB component."
   (format "%g" (/ value 65535.0)))
-- 
1.7.9.1

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [patch] Need test: New method which is used to preview latex fragements
  2012-03-27  4:23             ` [patch] Need test: New method " FengShu
@ 2012-03-27  8:16               ` Bastien
       [not found]                 ` <87haxapcof.fsf@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread
From: Bastien @ 2012-03-27  8:16 UTC (permalink / raw)
  To: FengShu; +Cc: emacs-orgmode

Hi,

FengShu <tumashu@gmail.com> writes:

> this is v5 patch, if you want to use it,you can apply the patch and
> set like this:

since you're still working on this patch, some advice:

- use a shorter line for the overall description

- create an Emacs-ready ChangeLog with `C-x 4 a' that will be the
  content of the commit message.

I had a quick look at the patch and this looks like a great addition --
*BUT* you need to sign FSF papers in order for this patch to be applied
to master.  I'm sending instructions in private.

Thanks for your work!

-- 
 Bastien

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [patch] Need test: New method which is used to preview latex fragements
       [not found]                       ` <87vclq401w.fsf@gnu.org>
@ 2012-03-27 23:48                         ` Feng Shu
  2012-04-23 13:10                           ` Bastien
  0 siblings, 1 reply; 11+ messages in thread
From: Feng Shu @ 2012-03-27 23:48 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 85 bytes --]


Sorry, I change my name in commit from FengShu to Feng Shu.
Thanks for your help!



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Adding-a-new-method-for-previewing-latex-fragements.patch --]
[-- Type: text/x-diff, Size: 15583 bytes --]

From ef9c4ad164161c5dfde01dd5d8c3221163adb231 Mon Sep 17 00:00:00 2001
From: Feng Shu <tumashu@gmail.com>
Date: Wed, 28 Mar 2012 07:40:04 +0800
Subject: [PATCH] Adding a new method for previewing latex fragements.

	* org-latex.el (org-latex-to-pdf-process): Get the latex fragements
	converting command(s) from this variable, now you can save your
	own latex fragments converting command(s) into this variable.

	* org-latex.el (org-export-as-pdf): Deal with the changes of
	'org-latex-to-pdf-process.

	* org.el (org-create-formula-image-program): New defcustom, which will be
	used to set the command converting pdf or dvi files to  png files.

	* org.el (org-create-formula-image): Change its name to
	'org-create-formula-image-with-dvipng.

	* org.el (org-create-formula-image-with-imagemagick): New function by
	which a latex fragment can be convert to a pdf file and then
	convert to a png file.

	* org.el (org-latex-color): New function, which will get an rgb color
	specification from default background or foreground, and using
	this specification, we can define a latex color.

	* org.el (org-latex-color-format): New fuction, which can translate a
	color-name to specification, and using this specification, we can define a
	latex color.

	* org.el (org-format-latex): Add 'imagemagick support.
	* org-exp.el (org-export-with-LaTeX-fragments): Add 'imagemagick support.
	* org-html.el (org-export-html-preprocess): Add 'imagemagick support.
---
 lisp/org-exp.el   |   15 +++--
 lisp/org-html.el  |    1 +
 lisp/org-latex.el |   57 +++++++++++-------
 lisp/org.el       |  174 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 4 files changed, 205 insertions(+), 42 deletions(-)

diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index eeee1ce..ccf2247 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -537,12 +537,14 @@ This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
 
 Allowed values are:
 
-nil        Don't do anything.
-verbatim   Keep everything in verbatim
-dvipng     Process the LaTeX fragments to images.
-           This will also include processing of non-math environments.
-t          Do MathJax preprocessing if there is at least on math snippet,
-           and arrange for MathJax.js to be loaded.
+nil             Don't do anything.
+verbatim        Keep everything in verbatim
+dvipng          Process the LaTeX fragments to images.
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf files and then use imagemagick 
+                convert pdf files to png files
+t               Do MathJax preprocessing if there is at least on math snippet,
+                and arrange for MathJax.js to be loaded.
 
 The default is nil, because this option needs the `dvipng' program which
 is not available on all systems."
@@ -552,6 +554,7 @@ is not available on all systems."
 	  (const :tag "Do not process math in any way" nil)
 	  (const :tag "Obsolete, use dvipng setting" t)
 	  (const :tag "Use dvipng to make images" dvipng)
+	  (const :tag "Use imagemagick to make images" imagemagick)
 	  (const :tag "Use MathJax to display math" mathjax)
 	  (const :tag "Leave math verbatim" verbatim)))
 
diff --git a/lisp/org-html.el b/lisp/org-html.el
index 2de2ea9..15a6c3e 100644
--- a/lisp/org-html.el
+++ b/lisp/org-html.el
@@ -685,6 +685,7 @@ The default is an extended format of the ISO 8601 specification."
       ((eq (plist-get parameters :LaTeX-fragments) 'verbatim) 'verbatim)
       ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
       ((eq (plist-get parameters :LaTeX-fragments) t        ) 'mathjax)
+      ((eq (plist-get parameters :LaTeX-fragments) 'imagemagick  ) 'imagemagick)
       ((eq (plist-get parameters :LaTeX-fragments) 'dvipng  ) 'dvipng)
       (t nil))))
   (goto-char (point-min))
diff --git a/lisp/org-latex.el b/lisp/org-latex.el
index 4418dee..47be65d 100644
--- a/lisp/org-latex.el
+++ b/lisp/org-latex.el
@@ -632,11 +632,24 @@ allowed.  The default we use here encompasses both."
   '("pdflatex -interaction nonstopmode -output-directory %o %f"
     "pdflatex -interaction nonstopmode -output-directory %o %f"
     "pdflatex -interaction nonstopmode -output-directory %o %f")
-  "Commands to process a LaTeX file to a PDF file.
-This is a list of strings, each of them will be given to the shell
-as a command.  %f in the command will be replaced by the full file name, %b
-by the file base name (i.e. without extension) and %o by the base directory
-of the file.
+  "Commands to process a LaTeX file to a PDF file and process latex
+fragments to pdf files.By default,this is a list of strings,and each of
+strings will be given to the shell as a command. %f in the command will
+be replaced by the full file name, %b by the file base name (i.e. without
+extension) and %o by the base directory of the file.
+
+If you set 'org-create-formula-image-program to 'imagemagick or 
+set 'org-export-with-LaTeX-fragments to 'imagemagick,You can add a 
+sub-list which contains your own command(s) for latex fragments 
+previewing,like this:
+
+   '(\"xelatex -interaction nonstopmode -output-directory %o %f\"
+     \"xelatex -interaction nonstopmode -output-directory %o %f\"
+     ;; use below command(s) to convert latex fragments
+     (\"xelatex %f\"))
+
+If not having any sublist ,the default command which is used to convert 
+latex fragments will be the first string of the list. 
 
 The reason why this is a list is that it usually takes several runs of
 `pdflatex', maybe mixed with a call to `bibtex'.  Org does not have a clever
@@ -1084,22 +1097,24 @@ when PUB-DIR is set, use this as the publishing directory."
 	    (funcall cmds (shell-quote-argument file))
 	  (while cmds
 	    (setq cmd (pop cmds))
-	    (while (string-match "%b" cmd)
-	      (setq cmd (replace-match
-			 (save-match-data
-			   (shell-quote-argument base))
-			 t t cmd)))
-	    (while (string-match "%f" cmd)
-	      (setq cmd (replace-match
-			 (save-match-data
-			   (shell-quote-argument file))
-			 t t cmd)))
-	    (while (string-match "%o" cmd)
-	      (setq cmd (replace-match
-			 (save-match-data
-			   (shell-quote-argument output-dir))
-			 t t cmd)))
-	    (shell-command cmd outbuf)))))
+	    (cond 
+	     ((not (listp cmd))
+	      (while (string-match "%b" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument base))
+			   t t cmd)))
+	      (while (string-match "%f" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument file))
+			   t t cmd)))
+	      (while (string-match "%o" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument output-dir))
+			   t t cmd)))
+	      (shell-command cmd outbuf)))))))
     (message (concat "Processing LaTeX file " file "...done"))
     (setq errors (org-export-latex-get-error outbuf))
     (if (not (file-exists-p pdffile))
diff --git a/lisp/org.el b/lisp/org.el
index d3b1ddc..d4b5c8a 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3389,6 +3389,22 @@ When using MathToWeb as the converter, set this to
   :type '(choice
 	  (const :tag "None" nil)
 	  (string :tag "\nShell command")))
+(defcustom org-create-formula-image-program 'dvipng
+  "When a org file you are editing include some latex fragments,
+you want to preview them,you can convert them into png files and 
+then insert into the org buffers
+
+dvipng          Process the LaTeX fragments to dvi file, then convert
+                dvi files  to png files using dvipng. 
+                This will also include processing of non-math environments.
+imagemagick     Convert the LaTeX fragments to pdf files and then use
+                imagemagick convert pdf files to png files"
+
+  :group 'org-latex
+  :version "24.1"
+  :type '(choice
+	  (const :tag "dvipng" dvipng)
+	  (const :tag "imagemagick" imagemagick)))
 
 (defun org-format-latex-mathml-available-p ()
   "Return t if `org-latex-to-mathml-convert-command' is usable."
@@ -16896,7 +16912,7 @@ The images can be removed again with \\[org-ctrl-c-ctrl-c]."
 	 (concat "ltxpng/" (file-name-sans-extension
 			    (file-name-nondirectory
 			     buffer-file-name)))
-	 default-directory 'overlays msg at 'forbuffer 'dvipng)
+	 default-directory 'overlays msg at 'forbuffer org-create-formula-image-program)
       (message msg "done.  Use `C-c C-c' to remove images.")))))
 
 (defvar org-latex-regexps
@@ -16960,7 +16976,8 @@ Some of the options can be changed using the variable
 				'(org-protected t))))
 		(add-text-properties (match-beginning n) (match-end n)
 				     '(org-protected t))))
-	     ((eq processing-type 'dvipng)
+	     ((or (eq processing-type 'dvipng)
+		  (eq processing-type 'imagemagick))
 	      ;; Process to an image
 	      (setq txt (match-string n)
 		    beg (match-beginning n) end (match-end n)
@@ -16981,17 +16998,25 @@ Some of the options can be changed using the variable
 	      (unless checkdir ; make sure the directory exists
 		(setq checkdir t)
 		(or (file-directory-p todir) (make-directory todir t)))
-
-	      (unless executables-checked
-		(org-check-external-command
-		 "latex" "needed to convert LaTeX fragments to images")
-		(org-check-external-command
-		 "dvipng" "needed to convert LaTeX fragments to images")
-		(setq executables-checked t))
-
-	      (unless (file-exists-p movefile)
-		(org-create-formula-image
-		 txt movefile opt forbuffer))
+	      (cond 
+	       ((eq processing-type 'dvipng)
+		(unless executables-checked
+		  (org-check-external-command
+		   "latex" "needed to convert LaTeX fragments to images")
+		  (org-check-external-command
+		   "dvipng" "needed to convert LaTeX fragments to images")
+		  (setq executables-checked t))
+		(unless (file-exists-p movefile)
+		  (org-create-formula-image-with-dvipng
+		   txt movefile opt forbuffer)))
+	       ((eq processing-type 'imagemagick)
+		(unless executables-checked
+		  (org-check-external-command
+		   "convert" "is needed,please install imagemagick package")
+		  (setq executables-checked t))
+		(unless (file-exists-p movefile)
+		  (org-create-formula-image-with-imagemagick
+		   txt movefile opt forbuffer))))
 	      (if overlays
 		  (progn
 		    (mapc (lambda (o)
@@ -17127,7 +17152,7 @@ inspection."
       latex-frag)))
 
 ;; This function borrows from Ganesh Swami's latex2png.el
-(defun org-create-formula-image (string tofile options buffer)
+(defun org-create-formula-image-with-dvipng (string tofile options buffer)
   "This calls dvipng."
   (require 'org-latex)
   (let* ((tmpdir (if (featurep 'xemacs)
@@ -17189,7 +17214,109 @@ inspection."
 	    nil)
 	;; Use the requested file name and clean up
 	(copy-file pngfile tofile 'replace)
-	(loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
+	(loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
+	      (delete-file (concat texfilebase e)))
+	pngfile))))
+(defun org-create-formula-image-with-imagemagick (string tofile options buffer)
+  "This calls convert, which is included into imagemagick."
+  (require 'org-latex)
+  (let* ((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"))
+	 (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))
+		 "white")))
+    (if (eq fg 'default) (setq fg (org-latex-color :foreground))
+      (setq fg (org-latex-color-format fg)))
+    (if (eq bg 'default) (setq bg (org-latex-color :background))
+      (setq bg (org-latex-color-format
+		(if (string= bg "Transparent")(setq bg "white")))))
+    (with-temp-file texfile
+      (insert (org-splice-latex-header
+	       org-format-latex-header
+	       org-export-latex-default-packages-alist
+	       org-export-latex-packages-alist t
+	       org-format-latex-header-extra))
+      (insert "\n\\begin{document}\n" 
+	      "\\definecolor{fg}{rgb}{" fg "}\n"
+	      "\\definecolor{bg}{rgb}{" bg "}\n"
+	      "\n\\pagecolor{bg}\n"
+	      "\n{\\color{fg}\n"
+	      string
+	      "\n}\n"
+	      "\n\\end{document}\n" )
+      (require 'org-latex)
+      (org-export-latex-fix-inputenc))
+    (let ((dir default-directory))
+      (condition-case nil
+	  (progn
+	    (cd tmpdir)
+	    (setq cmds org-latex-to-pdf-process)
+	    (while cmds
+	      (setq latex-frags-cmds (pop cmds))
+	      (if (listp latex-frags-cmds)
+		  (setq cmds nil)
+		(setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
+	    (while latex-frags-cmds
+	      (setq cmd (pop latex-frags-cmds))
+	      (while (string-match "%b" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument texfile))
+			   t t cmd)))
+	      (while (string-match "%f" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument (file-name-nondirectory texfile)))
+			   t t cmd)))
+	      (while (string-match "%o" cmd)
+		(setq cmd (replace-match
+			   (save-match-data
+			     (shell-quote-argument (file-name-directory texfile)))
+			   t t cmd)))
+	      (shell-command cmd)))
+	(error nil))
+      (cd dir))
+    (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" ".out") do
 	      (delete-file (concat texfilebase e)))
 	pngfile))))
 
@@ -17263,6 +17390,23 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML."
 					   ((eq attr :background) 'background))))
 		   (color-values (face-attribute 'default attr nil))))))
 
+(defun org-latex-color (attr)
+  "Return an rgb color specification for latex color package."
+  (apply 'format "%s,%s,%s"
+	 (mapcar 'org-normalize-color
+		 (if (featurep 'xemacs)
+		     (color-rgb-components
+		      (face-property 'default
+				     (cond ((eq attr :foreground) 'foreground)
+					   ((eq attr :background) 'background))))
+		   (color-values (face-attribute 'default attr nil))))))
+
+(defun org-latex-color-format (color-name)
+  "Convert color name to RGB value with format num1,num2,num3"
+  (apply 'format "%s,%s,%s"
+	 (mapcar 'org-normalize-color
+		   (color-values color-name))))
+
 (defun org-normalize-color (value)
   "Return string to be used as color value for an RGB component."
   (format "%g" (/ value 65535.0)))
-- 
1.7.9.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [patch] Need test: New method which is used to preview latex fragements
  2012-03-27 23:48                         ` Feng Shu
@ 2012-04-23 13:10                           ` Bastien
  0 siblings, 0 replies; 11+ messages in thread
From: Bastien @ 2012-04-23 13:10 UTC (permalink / raw)
  To: Feng Shu; +Cc: emacs-orgmode

Hi Feng,

this patch is now in master, with some small modifications.

Thanks for this contribution!

-- 
 Bastien

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2012-04-23 13:21 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-23  8:22 [patch] A experimental toy which is used to preview latex fragements FengShu
2012-03-24 13:34 ` Need Help: " FengShu
2012-03-24 17:32   ` Nick Dokos
2012-03-24 17:45     ` Nick Dokos
2012-03-24 18:54       ` Nick Dokos
2012-03-25 12:36         ` FengShu
2012-03-26  6:07           ` [patch] Need test: New path " FengShu
2012-03-27  4:23             ` [patch] Need test: New method " FengShu
2012-03-27  8:16               ` Bastien
     [not found]                 ` <87haxapcof.fsf@gmail.com>
     [not found]                   ` <87limmqoyu.fsf@gnu.org>
     [not found]                     ` <87k426xk5t.fsf@gmail.com>
     [not found]                       ` <87vclq401w.fsf@gnu.org>
2012-03-27 23:48                         ` Feng Shu
2012-04-23 13:10                           ` Bastien

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).