emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* HTML export with LaTeX babel blocks
@ 2019-08-29  0:59 Michaël Cadilhac
  2019-08-29 18:13 ` Berry, Charles
  0 siblings, 1 reply; 2+ messages in thread
From: Michaël Cadilhac @ 2019-08-29  0:59 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 1728 bytes --]

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.

[-- Attachment #1.2: Type: text/html, Size: 2073 bytes --]

[-- Attachment #2: 0001-ob-latex-Use-org-create-formula-image-when-generatin.patch --]
[-- Type: text/x-patch, Size: 3040 bytes --]

From 7f2cbee0e45ba6a57c913ed49690262401e67f39 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Cadilhac?= <michael@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


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

* Re: HTML export with LaTeX babel blocks
  2019-08-29  0:59 HTML export with LaTeX babel blocks Michaël Cadilhac
@ 2019-08-29 18:13 ` Berry, Charles
  0 siblings, 0 replies; 2+ messages in thread
From: Berry, Charles @ 2019-08-29 18:13 UTC (permalink / raw)
  To: Michaël Cadilhac; +Cc: emacs-orgmode@gnu.org



> On Aug 28, 2019, at 5:59 PM, Michaël Cadilhac <michael@cadilhac.name> 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


 



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

end of thread, other threads:[~2019-08-29 18:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-08-29  0:59 HTML export with LaTeX babel blocks Michaël Cadilhac
2019-08-29 18:13 ` Berry, Charles

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).