Re: [O] HTML export with LaTeX babel blocks

2019-08-29 Thread Berry, Charles


> On Aug 28, 2019, at 5:59 PM, Michaël Cadilhac  wrote:
> 
> 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:
> 
> -
[snip]

Well you can try to continue on your path, but it can get `interesting'.

An alternative is to do something like this:

Define an export backend that derives from 'html. 

Flesh out the function `org-tikz-html-export-block' to process the content of 
`tikz' export blocks to render the derived SVG. Use existing functions in 
`ox-latex.el' as helpers if that works.

#+begin_src emacs-lisp
  (org-export-define-derived-backend 'tikz-html 'html
:translate-alist '((export-block . org-tikz-html-export-block)))

  (defun org-tikz-html-export-block (export-block contents info)
"Transcoder to TikZ in html exports."
(when (string= (org-element-property :type export-block) "TIKZ")
  (let ((tikz-code 
 (org-remove-indentation
  (org-element-property :value export-block
;; process TikZ code to SVG
;; produce a suitable link to include the SVG as the result
)))
#+end_src


Then blocks like

#+begin_export tikz
% tikz code goes here
#+end_export

should render when you run

#+begin_src emacs-lisp
  (org-export-to-file 'tikz-html "file-name.html") 
#+end_src 

You might adapt `org-html-export-to-html' if the features it offers are needed 
and add a :menu-entry to enable running from the export dispatcher under the 
html choices.

HTH,

Chuck


 




[O] HTML export with LaTeX babel blocks

2019-08-28 Thread Michaël Cadilhac
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?= 
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