Tikz snippet preview in orgmode

2019-05-09, updated 2022-07-23 — emacs tech blog   ⇦Emacs Tramp asking for passwordBaby white-woodpeckers are dumb⇨

For some reason, in my installation, both dvipng, and imagemgick don't work when using tikz, with errors such as the following when using a latex src block,

org-compile-file: File "/tmp/babel-aqvAMK/latex-Ehm75V.pdf" wasn’t produced. See "Org PDF LaTeX Output" for details

Or the image was simply not showing up.

My solution was to set use dvisvgm

(setq org-preview-latex-default-process 'dvisvgm)

This allows me to simply insert tikz code in my file, and the latex fragment will display it, as long as I have added the necessary header.

TODO dvisvgm has been broken after I updated emacs

#+latex_header: \usepackage{tikz}
\begin{tikzpicture}
\draw[->] (-3,0) -- (-2,0) arc[radius=0.5cm,start angle=-180,end angle=0] (-1,0) -- (1,0) arc[radius=0.5cm,start angle=180,end angle=0] (2,0) -- (3,0);
\filldraw (-1.5,0) circle[radius=1mm];
\filldraw (1.5,0) circle[radius=1mm];
\end{tikzpicture}
Sorry, your browser does not support SVG.

It is also considerably faster than using dvipng, at least on my machine.

If you want to use the image in your html export, instead of your pdf export, you can create a .svg folder, and then run the following:

#+HEADER: :file ./svg/example.svg 
#+HEADER: :results output raw :headers '("\\usepackage{tikz}")
#+HEADER: :fit yes 
#+BEGIN_src latex
\begin{tikzpicture}
\draw[->] (-3,0) -- (-2,0) arc[radius=0.5cm,start angle=-180,end angle=0] (-1,0) -- (1,0) arc[radius=0.5cm,start angle=180,end angle=0] (2,0) -- (3,0);
\filldraw (-1.5,0) circle[radius=1mm];
\filldraw (1.5,0) circle[radius=1mm];
\end{tikzpicture}
#+END_SRC 

Author: Ivan Tadeu Ferreira Antunes Filho

Date: 2022-07-23 Sat 05:11

Github: github.com/itf

Made with Emacs 27.1 (Org mode 9.3) and Org export head