Generating images with LaTeX equations

Sometimes it is useful to generate an image (like a PNG file) with a high-resolution mathematical equation. I know that there are online tools for doing that but imagine you are like me and prefer getting things done “in-house”.

I will assume you have docker available (if not, go to https://www.docker.com/ and install Docker Desktop).

In the first step we will need to get two images from Docker Hub (we do that in terminal / command line):

docker pull miktex/miktex
docker pull dpokidov/imagemagick

Both commands will download some stuff and should be ready in a minute or so. Now we will need our LaTeX code that defines the equation:

\documentclass{article}
\usepackage{amsmath}
\pagenumbering{gobble}
\begin{document}
\begin{equation*}
e^{i\pi }+1=0
\end{equation*}
\end{document}

Now assuming that the LaTeX code is saved in test.tex file we will need to execute 3 steps:

docker run --rm -v miktex:/miktex/.miktex -v $PWD:/miktex/work miktex/miktex latex test.tex
docker run --rm -v miktex:/miktex/.miktex -v $PWD:/miktex/work miktex/miktex dvipng -D 600 -bg Transparent test.dvi
docker run --rm -v $PWD:/imgs dpokidov/imagemagick imgs/test1.png -trim imgs/test2.png

The first command uses miktex/miktex Docker Image to “compile” out LaTeX file to DVI format.

The second command uses the same Docker Image to convert DVI into PNG image. Our LaTeX document has one page, so one PNG will be created after conversion (test1.png). Unfortunately, we are not done yet, because our PNG image has strange margins.

In the last step, we fix those margins by using dpokidov/imagemagick Docker Image. Our final result looks like this (test2.png):

Leave replay