# How to use AUCTeX to create a PNG file of a TikZ picture using the 'standalone' package (macOS)?

I would like to use AUCTeX to create a PNG image of a TikZ picture using the standalone package. I use macOS Sierra.

According to the standalone documentation v1.2-2015/07/15 (section 4.6 Conversion to images, p. 15)

Using the convert class option the standalone file can be easily converted to an raster image. This is done by executing an external program to convert the output file (PDF or PS) to an image (recommended is the lossless PNG format, but also others are supported).

According to subsection 4.6.1 Conversion settings (p. 15)

Conversion settings can be given as the value of the convert={<settings>} option. By default conversion is disabled (convert=false). If enabled without providing own settings (convert,convert=true) the following default settings are used: PNG format, a density of 300dpi, no explicit size and the output file name is given by \jobname, i.e. the name of the LaTeX document. Using the convert option with any value other than false will enabled it.

According to section 4.6.2 Conversion software (p. 16)

The conversion requires an external image converter program to be installed. By default the two following tools are supported and either of them must be installed in order to use the conversion feature. In order for an external program to be executed the -shell-escape option must be used for the compiler executable, e.g. pdflatex -shell-escape filename. Without this option no conversion is possible.

By default the conversion program of Image Magick is used for PDF LaTeX files [...]

So I used Homebrew (a popular package manager for macOS) to install Image Magick. It was installed in the directory /usr/local/Cellar/imagemagick/7.0.6-0. I then added the following lines to my Emacs init file (~/.emacs) so that Emacs would know where to look for the Image Magick executables:

(setenv "PATH" (concat (getenv "PATH") ":/usr/local/Cellar/imagemagick/7.0.6-0/bin/"))
(setq exec-path (append exec-path '("/usr/local/Cellar/imagemagick/7.0.6-0/bin/")))


After restarting Emacs, I wrote the following LaTeX manuscript, called my_tikz_drawing.tex:

\documentclass[tikz,convert]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (1,1)
[rounded corners] -- (2,0) -- (3,1)
[sharp corners] -- (3,0) -- (2,1);
\end{tikzpicture}
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% TeX-engine: default
%%% TeX-command-extra-options: "-shell-escape"
%%% End:


and compiled it using C-c C-a.

The result was a PDF file, but the PNG file was not created, at least not in the same directory where the LaTeX manuscript was.

I then opened Terminal (the macOS command-line console), cd-ed to the directory containing the LaTeX manuscript, and executed the following command:

pdflatex -shell-escape my_tikz_drawing


The result was a my_tikz_drawing.png file in the same directory.

A possible clue

Executing the following command, which is found in the log file, once from the Terminal and once from Emacs (using M-!):

convert -density 300 my_tikz_drawing.pdf -quality 90 my_tikz_drawing.png


yields the following results.

• From the Terminal: The command executes silently and creates the file my_tikz_drawing.png.

• From Emacs: No PNG file is created, and an error message shows in the mini-buffer:

convert: no images defined 'my_tikz_drawing.png' @error/convert.c/ConvertImageCommand/3254.

This is the same error that appears in the output from the Emacs pdflatex run.

• What is AUCTeX set to execute when compiling? The default may well omit -shell-escape option – Dai Bowen Jul 5 '17 at 14:00
• Exactly which version of AUCTEX are you using? I use an older one, 11.87 and there does not seem to be any options like that in the auctex .el files. Thus when I try your code, my Emacs just ignores the option – daleif Jul 5 '17 at 14:03
• you need 11.88 for that and also to allow local variables (which are disabled by default) – David Carlisle Jul 5 '17 at 14:05
• what does the log file look like, that should show if latex was called with shell-escape and if so whether it found the convert program – David Carlisle Jul 5 '17 at 14:07
• Just hit C-c C-l in Emacs (C might not be Ctrl on the Mac). That opens the log. The exact command executed is listed at the start of the log. Where exactly did you get the runsystem part from? – daleif Jul 5 '17 at 14:16

Emacs can't find the GhostScript executable. Run

which gs


in Terminal, and copy the path, say /usr/local/bin. Then add the following lines in your Emacs init file (say, ~/.emacs), and restart Emacs.

(setenv "PATH" (concat (getenv "PATH") ":/usr/local/bin/"))
(setq exec-path (append exec-path '("/usr/local/bin/")))


(The first of these lines may not strictly be necessary, but it doesn't hurt...)

This answer was made possible thanks to the good people in the Image Magick Users forum, who answered my post.