# Compile LaTeX to PNG of specific width and DPI quality and cropped as much as possible

I want to compile LaTeX files directly to PNG (preferably skipping PDF) and to have the outputted PNG be as cropped as possible around the content.

The PNG output needs to very high quality and cannot exceed 500px in width and 500KB in file size.

To experiment I converted a PDF of one of the LaTeX files to a 600DPI PNG and it looked good. I converted the same PDF to a 300DPI PNG and it looked terrible.

I use MikTeX through Texmaker on a Windows 10 PC. I have about 400 of these LaTeX files but I'm fine doing about 50 per week.

Two typical example LaTeX files follow where I tried using the geometry package to control width.

\documentclass[fleqn]{article}
\nofiles
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[papersize={500px,100px},margin=1cm]{geometry}
\pagenumbering{gobble}

\begin{document}

\noindent Differentiate: $f(x)=\sqrt[8]{x}-\frac{5}{\sqrt{x}}$.

\end{document}

\documentclass[fleqn]{article}
\nofiles
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[papersize={500px,100px},margin=1cm]{geometry}
\pagenumbering{gobble}

\begin{document}

\noindent It is estimated that $t$ years from now, the population of a certain suburban community will be $p(t)=40-\frac{5}{7 t+3}$ thousand people. At what rate will the population be growing 5 years from now?

\end{document}

• Welcome to TeX.SE! Oct 18 at 19:08
• you can obviously script things so it seems to be in one step (the standalone package may help here) but latex can not genearte png so you need to generate pdf or dvi as an intermediate step "in the background" Oct 18 at 19:08
• I recommend that you use a batch file to 1. create a pdf, 2. crop using pdfcrop, 3. convert to PNG using Imagemagick. You can even create a loop in your batch file to operate over all TEX files in your directory. If you are not familiar with these tools, I could write a full answer later. Oct 18 at 19:21
• @James That sounds great. Does Imagemagick give me control over quality or whatever? I am not familiar with batch files. Oct 18 at 20:07
• Yes, you will have control of quality. See my answer below. You can create the batch file with a text editor and name it with a ".bat" extension. Oct 18 at 20:18

Welcome to the site!

As I mentioned in my comment, here's the way I'd do it. I copied your first example above into a file named sample.tex. Then I run the following batch file (on windows).

pdflatex sample.tex
pdfcrop sample.pdf sample.pdf
magick convert -alpha off -density 1000 sample.pdf -scale 25%% sample.png


This compiles to sample.pdf, crops the white borders, then calls the Imagemagick convert command to write a PNG. The convert flags I've used do the following...

-alpha off means that the PNG background won't be transparent.

-density 1000 converts to 1000 dpi.

-scale 25%% (double percentage signs needed in DOS) then scales down the PNG to 25%

Using a high DPI and then scaling down is know as "oversampling". In this case, it makes your image look a lot better than just using a DPI of 250. In this example, the final image is 476 pixels wide.

pdfcrop and imagemagick are both open source, freely available programs. Good luck!

EDIT: I think the following batch script will loop through all .tex files in the current directory. I don't have access to my Windows computer tonight, so I haven't tested it yet.

for %%f in (*.tex) do (
pdflatex %%~nf.tex
pdfcrop %%~nf.pdf %%~nf.pdf
magick convert -alpha off -density 1000 %%~nf.pdf -scale 25%% %%~nf.png
)

• @UngarLinski: Thanks for finding those typos! Older versions of Imagemagick didn't require the magick in the command. Imagemagick is very powerful and well worth your study time if you will be manipulating graphics using batch files in the future. Oct 19 at 11:06