# LaTeX math as an image of a specific size

I know very little about TeX and LaTeX, but I often encounter strings containing markup described as LaTeX, because various tools, including MathJax, SymPy and Apple's Grapher, use LaTeX as a lingua franca for math. A user can often right-click on a nicely-formatted equation in a GUI and get a representation of it "as LaTeX" or call a method in a script and get a string described as containing "LaTeX markup". (I don't know if I'm using the correct terminology.)

I'm trying to take such a string and use automation to turn it into an image of a specific size (625 x 465 pixels as an example) with the equation expanded or shrunk to fit the image, or, failing that, at least centered.

I'm programmatically creating .tex files, using latex to compile to DVI, then using dvipng to convert to PNG. I have tried over 30 permutations of the various suggestions I've read on Stack Exchange. I've tried document classes including standalone, article, report and minimal. I've tried packages including graphicx, preview and geometry. I've tried commands including resizebox, vspace, topskip, centering and noindent. I've tried dvipng options -T and -D.

The results I get are never quite right:

Wrong size:

Correct size, but oddly aligned:

Others are the correct size but blank (especially using standalone with options other than preview).

How can I produce images of the specified size that look right? Does anyone know a combination of a TeX document template into which I can insert the math markup, and a dvipng command that will convert it correctly?

I am running on Debian. latex --version reports pdfTeX 3.14159265-2.6-1.40.15 (TeX Live 2015/dev/Debian). dvipng --version reports dvipng 1.14.

an example latex command:

latex -halt-on-error -interaction batchmode -output-directory /data/notebooks/2016-09-13T17:41:22.181658_typesetting /data/notebooks/2016-09-13T17:41:22.181658_typesetting/foo21.tex


an example dvipng command:

dvipng -T 6.25in,4.65in -z 9 -bg gray 0.80 -o /data/notebooks/2016-09-13T17:41:22.181658_typesetting/foo21.png /data/notebooks/2016-09-13T17:41:22.181658_typesetting/foo21.dvi


I've been keeping a catalog of attempts in a Jupyter notebook. If necessary, maybe I could put all 31 (and counting) attempts on a Gist or something, with the .tex files, latex commands, dvipng commands, and result images.

• consider using package geometry to specify the output page dimensions. You need also that your TeX (LaTeX?) snippet produces no page number. However with dvipng I think it is not possible to get png images including the information of absolute pixel size. But others will correct me if needed. – user4686 Sep 15 '16 at 18:03
• the following thread lists.nongnu.org/archive/html/dvipng/2012-10/msg00000.html may be relevant. But perhaps things have changed since 2012. – user4686 Sep 15 '16 at 18:06
• actually the thread may be irrelevant ; have you tried -T="tight" or something like that with dvipng (and page dimensions -- and margins etc -- set by package geometry) ? – user4686 Sep 15 '16 at 18:11
• Is the issue that the png file is the wrong size, or then reincluding it in another document produces the wrong size? I am confused. – Steven B. Segletes Sep 15 '16 at 18:19
• I too don't understand the problem. Is the dvi file ok? Wouldn't producing a pdf and then converting to png being a proper solution? – Guilherme Zanotelli Sep 15 '16 at 19:14

In the end, the easiest way to get what I wanted was to use the preview package with the tightpage option in the .tex file, and use the convert command from ImageMagick to expand the image.

This is the template I use for the .tex file:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{bm}
\usepackage[active,displaymath,textmath,tightpage]{preview}
\pagestyle{empty}
\begin{document}
$% MATH GOES HERE$
\end{document}


Some of the packages and options might be unnecessary, but the above works for me.

Then I run this latex command (substitute your working directory for /bar):

latex -halt-on-error -interaction batchmode -output-directory /bar /bar/foo.tex


Then this dvipng command (I have to choose an appropriate value for the -D option manually):

dvipng -D 225 -z 9 -bg white -o /bar/foo.png /bar/foo.dvi


Then this convert command:

convert /bar/foo.png -background white -gravity center -extent 625x465 /bar/foo_resized.png


To get the necessary tools installed I ran:

apt-get install texlive
apt-get install texlive-latex-extra
apt-get install dvipng
apt-get install imagemagick