From mboxrd@z Thu Jan 1 00:00:00 1970 From: Benjamin Motz Subject: [patch] Problems producing TikZ PNGs from LaTeX src blocks (was: Problems producing TikZ PNGs from LaTeX src blocks) Date: Sat, 29 Sep 2018 23:07:28 +0200 Message-ID: References: <87efdenepr.fsf@christianmoe.com> <87mus1x6ft.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:42109) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g6MSW-0005Hi-Cn for Emacs-orgmode@gnu.org; Sat, 29 Sep 2018 17:07:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g6MSV-0004ty-9X for Emacs-orgmode@gnu.org; Sat, 29 Sep 2018 17:07:28 -0400 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]:41785) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1g6MSU-0004sw-Sj for Emacs-orgmode@gnu.org; Sat, 29 Sep 2018 17:07:27 -0400 Received: by mail-wr1-x42c.google.com with SMTP id j15-v6so9803299wrt.8 for ; Sat, 29 Sep 2018 14:07:26 -0700 (PDT) In-Reply-To: <87mus1x6ft.fsf@gmail.com> (Eric S. Fraga's message of "Fri, 28 Sep 2018 11:27:50 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Christian Moe Cc: Emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Eric S Fraga writes: > On Friday, 28 Sep 2018 at 11:39, Christian Moe wrote: >> Hi, >> >> I am trying to generate a PNG file with a TikZ picture from a LaTeX src >> block, looking at instructions here: >> https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-LaTeX.html >> >> I try this: >> >> #+header: :file "tikzpic.png" :fit yes :results raw file :exports results :headers '("\\usepackage{tikz}") >> #+begin_src latex >> \begin{tikzpicture} >> \draw [fill=green] (0,4) -- (3,0) -- (-3,0) -- cycle; >> \end{tikzpicture} >> #+end_src It seems that the function `org-babel-execute:latex' ignores the property :headers under certain circumstances. Therefore the line "\usepackage{tikz}" will be missing in your resulting latex document. The property is not ignored if you also append the property ":imagemagick t" to the #+header line. So you can use the following header as a fix (for another fix, see below): #+header: :file "tikzpic.png" :fit yes :results raw file :exports results :headers '("\\usepackage{tikz}") :imagemagick t I've drafted a patch that fixes the issue. I would appreciate if some maintainer could have a look and comment, or apply the patch: --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Fix-ob-latex-to-respect-the-keyword-headers.patch >From a54f83d2b411ae58941c32cb23fd47f38e70dc80 Mon Sep 17 00:00:00 2001 From: Benjamin Motz Date: Fri, 28 Sep 2018 16:32:59 +0200 Subject: [PATCH 1/2] Fix ob-latex to respect the keyword :headers This adds support for property :headers in org-format-latex-options. TINYCHANGE --- lisp/ob-latex.el | 8 ++++++-- lisp/org.el | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el index 8d037090e..19e44dee5 100644 --- a/lisp/ob-latex.el +++ b/lisp/ob-latex.el @@ -108,8 +108,12 @@ This function is called by `org-babel-execute-src-block'." (append (cdr (assq :packages params)) org-latex-packages-alist))) (cond ((and (string-suffix-p ".png" out-file) (not imagemagick)) - (org-create-formula-image - body out-file org-format-latex-options in-buffer)) + (let ((latex-options (copy-tree org-format-latex-options))) + (when headers + (plist-put latex-options :headers + (append (plist-get latex-options :headers) headers))) + (org-create-formula-image + body out-file latex-options in-buffer))) ((string-suffix-p ".tikz" out-file) (when (file-exists-p out-file) (delete-file out-file)) (with-temp-file out-file diff --git a/lisp/org.el b/lisp/org.el index f3a19d643..d2ff4e41c 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -18571,6 +18571,9 @@ a HTML file." (if (string= bg "Transparent") "white" bg)))) (with-temp-file texfile (insert latex-header) + (when-let ((headers (plist-get options :headers))) + (dolist (h headers) + (insert h))) (insert "\n\\begin{document}\n" "\\definecolor{fg}{rgb}{" fg "}\n" "\\definecolor{bg}{rgb}{" bg "}\n" -- 2.18.0 --=-=-= Content-Type: text/plain > This works for me, cutting and pasting exactly what you have posted into a small org file. You probably have "\\usepackage{tikz}" set in your variable `org-format-latex-header' (another fix for the issue). > Output from my *Messages* buffer: > ,---- > | Wrote /tmp/x.org > | org-babel-exp process latex at position 128... > | Evaluate this latex code block on your system? (y or n) y > | executing Latex code block... > | (Shell command succeeded with no output) > | Code block evaluation complete. > | Local Ispell dictionary set to british > | Saving file /tmp/x.tex... > `---- > when I export to LaTeX. What does your message log say? Actually, the output looks identical when the command indeed fails to run latex without errors - the output file is generated nonetheless and the user thinks everything is ok. I'd suggest to add '-halt-on-error' to avoid creating an output file. This will then signal an error to the user. A possible patch is appended: --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0002-preview-latex-don-t-produce-output-file-on-error.patch >From 9f08a34c7eddfea394b9a1e78d54b3b26b139b51 Mon Sep 17 00:00:00 2001 From: Benjamin Motz Date: Sat, 29 Sep 2018 22:15:39 +0200 Subject: [PATCH 2/2] preview-latex: don't produce output file on error This will result in an error-message visible to the user. Otherwise, the user might just assume that everything went ok, even though there were issues when creating the output file. TINYCHANGE --- lisp/org.el | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index d2ff4e41c..0c7358eb0 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -3939,7 +3939,7 @@ All available processes and theirs documents can be found in :image-input-type "dvi" :image-output-type "png" :image-size-adjust (1.0 . 1.0) - :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f") + :latex-compiler ("latex -interaction nonstopmode -halt-on-error -output-directory %o %f") :image-converter ("dvipng -fg %F -bg %B -D %D -T tight -o %O %f")) (dvisvgm :programs ("latex" "dvisvgm") @@ -3949,7 +3949,7 @@ All available processes and theirs documents can be found in :image-input-type "dvi" :image-output-type "svg" :image-size-adjust (1.7 . 1.5) - :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f") + :latex-compiler ("latex -interaction nonstopmode -halt-on-error -output-directory %o %f") :image-converter ("dvisvgm %f -n -b min -c %S -o %O")) (imagemagick :programs ("latex" "convert") @@ -3959,7 +3959,7 @@ All available processes and theirs documents can be found in :image-input-type "pdf" :image-output-type "png" :image-size-adjust (1.0 . 1.0) - :latex-compiler ("pdflatex -interaction nonstopmode -output-directory %o %f") + :latex-compiler ("pdflatex -interaction nonstopmode -halt-on-error -output-directory %o %f") :image-converter ("convert -density %D -trim -antialias %f -quality 100 %O"))) "Definitions of external processes for LaTeX previewing. -- 2.18.0 --=-=-= Content-Type: text/plain Best regards, Benjamin --=-=-=--