emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [PATCH] babel latex headers and image generation commands


From: Matt Huszagh
Subject: Re: [PATCH] babel latex headers and image generation commands
Date: Wed, 02 Sep 2020 10:32:07 -0700

Matt Huszagh <huszaghmatt@gmail.com> writes:

> Ok, I've finally gotten around to taking a crack at this. The patch is
> attached. Basically, it allows a lot more control when converting a
> latex source block into an svg image file.

I've added a few changes to the patch that additionally allow custom the
begin and end document environments. The purpose here is to allow latex
code within the document environment that is ignored by the body
export. For instance, I can set the page color with
{\color{some-color}...} and this doesn't mess up latex exports.

Please comment with any questions/concerns/thoughts.

Thanks!
>From 6dbd5ae840f02333f83d40a9c27be06968279563 Mon Sep 17 00:00:00 2001
From: Matt Huszagh <huszaghmatt@gmail.com>
Date: Fri, 28 Aug 2020 22:26:05 -0700
Subject: [PATCH] ob-latex.el: Make latex to svg compilation very customizable

* lisp/ob-latex.el (org-babel-latex-preamble): Add latex preamble
customization.
(org-babel-latex-begin-env): Add latex document environment begin
customization.
(org-babel-latex-end-env): Add latex document environment end
customization.
(org-babel-latex-pdf-svg-process): Add customization for converting a
pdf to svg.
(org-babel-execute:latex): Add specific case for svg generation from
latex block.
---
 lisp/ob-latex.el | 57 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 55 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index 4b343dd14..359179476 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -70,6 +70,45 @@
   :group 'org-babel
   :type 'string)
 
+(defcustom org-babel-latex-preamble
+  (lambda (_)
+    "\\documentclass[preview]{standalone}
+\\def\\pgfsysdriver{pgfsys-tex4ht.def}
+")
+  "Closure which evaluates at runtime to the latex preamble.  It
+takes 1 argument which is the parameters of the source block."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-begin-env
+  (lambda (_)
+    "\\begin{document}")
+  "Closure which evaluates at runtime to the begin part of the
+document environment.  It takes 1 argument which is the
+parameters of the source block.  This allows adding additional
+code that will be ignored when exporting the literal latex
+source."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-end-env
+  (lambda (_)
+    "\\end{document}")
+  "Closure which evaluates at runtime to the end part of the
+document environment.  It takes 1 argument which is the
+parameters of the source block.  This allows adding additional
+code that will be ignored when exporting the literal latex
+source."
+  :group 'org-babel
+  :type 'function)
+
+(defcustom org-babel-latex-pdf-svg-process
+  "inkscape --pdf-poppler %f -T -l -o %O"
+  "Command used to convert a PDF file to an SVG file when
+executing a latex source block."
+  :group 'org-babel
+  :type 'string)
+
 (defcustom org-babel-latex-htlatex-packages
   '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}")
   "Packages to use for htlatex export."
@@ -114,12 +153,26 @@ This function is called by `org-babel-execute-src-block'."
                         (mapconcat #'identity headers "\n"))))
           (org-create-formula-image
             body out-file org-format-latex-options in-buffer)))
+        ((string= "svg" extension)
+         (with-temp-file tex-file
+                (insert (concat (funcall org-babel-latex-preamble params)
+                        (mapconcat #'identity headers "\n")
+                        (funcall org-babel-latex-begin-env)
+                        body
+                        (funcall org-babel-latex-end-env))))
+         (let ((tmp-pdf (org-babel-latex-tex-to-pdf tex-file)))
+                  (let* ((log-buf (get-buffer-create "*Org Babel LaTeX 
Output*"))
+                         (err-msg "org babel latex failed")
+                         (img-out (org-compile-file
+                                  tmp-pdf
+                                   (list org-babel-latex-pdf-svg-process)
+                                   extension err-msg log-buf)))
+                    (shell-command (format "mv %s %s" img-out out-file)))))
          ((string-suffix-p ".tikz" out-file)
          (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))
+        ((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
-- 
2.28.0


reply via email to

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