Skip to content

Tutorial: Tikz ‐ Compiling figures to image files

Damien LaRocque edited this page Apr 7, 2022 · 2 revisions

TikZ is a nice LaTeX library to generate figures for articles and for presentations in a clean and professional way. A good tutorial about TikZ is available on the wiki.

Yet, LaTeX only compiles PDF files, which can be cumbersome when importing figures in a Powerpoint-like (Google Slides, Keynote, etc) presentation. If using beamer to create presentations in LaTeX is an option, it can be better in certain contexts to compile LaTeX to image files (jpg, png, etc). This tutorial shows how to setup your computer (or Overleaf) to compile TikZ figures in images.

Initial setup on your machine (for local LaTeX compilation)

When LaTeX compiles TikZ pictures into image files, it actually compiles a PDF and then it convert this PDF into a JPG or a PNG using :

These two softwares are generally installed by default on Ubuntu. Yet, it's a good practice to verify that we have both and the right versions :

gs --version # gs ≥ 9.24
convert --version  # Must be ImageMagick 6+

GhostScript used to have a security vulnerability until GhostScript version 9.24. Since we have a version of GhostScript that is higher than 9.24, GhostScript is safe enough to disable all security policies that are added by default in its configuration. To remove these security policies, we must edit /etc/ImageMagick-6/policy.xml and remove the following lines from the file:

<!-- disable ghostscript format types -->
<policy domain="coder" rights="none" pattern="PS" />
<policy domain="coder" rights="none" pattern="PS2" />
<policy domain="coder" rights="none" pattern="PS3" />
<policy domain="coder" rights="none" pattern="EPS" />
<policy domain="coder" rights="none" pattern="PDF" />
<policy domain="coder" rights="none" pattern="XPS" />

Compiling a TikZ figure in LaTeX

Here is a minimal working example of a LaTeX document that would generate image files :

% !TEX program = pdflatex
% !TEX options = --shell-escape -synctex=1 -interaction=batchmode -halt-on-error -file-line-error "%DOC%"

\documentclass[preview, tikz, border=7pt, convert={density=600, png}]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows.meta,backgrounds}
\tikzset{
    white background/.style={
        show background rectangle,
        tight background,
        background rectangle/.style={
            fill=white
        }
    }
}

\begin{document}
\begin{tikzpicture}[white background]
    \node [rectangle] (a_block) {This is a block};
\end{tikzpicture}

\begin{tikzpicture}[white background]
    \node [rectangle] (another_block) {This is another block in a new page};
\end{tikzpicture}

\end{document}

Let's examine this example line by line :

Compilation method

Retrieving the images

  • If you use a local compiler, the image files are directly inside your current working directory.
  • If you use Overleaf, you can download the image files with the Other logs and files button at the bottom of the log pane :

OVerleaf Export

Clone this wiki locally