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

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