How to make png files from latex

[linkstandalone]

?????

So, my original motivation here was to create an easy way to quickly generate transparent-background png images of equations that I could drop in LibreOffice impress when making presentations (or something similar). LibreOffice and Microsoft Office each have their own equation editors, but I have found latex to be much more extensible, efficient, and generally better looking. I realize LibreOffice has a latex extension, but I'm not really interested in using that if at all possible. In addition, I wanted the ability to easily convert this text to white because I tend to make presentations on a black background (and in some instances this is just a good option). There are a few ways you can accomplish this. For instance, GIMP is capable of taking the pdf files generated from latex and editing to the expected form, but that was too slow for me. The solution I ultimately decided upon involves compiling the latex into a postscript or pdf format and using imagemagick to convert to an image file with the intended properties. If you pack all these instructions in a simple shell script, the whole process becomes nearly instantaneous.

To make this solution work, you need to have imagemagick and a latex compiler installed. On Linux systems, you would generally use the TeX Live suite for latex complilation. As part of an aliasing workaround, I used texi2dvi to compile the tex and dvips to convert to postscript. In Arch Linux, imagemagick has its own package, TeX Live is available with the texlive-core package, texi2dvi is owned by texinfo (included in the arch linux base package), and dvips is owned by texlive-bin (a dependency of texlive-core).

Let's start with an example tex file with an equation in it, perhaps the Fermi-Dirac distribution.


\documentclass{article}
\pagestyle{empty}
\begin{document}
\begin{math}
\bar{n}_i = \frac{1}{e^{(\varepsilon_i-\mu) / k T} + 1}
\end{math}
\end{document}
Here, the code should be mostly self-explanatory for latex users. I have included \pagestyle{empty} such that when latex compiles, it only includes the intended text, as opposed to the whole area of a sheet of paper with a page number. This significantly speeds up the time for the whole script, but we will remove extra "white space" later. I'm going to save this tex script in fermi.tex for now.

Now let's start with the latex complilation. Traditionally, we would probably compile the code with pdflatex fermi.tex using TeX Live. I have experienced an aliasing issue using this compiler on one of my systems, but on another system, I had no issue. Essentially what would happen is that using imagemagick on the resulting pdf file would always appear to be low resolution, regardless of any settings. I eventually found a workaround which involved using GNU's Texinfo compiler. First thing is to compile the latex code to the Device-Independent File Format (*.dvi), which is a binary representation of the typeset document. This is done with texi2dvi fermi.tex Next thing is to convert this *.dvi file to postscript (fermi.ps)using dvips: dvips -E fermi.dvi In theory, we could just use texi2pdf fermi.tex, instead. Overleaf maintains a page on compilers and conversion which may be worth referencing.

Now that we have a compiled pdf or postscript file, let's make it a png with imagemagick. In the most simple case, we might get away with convert fermi.pdf fermi.png, but we'll throw it a couple options. Firstly, you need to decide on an image size, which you can declare with the -depth option. I like to have absurdly large images in the case I need to scale up the image, so I might give it something like 1200 dpi. You can check the resulting image's size later using imagemagick's identify tool: identify fermi.png. Next, you probably want to crop the image so only the text you care about is included, -trim. And I like to turn the text white in some cases, -channel RGB -negate. Finally, if you run the command just like this, imagemagick may spit out a warning convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG. You can suppress this with -colorspace RGB. So in the end, we may end up with convert -density 1200 -colorspace RGB -channel RGB -negate -trim fermi.pdf fermi.png

Now, let's stuff everything in a script.


#!/bin/sh
FILE=$(echo $1 | sed 's/.\{4\}$//') #Removes the last 4 characters (.tex) so we can generate files with other extensions
pdflatex $1
#texi2dvi $1
#dvips -E $FILE.dvi
#texi2pdf $1
COMPILED=$FILE.pdf
#COMPILED=$FILE.ps
convert -density 1200 -colorspace RGB -channel RGB -negate -trim $COMPILED $FILE.png #convert to png
rm $FILE.aux $FILE.log $COMPILED $FILE.dvi #clean up unnecessary files
Okay, and finally make the script executable (if it is not already) and execute it on the tex file. chmod 777 script.sh ./script.sh fermi.tex

Here's our result:
Fermi Dirac Distribution
And this actually turns out to be extensible to larger blocks of text, and for multiple pages.
Extra Text

PS: For anyone experiencing security policy issues, there may be a bug involving ghostscript. There has been instances in the past where the imagemagick project has changed the default security policy such that working with postscript of pdf may be disallowed. A workaround may include manually changing these settings in /etc/ImageMagick-7/policy.xml