My goal is to export SVG files of TikZ drawings in HTML.  Now, what follows
is a bit of a rant on `org-babel-execute:latex`; let's go through the
options:

- You're exporting to PNG without imagemagick:
This uses `org-create-formula-image` which works really well, but it
discards the options of the LaTeX block (fit, width, height, ...),
including headers.  It also fails to *force* using 'dvipng as processing
type, so if `org-preview-latex-default-process` has been changed, it may be
confusing.  Finally, it uses `in-buffer` to render LaTeX as if it were in
the buffer, which is not what the user would want since they are exporting
to html (as a result, I get a black background on my pictures, since my
buffer background is black).

- You're exporting to PNG with imagemagick or to PDF:
This may work.  Sadly, "#+LATEX_HEADER:" is ignored, though it is in the
previous case.

- You're exporting to HTML.  This uses htlatex—it's very rarely functional,
but eh, you asked for TeX to HTML, and pdf2htmlEX seems unmaintained, so
expect chaos.

- You're exporting to SVG.
This… uses htlatex?  Why in paradise?  org-create-formula-image would do
that *brilliantly.  *In any case, I can't seem to make it work: my fonts
are always disappearing.

So what do I do?  I use the attached patch, that bypasses `htlatex` and
uses the oh-so-working `org-create-formula-image`.  This, of course, is not
a complete solution—this whole function is messy at best, buggy at
worst—but in the meantime, I can finally export my pictures to SVG (end
result: https://autoboz.org/open-problems/19.1-ccra/).

If anyone has an opinion on what to do with this situation, I'd be happy to
help.

Cheers,
M.
From 7f2cbee0e45ba6a57c913ed49690262401e67f39 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Cadilhac?= <mich...@cadilhac.name>
Date: Thu, 15 Aug 2019 10:28:27 -0400
Subject: [PATCH] ob-latex: Use org-create-formula-image when generating SVG

* lisp/ob-latex.el (org-babel-execute:latex): Remove convoluted and
buggy htlatex based SVG generation, and use `org-create-formula-image`
---
 lisp/ob-latex.el | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index adf83d460..02ddfa2a8 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -114,14 +114,13 @@ This function is called by `org-babel-execute-src-block'."
 	  (when (file-exists-p out-file) (delete-file out-file))
 	  (with-temp-file out-file
 	    (insert body)))
-	 ((and (or (string= "svg" extension)
-		   (string= "html" extension))
+	 ;; TODO: this is a very different way of generating the
+	 ;; frame latex document than in the pdf case.  Ideally, both
+	 ;; would be unified.  This would prevent bugs creeping in
+	 ;; such as the one fixed on Aug 16 2014 whereby :headers was
+	 ;; not included in the SVG/HTML case.
+	 ((and (string= "html" extension)
 	       (executable-find org-babel-latex-htlatex))
-	  ;; TODO: this is a very different way of generating the
-	  ;; frame latex document than in the pdf case.  Ideally, both
-	  ;; would be unified.  This would prevent bugs creeping in
-	  ;; such as the one fixed on Aug 16 2014 whereby :headers was
-	  ;; not included in the SVG/HTML case.
 	  (with-temp-file tex-file
 	    (insert (concat
 		     "\\documentclass[preview]{standalone}
@@ -143,23 +142,14 @@ This function is called by `org-babel-execute-src-block'."
 	  (when (file-exists-p out-file) (delete-file out-file))
 	  (let ((default-directory (file-name-directory tex-file)))
 	    (shell-command (format "%s %s" org-babel-latex-htlatex tex-file)))
-	  (cond
-	   ((file-exists-p (concat (file-name-sans-extension tex-file) "-1.svg"))
-	    (if (string-suffix-p ".svg" out-file)
-		(progn
-		  (shell-command "pwd")
-		  (shell-command (format "mv %s %s"
-					 (concat (file-name-sans-extension tex-file) "-1.svg")
-					 out-file)))
-	      (error "SVG file produced but HTML file requested")))
-	   ((file-exists-p (concat (file-name-sans-extension tex-file) ".html"))
-	    (if (string-suffix-p ".html" out-file)
-		(shell-command "mv %s %s"
-			       (concat (file-name-sans-extension tex-file)
-				       ".html")
-			       out-file)
-	      (error "HTML file produced but SVG file requested")))))
+	  (when (file-exists-p (concat (file-name-sans-extension tex-file) ".html"))
+	    (shell-command "mv %s %s"
+			   (concat (file-name-sans-extension tex-file)
+				   ".html")
+			   out-file)))
+	 ((string= "svg" extension)
+	  (org-create-formula-image
+           body out-file org-format-latex-options nil 'dvisvgm))
 	 ((or (string= "pdf" extension) imagemagick)
 	  (with-temp-file tex-file
 	    (require 'ox-latex)
-- 
2.22.0

Reply via email to