From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id YMVoCDjWLWOmLwEAbAwnHQ (envelope-from ) for ; Fri, 23 Sep 2022 17:52:24 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id wCxqCDjWLWNW6wAAauVa8A (envelope-from ) for ; Fri, 23 Sep 2022 17:52:24 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id C32BA1712B for ; Fri, 23 Sep 2022 17:52:23 +0200 (CEST) Received: from localhost ([::1]:49090 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1obkyU-0005jX-U4 for larch@yhetil.org; Fri, 23 Sep 2022 11:52:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44212) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1obYRI-0008Vl-PP for emacs-orgmode@gnu.org; Thu, 22 Sep 2022 22:29:16 -0400 Received: from ms11p00im-qufo17281601.me.com ([17.58.38.53]:57496) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1obYRG-0005cg-9o for emacs-orgmode@gnu.org; Thu, 22 Sep 2022 22:29:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=akirakyle.com; s=sig1; t=1663900150; bh=EsV8QmzDhxCATfEJALOKxpQRerJg7/orAmMJh/Nd9Fc=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=poJg1HGRf/WUpZfR0s4gAHi+WtLnBdVQoIGzAqoSxivUEUvvcITpS/SqDjFV7PpaN 7A7RRYIgKyQLWaRiyxhzeoRg8fCLwHc98PI3M/sqG4HJjEYOWoCno2heCuhixW6UhR yNUOZdK7wd1DTnV5UUG73kkqzgUZY4U4nrM3ntk0iEcua9KPzglcHFJgZk90UUwEU+ ricNjfCYgbwb11LDPvZBKJED6PEdkdnTxu/bIXhA4aJzFz/X60wqYxb1FGntcZf2v3 kFai4LuYOSlNC5/FEwF2joRxsM5RC+xzsGqzY3gjWbasw4KVEyAd9xuZRyfwxyYe/Q USl0PcXgxkjGQ== Received: from data (ms11p00im-dlb-asmtpmailmevip.me.com [17.57.154.19]) by ms11p00im-qufo17281601.me.com (Postfix) with ESMTPSA id CB69EAA0523; Fri, 23 Sep 2022 02:29:09 +0000 (UTC) From: Akira Kyle To: yantar92@gmail.com Cc: emacs-orgmode@gnu.org,reza@housseini.me Subject: Re: svg file from tikz picture In-Reply-To: <875yhhxece.fsf@localhost> Date: Thu, 22 Sep 2022 20:28:57 -0600 Message-ID: <87czbmg6xi.fsf@akirakyle.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Proofpoint-GUID: QeBZrv6PK9WRtnKc6Kr575zcTSDyGYKV X-Proofpoint-ORIG-GUID: QeBZrv6PK9WRtnKc6Kr575zcTSDyGYKV X-Proofpoint-Virus-Version: =?UTF-8?Q?vendor=3Dfsecure_engine=3D1.1.170-22c6f66c430a71ce266a39bfe25bc?= =?UTF-8?Q?2903e8d5c8f:6.0.425,18.0.572,17.11.62.513.0000000_definitions?= =?UTF-8?Q?=3D2022-01-14=5F01:2022-01-14=5F01,2020-02-14=5F11,2021-12-02?= =?UTF-8?Q?=5F01_signatures=3D0?= X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 bulkscore=0 mlxscore=0 mlxlogscore=999 suspectscore=0 malwarescore=0 adultscore=0 spamscore=0 clxscore=1030 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2209230014 Received-SPF: pass client-ip=17.58.38.53; envelope-from=akira@akirakyle.com; helo=ms11p00im-qufo17281601.me.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 23 Sep 2022 11:45:48 -0400 X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1663948343; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=EsV8QmzDhxCATfEJALOKxpQRerJg7/orAmMJh/Nd9Fc=; b=Ucgm/MbtOI7Y2xzj8I8az+Q32ph7VftUMsP/jZ1FMeJacElkpUz3k7VLbb26XD0QGJhRcW Gadq+OzeBMuTBSPsbcZJtWGKcK4or2jlywaHtsTLrr0GaOxO+xRLG9AOKg9AcyRZitw/U9 xoEZmfRnhXzoyr4YcDpsEBe+jUZH2JY9qV99nvZRWh28DLMWphYsXKorZ9HX0katihgJk6 JL/+frf3Rr5lbNpj17hecj3iym9w2mrURVyXAoSFeabgkSYNCjtSMcNBfl1/iVMK3NoHfO 9kpVRc+6CX2IbrpkiJWJrlFX6mK/AB4MCshfvg5KlNA5+8F4TK4hVkPB6hYh0g== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1663948343; a=rsa-sha256; cv=none; b=dXZCN4R8yO1uHZuE1iymjgWSCvhMlYXuK6ANtLmt/Pel1BfY8sJRbb/9AP8NXvoFbZ7zjo oSOqSCUdCnPiHRhDFs349SPu/RS8GTZeg1LSLC+DbLLJfYqckVv2AnDG65sJMu4ILITlTX /+Kw+9WQoDeUWJvDLgrVxhwS0p+9wGIrAKKpm2CPC4zHTG/+kijnaEh1LLvQCCPWwPSRE0 WrDMtpAlvdsaPenir2Yi7u7lhcgZ5hoaOA1w58xB0Z2jpQkENj4Kn1vClA3EQIyJNmP4qd FrL9Kq7f1sL3YBiWaP5V5KjaaOx6Re5FxW7e/DVCTRIGnb82z6nX6cKnV+0jKA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=akirakyle.com header.s=sig1 header.b=poJg1HGR; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -3.45 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=akirakyle.com header.s=sig1 header.b=poJg1HGR; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: C32BA1712B X-Spam-Score: -3.45 X-Migadu-Scanner: scn1.migadu.com X-TUID: OjicXcXMYbR0 --=-=-= Content-Type: text/plain I've been using the attached patch for the last few years and I've meaning to send it here/start a discussion about ob-latex.el since I used it pretty much daily to write tikz figures in org mode. So I'm glad to see this discussion has been started! I've found it to be incredibly productive to use babel to develop tikz diagrams as I can make come changes and quickly `org-ctrl-c-ctrl-c` to render them in the same buffer. I think when I made this patch I had been caught by some of the quirks of the svg export. For example, sometimes I would have some latex equation which I use ~org-latex-preview~ on as I was writing it, but then it would fail to render as mathjax upon html export since I would use some latex package that isn't available under mathjax. So by using ob-latex I could easily fix this by using the ~:file .svg~ header and get a nice html export. However due to the different way of assembling the ~.tex~ file sometimes ~org-latex-preview~ would work but ob-latex wouldn't. I think my use case may be fairly common and so I think ob-latex really should be updated so svg uses the ~org-latex-preview~ code. o Also I think the ~.tikz~ extension doesn't really make any sense since one really can but arbitrary tex code in such a block, and I think that's why I renamed it in my patch. However I'm now realizing that this evaluation method probably doesn't make much since `:tangle` will already do this, with the added benefit of handling noweb references correctly. So perhaps this should be removed and document using tangling in lieu of ~:file *.tikz~? PS: I'm not currently subbed to this mailing list, so please try to cc me --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-ob-latex-Create-svg-images-the-same-way-as-png-image.patch >From 5d94745dfbe0858d4fd7d6530b821445b06d5013 Mon Sep 17 00:00:00 2001 From: Akira Kyle Date: Tue, 4 Jan 2022 14:33:05 -0700 Subject: [PATCH] ob-latex: Create svg images the same way as png images Also some cleanup of generation .html and .pdf files --- lisp/ob-latex.el | 161 +++++++++++++++-------------------------------- 1 file changed, 50 insertions(+), 111 deletions(-) diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el index a86699e22..71b01058c 100644 --- a/lisp/ob-latex.el +++ b/lisp/ob-latex.el @@ -39,7 +39,7 @@ (declare-function org-create-formula-image "org" (string tofile options buffer &optional type)) (declare-function org-latex-compile "ox-latex" (texfile &optional snippet)) -(declare-function org-latex-guess-inputenc "ox-latex" (header)) +(declare-function org-latex-make-preamble "ox-latex" (info &optional template snippet?)) (declare-function org-splice-latex-header "org" (tpl def-pkg pkg snippets-p &optional extra)) (declare-function org-at-heading-p "org" (&optional _)) (declare-function org-back-to-heading "org" (&optional invisible-ok)) @@ -115,12 +115,6 @@ exporting the literal LaTeX source." :group 'org-babel :type 'string) -(defcustom org-babel-latex-htlatex-packages - '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}") - "Packages to use for htlatex export." - :group 'org-babel - :type '(repeat (string))) - (defun org-babel-expand-body:latex (body params) "Expand BODY according to PARAMS, return the expanded body." (mapc (lambda (pair) ;; replace variables @@ -142,132 +136,82 @@ This function is called by `org-babel-execute-src-block'." (extension (file-name-extension out-file)) (tex-file (org-babel-temp-file "latex-" ".tex")) (border (cdr (assq :border params))) - (imagemagick (cdr (assq :imagemagick params))) - (im-in-options (cdr (assq :iminoptions params))) - (im-out-options (cdr (assq :imoutoptions params))) (fit (or (cdr (assq :fit params)) border)) (height (and fit (cdr (assq :pdfheight params)))) (width (and fit (cdr (assq :pdfwidth params)))) (headers (cdr (assq :headers params))) (in-buffer (not (string= "no" (cdr (assq :buffer params))))) + (imagemagick (cdr (assq :imagemagick params))) + (im-in-options (cdr (assq :iminoptions params))) + (im-out-options (cdr (assq :imoutoptions params))) (org-latex-packages-alist - (append (cdr (assq :packages params)) org-latex-packages-alist))) + (append (cdr (assq :packages params)) org-latex-packages-alist)) + (org-format-latex-header + (concat org-format-latex-header + (mapconcat #'identity (cdr (assq :headers params)) "\n") + (if fit "\n\\usepackage[active, tightpage]{preview}\n" "") + (if border + (format "\\setlength{\\PreviewBorder}{%s}" border) "") + (if height + (concat "\n" (format "\\pdfpageheight %s" height)) "") + (if width + (concat "\n" (format "\\pdfpagewidth %s" width)) ""))) + (body (if fit + (concat "\n\\begin{preview}\n" body "\n\\end{preview}\n") + body))) (cond ((and (string-suffix-p ".png" out-file) (not imagemagick)) (let ((org-format-latex-header (concat org-format-latex-header "\n" (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) + (org-create-formula-image + body out-file org-format-latex-options in-buffer 'dvipng))) + ((and (string= "svg" extension) (not imagemagick)) + (org-create-formula-image + body out-file org-format-latex-options in-buffer 'dvisvgm)) + ((string-suffix-p ".tex" out-file) (when (file-exists-p out-file) (delete-file out-file)) (with-temp-file out-file (insert body))) - ((and (string= "html" extension) + ((and (string= "html" extension) (not imagemagick) (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} -\\def\\pgfsysdriver{pgfsys-tex4ht.def} -" - (mapconcat (lambda (pkg) - (concat "\\usepackage" pkg)) - org-babel-latex-htlatex-packages - "\n") - (if headers - (concat "\n" - (if (listp headers) - (mapconcat #'identity headers "\n") - headers) "\n") - "") - "\\begin{document}" - body - "\\end{document}"))) + (let ((org-format-latex-header + (concat org-format-latex-header + "\\def\\pgfsysdriver{pgfsys-tex4ht.def}"))) + (org-babel-latex-format-tex tex-file body)) (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"))))) + (rename-file (concat (file-name-sans-extension tex-file) ".html") + out-file)) ((or (string= "pdf" extension) imagemagick) - (with-temp-file tex-file - (require 'ox-latex) - (insert - (org-latex-guess-inputenc - (org-splice-latex-header - org-format-latex-header - (delq - nil - (mapcar - (lambda (el) - (unless (and (listp el) (string= "hyperref" (cadr el))) - el)) - org-latex-default-packages-alist)) - org-latex-packages-alist - nil)) - (if fit "\n\\usepackage[active, tightpage]{preview}\n" "") - (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "") - (if height (concat "\n" (format "\\pdfpageheight %s" height)) "") - (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "") - (if headers - (concat "\n" - (if (listp headers) - (mapconcat #'identity headers "\n") - headers) "\n") - "") - (if fit - (concat "\n\\begin{document}\n\\begin{preview}\n" body - "\n\\end{preview}\n\\end{document}\n") - (concat "\n\\begin{document}\n" body "\n\\end{document}\n")))) - (when (file-exists-p out-file) (delete-file out-file)) - (let ((transient-pdf-file (org-babel-latex-tex-to-pdf tex-file))) + (org-babel-latex-format-tex tex-file body) + (let ((default-directory (file-name-directory tex-file))) + (org-latex-compile tex-file)) + (let ((transient-pdf-file + (concat (file-name-sans-extension tex-file) ".pdf"))) + (when (file-exists-p out-file) (delete-file out-file)) (cond ((string= "pdf" extension) (rename-file transient-pdf-file out-file)) (imagemagick (org-babel-latex-convert-pdf - transient-pdf-file out-file im-in-options im-out-options) - (when (file-exists-p transient-pdf-file) - (delete-file transient-pdf-file))) - (t - (error "Can not create %s files, please specify a .png or .pdf file or try the :imagemagick header argument" - extension)))))) + transient-pdf-file out-file im-in-options im-out-options))))) + (t + (error "Can't create %s files, please specify a .tex, .pdf, .png, or .svg file, or try the :imagemagick header argument" + extension))) nil) ;; signal that output has already been written to file body)) +(defun org-babel-latex-format-tex (tex-file body) + "Generate a temporary tex file from execute params." + (with-temp-file tex-file + (insert + (org-latex-make-preamble + (org-export-get-environment (org-export-get-backend 'latex)) + org-format-latex-header) + (concat "\n\\begin{document}\n" body "\n\\end{document}\n")))) + (defun org-babel-latex-convert-pdf (pdffile out-file im-in-options im-out-options) "Generate a file from a pdf file using imagemagick." (let ((cmd (concat "convert " im-in-options " " pdffile " " @@ -275,11 +219,6 @@ This function is called by `org-babel-execute-src-block'." (message "Converting pdffile file %s..." cmd) (shell-command cmd))) -(defun org-babel-latex-tex-to-pdf (file) - "Generate a pdf file according to the contents FILE." - (require 'ox-latex) - (org-latex-compile file)) - (defun org-babel-prep-session:latex (_session _params) "Return an error because LaTeX doesn't support sessions." (error "LaTeX does not support sessions")) -- 2.36.1 --=-=-=--