emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Matt Huszagh <huszaghmatt@gmail.com>
To: Bastien <bzg@gnu.org>
Cc: , "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] babel latex headers and image generation commands
Date: Wed, 09 Sep 2020 12:43:00 -0700	[thread overview]
Message-ID: <87sgbqzqgr.fsf@gmail.com> (raw)
In-Reply-To: <874kobmnrc.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1406 bytes --]

Bastien <bzg@gnu.org> writes:

> Prefer
>
>   * lisp/ob-latex.el (org-babel-latex-preamble): New option for LaTeX
>   preamble customization.
>
> "New option" is quite standard, an "option" being a customizable
> variable.  In this case, "New option" would probably be enough, given
> the name of the option is quite self-explanatory.  Also, some find it
> pedantic, but "LaTeX" is the correct spelling in a changelog I guess.

Fixed in new patch (attached).

> The first line of the docstring should contain a sentence, so you'd
> need to have a new paragraph after "runtime to the LaTeX preamble."

Also fixed. Making the first line a full sentence means that some lines
are a little longer than 80 characters. Is this acceptable?

> What for users who don't have inkscape?  

This is just a default, but I could use a dvisvgm command as the default
instead? Either way, converting a PDF to SVG will require an executable
outside Emacs, but I guess dvisvgm is more likely to be installed for
people using a texlive installation. My personal preference for inkscape
is because it should handle all PDF inputs, whereas there are some cases
where dvisvgm may fail (see
https://github.com/mgieseki/dvisvgm/issues/139) due to changes in
ghostscript. Still, dvisvgm generally does a very good job with PDF
inputs. Let me know your thoughts, I'd be happy to set the default to a
dvisvgm command instead.

Matt


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-latex.el-Make-latex-to-svg-compilation-very-custo.patch --]
[-- Type: text/x-patch, Size: 3938 bytes --]

From 1ff86f2267b653dff225837ccf13ebf417f7ed03 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): New option for LaTeX
preamble customization.
(org-babel-latex-begin-env): New option for LaTeX document environment
begin customization.
(org-babel-latex-end-env): New option for LaTeX document environment
end customization.
(org-babel-latex-pdf-svg-process): New option for converting a pdf to
svg.
(org-babel-execute:latex): Add specific case for svg generation from
LaTeX block.
---
 lisp/ob-latex.el | 59 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 56 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index 4b343dd14..6a4f7a6ba 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -66,7 +66,46 @@
   "LaTeX-specific header arguments.")
 
 (defcustom org-babel-latex-htlatex "htlatex"
-  "The htlatex command to enable conversion of latex to SVG or HTML."
+  "The htlatex command to enable conversion of LaTeX to SVG or HTML."
+  :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)
 
@@ -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 params)
+			 body
+			 (funcall org-babel-latex-end-env params))))
+	  (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	other threads:[~2020-09-09 19:43 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-04  7:39 babel latex headers and image generation commands Matt Huszagh
2020-02-04 21:19 ` Matt Huszagh
2020-02-04 23:38   ` Matt Huszagh
2020-02-10  7:21     ` Bastien
     [not found]       ` <87y2lynft3.fsf@gmail.com>
2020-08-29  7:02         ` Matt Huszagh
2020-09-02 17:32           ` [PATCH] " Matt Huszagh
2020-09-02 18:53             ` Matt Huszagh
2020-09-06  6:18               ` Bastien
2020-09-09 19:43                 ` Matt Huszagh [this message]
2020-10-24 12:30                   ` Bastien
2020-10-24 16:39                     ` Matt Huszagh
2020-12-14  8:19                       ` Bastien
2020-09-06  5:59             ` Bastien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87sgbqzqgr.fsf@gmail.com \
    --to=huszaghmatt@gmail.com \
    --cc=bzg@gnu.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).