emacs-orgmode
[Top][All Lists]
Advanced

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

[O] [PATCH] Add svg export using pdf2svg, for latex math and src blocks.


From: Tom Prince
Subject: [O] [PATCH] Add svg export using pdf2svg, for latex math and src blocks.
Date: Sun, 2 Oct 2011 14:35:48 -0400

This is my attempt to add export of latex code (for HTML export)
via svg, rather than png. I don't know if this is the best way to go
about doing it, but I wanted to avoid as much duplicated code as possible.
---
 lisp/ob-latex.el |    5 +++-
 lisp/org-exp.el  |    1 +
 lisp/org-html.el |    1 +
 lisp/org.el      |   67 +++++++++++++++++++++++++++++++++---------------------
 4 files changed, 47 insertions(+), 27 deletions(-)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index 739a936..422e141 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -82,7 +82,10 @@ This function is called by `org-babel-execute-src-block'."
                      org-export-latex-packages-alist)))
         (cond
          ((string-match "\\.png$" out-file)
-          (org-create-formula-image
+          (org-create-formula-image (assq 'dvipng 
org-latex-image-formats-alist)
+           body out-file org-format-latex-options in-buffer))
+         ((string-match "\\.svg$" out-file)
+          (org-create-formula-image (assq 'pdf2svg 
org-latex-image-formats-alist)
            body out-file org-format-latex-options in-buffer))
          ((string-match "\\.pdf$" out-file)
          (require 'org-latex)
diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index 42b26e4..674f85f 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -530,6 +530,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 pdf2svg to make images" pdf2svg)
          (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 fde563b..80c9b6b 100644
--- a/lisp/org-html.el
+++ b/lisp/org-html.el
@@ -661,6 +661,7 @@ postamble DIV."
       ((eq (plist-get parameters :LaTeX-fragments) 'mathjax ) 'mathjax)
       ((eq (plist-get parameters :LaTeX-fragments) t        ) 'mathjax)
       ((eq (plist-get parameters :LaTeX-fragments) 'dvipng  ) 'dvipng)
+      ((eq (plist-get parameters :LaTeX-fragments) 'pdf2svg  ) 'pdf2svg)
       (t nil))))
   (goto-char (point-min))
   (let (label l1)
diff --git a/lisp/org.el b/lisp/org.el
index 73b1073..7c2d84e 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -16471,6 +16471,13 @@ The images can be removed again with 
\\[org-ctrl-c-ctrl-c]."
     ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
     ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
   "Regular expressions for matching embedded LaTeX.")
+(defconst org-latex-image-formats-alist
+  `((dvipng "latex" "dvipng" ".dvi" ".png" 'png nil)
+    (pdf2svg "pdflatex" "pdf2svg" ".pdf" ".svg" 'svg t))
+  "List of constants for image post-processors.
+
+ (<symbol> <latex-processor> <image-processor> <intermediate-ext> <final-ext> 
<format-symbol> <need-preview-fit>).")
+
 
 (defvar org-export-have-math nil) ;; dynamic scoping
 (defun org-format-latex (prefix &optional dir overlays msg at
@@ -16488,7 +16495,7 @@ Some of the options can be changed using the variable
         (org-format-latex-header-extra
          (plist-get (org-infile-export-plist) :latex-header-extra))
         (cnt 0) txt hash link beg end re e checkdir
-        executables-checked string
+        executables-checked string image-format
         m n block linkfile movefile ov)
     ;; Check the different regular expressions
     (while (setq e (pop re-list))
@@ -16522,21 +16529,22 @@ 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))))
-            ((or (eq processing-type 'dvipng) t)
+            ((or (assq processing-type org-latex-image-formats-alist) t)
              ;; Process to an image
              (setq txt (match-string n)
                    beg (match-beginning n) end (match-end n)
                    cnt (1+ cnt))
              (let (print-length print-level) ; make sure full list is printed
-               (setq hash (sha1 (prin1-to-string
+               (setq image-format (assq processing-type 
org-latex-image-formats-alist)
+                     hash (sha1 (prin1-to-string
                                  (list org-format-latex-header
                                        org-format-latex-header-extra
                                        org-export-latex-default-packages-alist
                                        org-export-latex-packages-alist
                                        org-format-latex-options
                                        forbuffer txt)))
-                     linkfile (format "%s_%s.png" prefix hash)
-                     movefile (format "%s_%s.png" absprefix hash)))
+                     linkfile (format (concat "%s_%s" (nth 4 image-format)) 
prefix hash)
+                     movefile (format (concat "%s_%s" (nth 4 image-format)) 
absprefix hash)))
              (setq link (concat block "[[file:" linkfile "]]" block))
              (if msg (message msg cnt))
              (goto-char beg)
@@ -16546,13 +16554,13 @@ Some of the options can be changed using the variable
 
              (unless executables-checked
                (org-check-external-command
-                "latex" "needed to convert LaTeX fragments to images")
+                (nth 1 image-format) "needed to convert LaTeX fragments to 
images")
                (org-check-external-command
-                "dvipng" "needed to convert LaTeX fragments to images")
+                (nth 2 image-format) "needed to convert LaTeX fragments to 
images")
                (setq executables-checked t))
 
              (unless (file-exists-p movefile)
-               (org-create-formula-image
+               (org-create-formula-image image-format
                 txt movefile opt forbuffer))
              (if overlays
                  (progn
@@ -16568,10 +16576,10 @@ Some of the options can be changed using the variable
                          (overlay-put ov 'invisible t)
                          (overlay-put
                           ov 'end-glyph
-                          (make-glyph (vector 'png :file movefile))))
+                          (make-glyph (vector (nth 5 image-format) :file 
movefile))))
                      (overlay-put
                       ov 'display
-                      (list 'image :type 'png :file movefile :ascent 'center)))
+                      (list 'image :type (nth 5 image-format) :file movefile 
:ascent 'center)))
                    (push ov org-latex-fragment-image-overlays)
                    (goto-char end))
                (delete-region beg end)
@@ -16581,7 +16589,7 @@ Some of the options can be changed using the variable
                                   "\"" "" txt)))))))))))))
 
 ;; This function borrows from Ganesh Swami's latex2png.el
-(defun org-create-formula-image (string tofile options buffer)
+(defun org-create-formula-image (image-format string tofile options buffer)
   "This calls dvipng."
   (require 'org-latex)
   (let* ((tmpdir (if (featurep 'xemacs)
@@ -16590,8 +16598,8 @@ Some of the options can be changed using the variable
         (texfilebase (make-temp-name
                       (expand-file-name "orgtex" tmpdir)))
         (texfile (concat texfilebase ".tex"))
-        (dvifile (concat texfilebase ".dvi"))
-        (pngfile (concat texfilebase ".png"))
+        (dvifile (concat texfilebase (nth 3 image-format)))
+        (pngfile (concat texfilebase (nth 4 image-format)))
         (fnh (if (featurep 'xemacs)
                   (font-height (get-face-font 'default))
                 (face-attribute 'default :height nil)))
@@ -16609,35 +16617,42 @@ Some of the options can be changed using the variable
               org-export-latex-default-packages-alist
               org-export-latex-packages-alist t
               org-format-latex-header-extra))
-      (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
+      (if (nth 6 image-format)
+         (insert 
"\\usepackage[tightpage,active]{preview}\n\\begin{document}\\begin{preview}\n" 
string "\n\\end{preview}\\end{document}\n")
+       (insert "\n\\begin{document}\n" string "\n\\end{document}\n"))
       (require 'org-latex)
       (org-export-latex-fix-inputenc))
     (let ((dir default-directory))
       (condition-case nil
          (progn
            (cd tmpdir)
-           (call-process "latex" nil nil nil texfile))
+           (call-process (nth 1 image-format) nil nil nil texfile))
        (error nil))
       (cd dir))
     (if (not (file-exists-p dvifile))
-       (progn (message "Failed to create dvi file from %s" texfile) nil)
+       (progn (message "Failed to create pdf file from %s" texfile) nil)
       (condition-case nil
-         (call-process "dvipng" nil nil nil
-                       "-fg" fg "-bg" bg
-                       "-D" dpi
-                       ;;"-x" scale "-y" scale
-                       "-T" "tight"
-                       "-o" pngfile
-                       dvifile)
+         (case (car image-format)
+           ('dvipng
+            (call-process "dvipng" nil nil nil
+                          "-fg" fg "-bg" bg
+                          "-D" dpi
+                          ;;"-x" scale "-y" scale
+                          "-T" "tight"
+                          "-o" pngfile
+                          dvifile))
+           ('pdf2svg
+            (call-process "pdf2svg" nil nil nil
+                          dvifile 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)
+             (error "Failed to create svg file from %s" texfile)
+           (message "Failed to create svg file from %s" texfile)
            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 `(,(nth 3 image-format) ".tex" ".aux" ".log" ,(nth 4 
image-format)) do
              (delete-file (concat texfilebase e)))
        pngfile))))
 
-- 
1.7.6.1




reply via email to

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