emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Add svg export using pdf2svg, for latex math and src blocks.
@ 2011-10-02 18:35 Tom Prince
  2011-10-12 13:04 ` Tom Prince
  2012-04-23 13:16 ` Bastien
  0 siblings, 2 replies; 7+ messages in thread
From: Tom Prince @ 2011-10-02 18:35 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Tom Prince

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-04-23 13:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-02 18:35 [PATCH] Add svg export using pdf2svg, for latex math and src blocks Tom Prince
2011-10-12 13:04 ` Tom Prince
2011-10-12 15:26   ` Eric S Fraga
2011-10-12 17:00   ` Nick Dokos
2011-10-12 17:09   ` Jambunathan K
2012-04-21  0:01   ` Bastien
2012-04-23 13:16 ` 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).