emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Benjamin Motz <benni.motz@gmail.com>
To: Christian Moe <mail@christianmoe.com>
Cc: Emacs-orgmode@gnu.org
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	[thread overview]
Message-ID: <m236ts108f.fsf_-_@gmail.com> (raw)
In-Reply-To: <87mus1x6ft.fsf@gmail.com> (Eric S. Fraga's message of "Fri, 28 Sep 2018 11:27:50 +0100")

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

Eric S Fraga <esflists@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:

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-ob-latex-to-respect-the-keyword-headers.patch --]
[-- Type: text/x-patch, Size: 1786 bytes --]

From a54f83d2b411ae58941c32cb23fd47f38e70dc80 Mon Sep 17 00:00:00 2001
From: Benjamin Motz <benjamin.motz@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


[-- Attachment #3: Type: text/plain, Size: 965 bytes --]


> 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:

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0002-preview-latex-don-t-produce-output-file-on-error.patch --]
[-- Type: text/x-patch, Size: 2091 bytes --]

From 9f08a34c7eddfea394b9a1e78d54b3b26b139b51 Mon Sep 17 00:00:00 2001
From: Benjamin Motz <benjamin.motz@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


[-- Attachment #5: Type: text/plain, Size: 24 bytes --]


Best regards,
Benjamin

  reply	other threads:[~2018-09-29 21:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-28  9:39 Problems producing TikZ PNGs from LaTeX src blocks Christian Moe
2018-09-28 10:27 ` Eric S Fraga
2018-09-29 21:07   ` Benjamin Motz [this message]
2018-09-30 19:43     ` [patch] Problems producing TikZ PNGs from LaTeX src blocks (was: Problems producing TikZ PNGs from LaTeX src blocks) Christian Moe
     [not found]   ` <87mus18z8t.fsf@christianmoe.com>
2018-09-30 18:47     ` Fwd: Re: Problems producing TikZ PNGs from LaTeX src blocks Christian Moe
     [not found]     ` <87sh1tsmm0.fsf@delle7240.chemeng.ucl.ac.uk>
2018-09-30 18:56       ` Christian Moe

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=m236ts108f.fsf_-_@gmail.com \
    --to=benni.motz@gmail.com \
    --cc=Emacs-orgmode@gnu.org \
    --cc=mail@christianmoe.com \
    /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).