How to Convert a LaTeX PDF to PNG

  • In this tutorial, we will learn how to compile a LaTeX document and get a PNG or jpeg file using the online LaTeX editor Overleaf. You may find this useful in the following cases:
    Ā  ā€£ Sharing your illustrations on social media,
    Ā  ā€£ Add images to your Blog,
    Ā Ā ā€£ Use images in PowerPoints if you are not Fan of beamer,
    Ā Ā ā€£ Or print your TikZ images on Coffee mugs šŸ˜‚
  • The method is not limited to equations, it can be used for TikZ illustrations, tables, and any typed LaTeX document. I used it to generate most of images published in different tutorials!

Use standalone class...

Most of the time, I useĀ OverleafĀ (Online LaTeX editor) to draw my illustrations and I choose Standalone as a documentclass. This class allows us to get an output file that only contains the picture with no extra (or specified) white margins. Here is an example of an equation with 0.2cm white margin:Ā 

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
	\node at (0,0) {\Huge $e^{i\pi}=-1$};
\end{tikzpicture}

\end{document}
Convert LaTeX equation to PNG

LaTeX equation converted to PNG

LaTeX to PNG

Converting LaTeX to PNG can be easily done in Overleaf as follows:

Ā  Ā 1. In the main folder, create a file and name it latexmkrcĀ without extension

Ā  Ā 2. Write the following line code in the latexmkrc file:Ā Ā 

END { system('convert -density 300 output.pdf myImage.png'); }

Ā  3. Compile your document

Ā  4. Ā You get a png file in the tab logs and output filesĀ with the same name specified in the line code (myImage.png).

Let's go deeper!

The idea is to create a configuration file in Overleaf project that contains just a single line:Ā 

END { system('convert -density 300 output.pdf myImage.png'); }

The file is namedĀ latexmkrcĀ as shown below:

LaTeX Equation to PNG

After a successful compilation of the main LaTeX file, we get a png file in the tab logs and output filesĀ with the same name specified in the line code (myImage.png).Ā 

Export LaTeX to PNG

The line command calls a Perl function system(...) which excutes a program calledĀ convert. The latter does the conversion of the pdf file (output.pdf) at 300 dpi (specified using -density 300) to a PNG file with the chosen name myImage.png.Ā 

In this post, we will highlight some useful options and the reader may refer to ImageMagick's online help for a complete version.

Resize the generated image file

In order to change the size of the generated image file, we can add -resizeĀ as follows:

END {system('convert -density 600 -resize 20% output.pdf myImage.png');}

This will produce an image with a size 20% of the original image and at 600 dpi.

LaTeX Equation to PNG resized image

20% of the original image

Convert LaTeX equation to PNG

Original image

Change the background color

Ā By default, the generated image has a transparent background. In order to change its color (for example to yellow), we add -background yellow -flatten to the previous line code:

END { system('convert -density 600 -background yellow -flatten output.pdf myImage.png');}Ā 

LaTeX to PNG Background color yellow

Converted LaTeX file to PNG with yellow background

LaTeX to PNG Background color cyan

Converted LaTeX file to PNG with cyan background

How to export a JPEG image from LaTeX

This can be done by changing the name of the image file from myImage.pngĀ  Ā to myImage.jpeg. That's all!

END {system('convert -density 600 -background yellow -flatten output.pdf myImage.jpeg'); }

Convert a specific page of a document to an ImageĀ 

Until now, we have considered standalone class to export TikZ illustrations to an image instead of a PDF file.

Suppose we have a document with many pages and we Ā would like to convert one page to an image. In this case, we modify output.pdfĀ to output[numberOfPage].pdf.Ā 

Here is an example of exporting page 2 of the PDF document to a PNG image with a nameĀ page2:

END { system('convert -density 600 -background white -flatten output[2].pdf page2.png'); }

4 thoughts on ā€œHow to Convert a LaTeX PDF to PNGā€

Comments are closed.