1

My question here is about generating images from LaTeX source blocks in .org files

The following source block (edited down to a simple use case) works fine and produces a .pdf, but this does not appear in the document when using org-display-inline-images.

#+header: :packages '("\\usepackage{tikz} \\usetikzlibrary{shapes.geometric, arrows} ")
#+begin_src latex :file figure.pdf :results raw file
  \begin{tikzpicture}
    \draw[red,thick,dashed] (0, 0) circle (1cm);
    \draw[blue, thick, dashed] (0.5, 0) circle (1.5cm);
    \path (-1.5,-1.5) node[]{$Test$};
    \path (2,2) node[] {$Another$};
  \end{tikzpicture}
#+end_src

Changing :file figure.pdf to :file figure.png results in a tiny image with all the content crammed in together. (CORRECTION: all the text is jammed together, without the text commands, there is nothing in the image)

If I follow one of the examples in the documentation and add the header

#+header: :imagemagick yes :iminoptions -density 600 :imoutoptions -geometry 400

I get the error message

Converting pdffile file convert -density 600 /tmp/babel-pv17K3/latex-KCde1K.pdf -geometry 400 figure.png...
convert: attempt to perform an operation not allowed by the security policy `gs' @ error/delegate.c/ExternalDelegateCommand/382.
convert: no images defined `figure.png' @ error/convert.c/ConvertImageCommand/3322.

I worked out that this is due to an old security issue (e.g. see here in ghostscript, and can be fixed by editing /etc/ImageMagick-7/policy.xml. I've tried it myself and the fix works. However, I'm a cautious guy, and would prefer not to edit policy.xml. So,

Question: Is there some alternative to ghostscript/ImageMagick for generating .png images in (LaTeX) org-babel code blocks?

For example, the LaTeX previews generated in org-mode come out alright, and are in .png format, it seems that these previews are generated via dvipng, see here. Is there any way to add options to dvipng via headers to make the .png images come out correctly?

I've also generated .png images from Python source blocks, using the matplotlib library, which doesn't (apparently?) use ghostscript as a backend for generating .png files.

8
  • 1
    FWIW, changing to :file figure.png does NOT produce a tiny image for me: the resulting png shows two circles, blue with diameter 3cm and red with diameter 2cm, exactly as specified. Although Org mode does its best to hide it, the command it executed to convert the pdf to png was convert -density 134.39999999999998 -trim -antialias /tmp/orgtexGEv8Ol.pdf -quality 100 /tmp/orgtexGEv8Ol.png.
    – NickD
    Oct 22 '21 at 16:46
  • @NickD Thanks - I believe you helped me on an earlier question involving latex image previews. Just FYI, I am doing this in a completely blank .org file without any #+LaTeX_HEADERS involved to makes sure nothing is going wrong, or latex templates, so that removes one explanation as to why the images are tiny for me but not for you... Oct 22 '21 at 19:05
  • @NickD do you have something in your init.el that automatically picks ImageMagick? And does your /etc/ImageMagick-X/policy.xml allow for ghostscript, or gives read-write permission for PDF files? Oct 22 '21 at 19:08
  • I do use the imagemagick method in org-preview-latex-process-alist, i.e. my org-preview-latex-default-process is imagemagick. I have not modified any settings in ` org-preview-latex-process-alist. My /etc/ImageMagick-6/policy.xml has a policymap section with everything commented out: I believe I have not touched it since installation (running on Fedora 33).
    – NickD
    Oct 22 '21 at 19:51
  • Trying the dvipng method instead fails: dvipng compl;ains This is dvipng 1.17 Copyright 2002-2015, 2019 Jan-Ake Larsson [1 <raw PostScriptdvipng warning: PostScript environment contains DVI commands.... and produces a PNG that cannot be displayed, although it returns a 0 exit status...
    – NickD
    Oct 22 '21 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.