Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to convert my LaTeX documents into PNG. The problem is, I also need the resulting image to be as short as possible (height-wise). I've tried latex followed by dvipng, but the result is always the size of a page. For instance, take a .tex file containing:

Hello. This is a test.
  L = 2                                                                     

If I compile it with latex, and then run dvipng, I get a PNG file that's the size of a full page. What I need is for the PNG file to be only as tall as needed for everything to fit. So the image would end immediately after the equation. The image still needs to have full width (because of the equation numbering).

Is there a way to achieve that?

share|improve this question
Why do you want to do this? If it's to serve them on a web page inlined with text, try MathJax instead. –  Matthew Leingang Feb 23 '11 at 18:34
@matthew: It's to insert inside a word document. –  Bruce Connor Feb 23 '11 at 18:48
@Bruce: Ah. My sympathies. :-) –  Matthew Leingang Feb 23 '11 at 20:32
@Bruce: Something like: texpoint.necula.org or dessci.com/en/products/mathtype –  Martin Scharrer Feb 23 '11 at 20:40
Try this: sourceforge.net/projects/texsword –  amorua Nov 12 '11 at 16:32

8 Answers 8

up vote 129 down vote accepted

You can use the standalone class for this. It loads the preview package automatically to crop the resulting PDF to the content. This makes the usage of pdfcrop unnecessary.

Simply exchange the article class with standalone. (It uses article internally but another class can be specified using the class option.) Note that since v1.0 the default option has been changed from preview to crop. The latter is better for pictures etc. but doesn't support line breaks. Either select preview manually or use the varwidth option.

Hello. This is a test.
L = 2

There is a border class option which sets the border around the content (the default is 0.5bp). This option excepts either one (border for all sides), two (left/right, top/bottom) or four values (left, bottom, right, top).

To convert it to a PNG I recommend to use the convert command of Image Magick:

pdflatex file
convert -density 300 file.pdf -quality 90 file.png

Here the density is 300 DPI which can be adapted to your needs. The quality setting selects the compression level and other things and 90 is AFAIK the optimum.

You can also select the DPI resolution for X and Y separately and also resize the resulting image, e.g.:

convert -density 600x600 file.pdf -quality 90 -resize 1080x800 file.png

Update 2011/12/21:

The new version 1.0 standalone now has the ability to call the above command line (and others) automatically, e.g.:


or simply (using default setting 300dpi, no resizing, PNG):


This needs the -shell-escape compiler option to allow the execution of the conversion program from within the LaTeX document.

share|improve this answer
Matin, this seem extremely useful. Any idea as to when this will be released? I'd like to do something very similar, except only specify a minimum height for the page (and allow it to grow beyond that if needed), I still want the width of the PNG fixed. –  Peter Grill May 5 '11 at 6:20
Another +1 (I didn't know the standalone class allows to pass ImageMagick command). –  chl Nov 10 '11 at 12:37
@chl: Well, the develop version does. The current stable version on CTAN doesn't include this yet. –  Martin Scharrer Nov 10 '11 at 12:57
Great answer, thanks! I wrote a shellscript that takes care of all the repetitive wrapping and conversion stuff. Find it here and do whatever GPL3 allows you to do with it. –  Raphael Mar 29 '12 at 11:41
@Raphael: Thanks! That sounds great. I might update the question and add a link to your script. –  Martin Scharrer Mar 29 '12 at 12:45

Another approach with preview package directly.

% host.tex

% this is main.tex
Hello. This is a test.
L = 2

\immediate\write18{pdflatex main.tex}
\immediate\write18{convert -density 100  main.pdf main.png}
The following  is a PNG image.\newline

enter image description here


  • ImageMagick must be installed on your machine.
  • Compile the host.tex with pdflatex -shell-escape host.tex.
share|improve this answer

If you have access to an Apple computer, I highly recommend LaTeXiT, which exports formulas as PNG. If you're on a Mac it's included in MacTeX...

share|improve this answer
Not only exports, it saves the code inside the image so you can import them back to LaTeXiT to edit the image. –  Manuel Jan 21 at 13:08
@Manuel, sure there's even more to it. –  Habi Jan 21 at 14:52

You can install TikzEDT which has a stanalone feature. From here you can directly export you picture into .pdf,.jpg,.png etc without any trouble.

share|improve this answer

If you only want to use the PNG image to include it into MS Word you can choose among several programs that will greatly simplify your task. By hand you have to go through the whole process every time you modify the LaTeX part of your document.

As far as I know there is:

They all have different properties, some are freeware, shareware or opensource but it improves your workflow quite a bit if you can simply change the figure in Word directly and use LaTeX in the background for nice typesetting. Internally they all use some VBA or .NET code to connect the LaTeX, dvips, convert toolchain to MS Word.

share|improve this answer

then dvipng crops the whitespace on top and bottom

share|improve this answer

If you have a dvi file file.dvi, running dvipng -T tight file.dvi will produce a png with the image automatically cropped as much as possible. (You might also want to set the output resolution using the -D flag, as in dvipng -T tight -D 150 file.dvi for 150 dots per inch.)

As Andrew points out in his answer, getting rid of the page numbers is a good idea.

share|improve this answer

One thing that's easy to miss is page numbers. The page number restricts the height of the final image so it's best to leave it out. An easy way to do that is to use the minimal document class.

What I do when doing images for this place is to have a document a bit like:

Hello. This is a test.
L = 2

Then run pdflatex on it to get a PDF; next run pdfcrop (comes with TeXLive) to make it as small as possible; finally convert it to PNG using the NetPBM library tools. (This is on a Unix machine.) So my workflow is:

pdflatex document.tex
pdfcrop document.pdf
pdftoppm document-crop.pdf|pnmtopng > document.png

et voila:

small png

share|improve this answer
Perfect! Thanks :) –  Bruce Connor Feb 23 '11 at 18:34
Is there a way to fine tune the resolution in the third command? –  Bruce Connor Feb 23 '11 at 18:40
@Bruce: The manpage for pdftoppm says that you can specify the resolution by using the argument -r RES. The default is 150DPI so experiment with that to find one that you like. –  Loop Space Feb 23 '11 at 22:04
You can merge the last two commands: pdftopnm doc.pdf | pnmcrop | pnmtopng > doc.png. –  Paul Gaborit Aug 10 '12 at 0:14
This is what I needed! Thanks a bunch! –  Goos Jul 17 at 19:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.