emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: FengShu <tumashu@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: Need Help: [patch] A experimental toy which is used to preview latex fragements
Date: Sun, 25 Mar 2012 20:36:13 +0800	[thread overview]
Message-ID: <87y5qohn7m.fsf@gmail.com> (raw)
In-Reply-To: <32078.1332615285@alphaville> (Nick Dokos's message of "Sat, 24 Mar 2012 14:54:45 -0400")

>
> 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

  reply	other threads:[~2012-03-25 12:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y5qohn7m.fsf@gmail.com \
    --to=tumashu@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).