From mboxrd@z Thu Jan 1 00:00:00 1970 From: FengShu Subject: [patch] A experimental toy which is used to preview latex fragements Date: Fri, 23 Mar 2012 16:22:20 +0800 Message-ID: <874ntfyber.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([208.118.235.92]:37936) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SAzkb-0004zX-Jq for emacs-orgmode@gnu.org; Fri, 23 Mar 2012 04:21:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SAzkZ-0005mg-7Z for emacs-orgmode@gnu.org; Fri, 23 Mar 2012 04:21:33 -0400 Received: from mail-iy0-f169.google.com ([209.85.210.169]:35716) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SAzkY-0005h1-V9 for emacs-orgmode@gnu.org; Fri, 23 Mar 2012 04:21:31 -0400 Received: by mail-iy0-f169.google.com with SMTP id r24so5293277iaj.0 for ; Fri, 23 Mar 2012 01:21:29 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain 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... --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Ihis-is-a-experimental-toy-which-use-car-org-latex-t.patch >From 6a3fbe47d967f8d234d3aead058148cc46b7d376 Mon Sep 17 00:00:00 2001 From: FengShu 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 --=-=-=--