Enabling tikz previews in org-mode

I just started using tikz for diagrams in technical documentation. It would export fine and look great (both pdf and html), but previewing in the Emacs buffer wasn't working at all. This I had to fix.

After some light reading I began to set it up. But, it failed miserably on windows. Working through the issues, there are three things that need to be in place.

  1. Install Ghostscript
  2. Install Imagemagick
  3. Configure emacs correctly.

Installing Ghostscript

Download the 32-bit version (this seems to be the default by other tools) and install it on your system. The binary ends up being somewhere like:

C:/Program Files/gs/gs9.14/bin

Add this location to your path ensuring that all programs can find gswin32c.exe.

Installing Imagemagick

One of the key insights is that dvipng does not support tikz, but imagemagick does. Or, more correctly, the convert.exe program will convert the pdf output into a regular image file.

The Imagemagick download page has instructions for installing the binaries and dealing with any issues.

The installer should also setup the path to the executables correctly.

Configuring Emacs

This ended up being the trickiest part for figuring out what to set, but easy settings to make.

The first part sets up tikz as one of the default packages for LaTeX processing. Then, enable the tikzpicture environment for previewing.

(add-to-list 'org-latex-packages-alist
             '("" "tikz" t))

(eval-after-load "preview"
  '(add-to-list 'preview-default-preamble "\\PreviewEnvironment{tikzpicture}" t))

Together those two pieces enable LaTeX to do its thing to make the picture. What is missing now is the processing of the pdf output into an image format.

The two options for images are dvipng or imagemagick. It turns out that the former cannot handle tikz, so we use imagemagick. Setting the default program here is simple too.

(setq org-latex-create-formula-image-program 'imagemagick)

After you complete these steps, a simple C-c C-x C-l will preview the sections. And a C-c C-c will turn them back to the source text.

What did I not cover?

  • Installing a TeX system
  • Adding LaTeX header options

Published on Wednesday, September 10, 2014. Posted in: emacs, org-mode.