by Joel Tari

Flexible LaTeX Workflow: Part 1. Transform your compiler output into images

How would you proceed in order to insert a LaTeX math expression in an MS Office1 presentation, in a figure, in an e-mail or in a Slack type messaging system ?

Naively, you would have to first write the expression in a LaTeX file, compile it, and have your expression displayed in a PDF. Then, the most common pattern amounts to taking a screenshot from (a part of) the PDF and saving it as an image. The image is then inserted in the targeted communication format (e-mail, PowerPoint, etc…).

This pattern is a bit clumsy, and we can do better.

In this two-part article, we will engineer a DIY utility system to efficiently write and export math expressions anywhere, as a <svg>-text, vectorized image or pixel-based image depending on the situation.

First Part: Transform your compiler output into images

In this first part, we will only focus on the transformation of a LaTeX math code to image. We will compile a tex file directly from the command line and then exploit the output to produce pixel-based and vectorized images.

For this demo, the exported math expression will be a multivariate Gaussian definition:

\begin{equation*} p(x) \triangleq \frac{1}{\sqrt[]{ (2 \pi )^{d} |\Sigma|} } \exp \left( -\frac{1}{2} (x- \mu )^{ \top } \Sigma ^{-1} (x-\mu ) \right) . \end{equation*}

This is a typical formula that is a pain to write without any completion system or snippet engine. However, we assume that we can write LaTeX in efficiently in one editor that we master (e.g. vim). The point of this series is to allow us to write complicated LaTeX where we are at ease, and export the resulting written material to a place where it would be more difficult to do so. But first, we have to compile our code and produce that output.

LaTeX Compilation

A LaTeX compiler is a program that processes your tex file into a printable document. This compiler is a command usually called under the hood of your LaTeX system (by the IDE/plugin you use). There are many back-end compilers for LaTeX and the process is described here for pdftex. If you are using another one, refer to its corresponding man page documentation.

Let’s write the math expression in a small tex file:

\documentclass[14pt,border=12pt]{minimal}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{amsmath, amssymb}
\usepackage{xcolor}

\begin{document}
\pagestyle{empty}
$$
p(x) \triangleq 
  \frac{1}{\sqrt[]{ (2 \pi )^{k} |\Sigma|} }
  \exp \left\{ -\frac{1}{2} (x- \mu )^{ \top } \Sigma ^{-1} (x-\mu )  \right\}
$$
\end{document}

Save the file as gaussian.tex and apply the following compilation command in the terminal:

pdftex -fmt latex -interaction=nonstopmode -halt-on-error gaussian.tex

The flags -interaction=nontopmode and -halt-on-error prevent hanging during compilation.

Examine the output, we now have three new files next to our gaussian.tex:

folder
├── gaussian.aux
├── gaussian.dvi
├── gaussian.log
└── gaussian.tex

The .aux file is used for cross-referencing (labels, sections numbers …) and the log file is a compilation trace. We are not interested in those files. Note that there is no pdf file, the .dvi file is actually our main output.

Transform dvi output to images svg/png

The utility command dvisvgm transform the content of dvi to SVG:

dvisvgm --no-fonts --exact gaussian.dvi

Hence, this creates the gaussian.svg:

gaussian.svg
gaussian.svg

For a pixel-based image, we can use the convert command from ImageMagick:

convert -density 175 -background none gaussian.svg gaussian.png

In the same way, the gaussian.png image now exists:

gaussian.png
gaussian.png

You can observe the pixel effect when you zoom on the raster image. The severity of the effect depends on the value of the density flag, I usually fix it at 280. On the other hand, the quality of the SVG image is invariant with respect to the zoom level.

Summary and Plan for Part Two

In this post, we wrote a simple LaTeX file that contains just a math expression. We applied a basic latex compilation command on that file to come up with dvi file, and from that, converted it a svg image and a png image.

Thus, we now possess the ingredients to transform a math expression2 to an image. Admittedly, for each situation requiring the insertion of a math image, it would be tiresome to:

  • make a new folder with a new tex file,
  • write a stand-alone tex file with our math expression,
  • apply compilation and transformation commands,
  • transfer the image where we want them,
  • clean up those files once consumed.

We would greatly appreciate to efficiently both write LaTeX “on-the-fly”, and place directly the output image where we want it to be. Hence, no context-switching occurs. For that we have to interact with our Linux desktop environment and our targeted communication format (e-mail, chat window, design software etc…).

In the second part of this article, we will remedy these issues by integrating this method into a script that interacts with our Linux desktop environment.

Footnotes

  1. Do not use their MS Office equation system if you want to keep the math expressions beautiful. Only TeX based system can do that.

  2. Though we only focused on math expressions, extending that method with other LaTeX constructs like algorithms is straightforward.