emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Problems producing TikZ PNGs from LaTeX src blocks
@ 2018-09-28  9:39 Christian Moe
  2018-09-28 10:27 ` Eric S Fraga
  0 siblings, 1 reply; 6+ messages in thread
From: Christian Moe @ 2018-09-28  9:39 UTC (permalink / raw)
  To: Emacs-orgmode

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


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

Expected result: A tikzpic.png image of a green triangle.

Actual result: A tikzpic.png image (attached) that only shows the line
of TikZ code. It seems that the :headers argument isn't working.

If I add a #+LATEX_HEADER: \usepackage{tikz} line to my document, I can
export latex blocks as well as latex src blocks as drawings within an
exported PDF. But exporting the image as a PNG file continues to fail
(in this case, for some reason, the above src block results in an empty
image, no longer showing either the code or the intended picture).

What might I be missing?

Yours,
Christian


[-- Attachment #2: tikzpic.png --]
[-- Type: image/png, Size: 891 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problems producing TikZ PNGs from LaTeX src blocks
  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   ` [patch] Problems producing TikZ PNGs from LaTeX src blocks (was: Problems producing TikZ PNGs from LaTeX src blocks) Benjamin Motz
       [not found]   ` <87mus18z8t.fsf@christianmoe.com>
  0 siblings, 2 replies; 6+ messages in thread
From: Eric S Fraga @ 2018-09-28 10:27 UTC (permalink / raw)
  To: Christian Moe; +Cc: Emacs-orgmode

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

This works for me, cutting and pasting exactly what you have posted into a small org file.

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?

Finally, what version of org are you using?  (maybe should have asked this first...)

-- 
Eric S Fraga via Emacs 27.0.50, Org release_9.1.11-620-ga548e4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [patch] Problems producing TikZ PNGs from LaTeX src blocks (was: Problems producing TikZ PNGs from LaTeX src blocks)
  2018-09-28 10:27 ` Eric S Fraga
@ 2018-09-29 21:07   ` Benjamin Motz
  2018-09-30 19:43     ` Christian Moe
       [not found]   ` <87mus18z8t.fsf@christianmoe.com>
  1 sibling, 1 reply; 6+ messages in thread
From: Benjamin Motz @ 2018-09-29 21:07 UTC (permalink / raw)
  To: Christian Moe; +Cc: Emacs-orgmode

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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Fwd: Re:  Problems producing TikZ PNGs from LaTeX src blocks
       [not found]   ` <87mus18z8t.fsf@christianmoe.com>
@ 2018-09-30 18:47     ` Christian Moe
       [not found]     ` <87sh1tsmm0.fsf@delle7240.chemeng.ucl.ac.uk>
  1 sibling, 0 replies; 6+ messages in thread
From: Christian Moe @ 2018-09-30 18:47 UTC (permalink / raw)
  To: Emacs-orgmode, Eric S Fraga


(Forgot to copy this to the list, sorry)

> Hi, Eric
>
> Thanks for looking into this.
>
>> This works for me, cutting and pasting exactly what you have posted into a small org file.
>>
>> 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?
>
> org-babel-exp process latex at position 148...
> executing Latex code block...
> Code block evaluation complete.
> Saving file /home/cm/Pro/CiP/latex-babel-test.tex...
> Wrote /home/cm/Pro/CiP/latex-babel-test.tex
> Processing LaTeX file latex-babel-test.tex...
> PDF file produced.
> Running evince /home/cm/Pro/CiP/latex-babel-test.pdf...done
>
> Nothing obvious here that I can see.
>
>> Finally, what version of org are you using?  (maybe should have asked this first...)
>
> I'm on 9.1.14 (2018-09-24) via ELPA.
>
> Yours,
> Christian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Problems producing TikZ PNGs from LaTeX src blocks
       [not found]     ` <87sh1tsmm0.fsf@delle7240.chemeng.ucl.ac.uk>
@ 2018-09-30 18:56       ` Christian Moe
  0 siblings, 0 replies; 6+ messages in thread
From: Christian Moe @ 2018-09-30 18:56 UTC (permalink / raw)
  To: Eric S Fraga, emacs-org list


Eric S Fraga writes:

> On Friday, 28 Sep 2018 at 16:37, Christian Moe wrote:
>> Hi, Eric
>>
>> Thanks for looking into this.
>>
>>> | executing Latex code block...
>>> | (Shell command succeeded with no output)
>>> | Code block evaluation complete.
>>
>> executing Latex code block...
>> Code block evaluation complete.
>>
>> Nothing obvious here that I can see.
>
> Well, for some reason my case outputs that extra line about shell command succeeding...

Yes, that may be where the problem lies. I also have a problem
outputting PDFs instead of PNGs with the same setup (just changing .png
extension to .pdf). This gives me a message that the file wasn't
produced, but in fact a PDF with the correct image is produced --
however, it has a temporary file name, not the one I assigned to it.

> Have you tried with emacs -Q to make sure it's not a configuration error?

I should probably try this with a minimal init file (need one loading latex as a
babel language and probably a few other things). Didn't get around to that.

> I do have
>
>   (add-to-list 'org-latex-packages-alist '("" "tikz"))
>
> in my configuration so maybe that's the key even though you are specifying that this package should be included in the babel header spec.

I have tried this without success. Also tried adding the LATEX_HEADERS
keyword to the document. Doesn't solve the problem, but changes the
output from an image of the code to an empty image.

Yours,
Christian

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [patch] Problems producing TikZ PNGs from LaTeX src blocks (was: Problems producing TikZ PNGs from LaTeX src blocks)
  2018-09-29 21:07   ` [patch] Problems producing TikZ PNGs from LaTeX src blocks (was: Problems producing TikZ PNGs from LaTeX src blocks) Benjamin Motz
@ 2018-09-30 19:43     ` Christian Moe
  0 siblings, 0 replies; 6+ messages in thread
From: Christian Moe @ 2018-09-30 19:43 UTC (permalink / raw)
  To: Benjamin Motz; +Cc: Emacs-orgmode


Benjamin Motz writes:

> 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

Thank you. When I try this workaround, I run into another issue, which
may be local to my setup. When I add the :imagemagick header, Org tries
to generate a PDF.  This gives me a message that the file wasn't
produced, but in fact a PDF with the correct image is produced --
however, it has a temporary file name, not the one I assigned to
it. (The same problem occurs if I try to create file output from this or
any other Babel latex block with a .pdf extension instead of .png.) It
looks like Org for some reason fails to take a last step in the
latex-to-PDF export -- changing the file name and cleaning up temporary
files. Probably as a result of this failure, no PNG is created.

I have also tried to apply your patch, without effect. Maybe I did it
wrong, but it increasingly looks like I have some weird problem with my
setup in addition to the issue you uncovered.

Yours,
Christian Moe

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-09-30 19:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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   ` [patch] Problems producing TikZ PNGs from LaTeX src blocks (was: Problems producing TikZ PNGs from LaTeX src blocks) Benjamin Motz
2018-09-30 19:43     ` 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

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).