emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] [patch] A experimental toy which is used to preview latex fragements


From: FengShu
Subject: [O] [patch] A experimental toy which is used to preview latex fragements
Date: Fri, 23 Mar 2012 16:22:20 +0800
User-agent: Gnus/5.130004 (Ma Gnus v0.4) Emacs/24.0.94 (gnu/linux)

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





>From 6a3fbe47d967f8d234d3aead058148cc46b7d376 Mon Sep 17 00:00:00 2001
From: FengShu <address@hidden>
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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]