Eric S Fraga <esfli...@gmail.com> 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:
>From a54f83d2b411ae58941c32cb23fd47f38e70dc80 Mon Sep 17 00:00:00 2001 From: Benjamin Motz <benjamin.m...@mailbox.org> 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
> 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:
>From 9f08a34c7eddfea394b9a1e78d54b3b26b139b51 Mon Sep 17 00:00:00 2001 From: Benjamin Motz <benjamin.m...@mailbox.org> 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
Best regards, Benjamin