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:
\documentclass{article}
\begin{document}
Hello. This is a test.
\begin{equation}
L = 2
\end{equation}
\end{document}
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?
Best Answer
You can use the
standalone
class for this. It loads thepreview
package automatically to crop the resulting PDF to the content. This makes the usage ofpdfcrop
unnecessary.Simply exchange the
article
class withstandalone
. (It usesarticle
internally but another class can be specified using theclass
option.) Note that since v1.0 the default option has been changed frompreview
tocrop
. The latter is better for pictures etc. but doesn't support line breaks. Either selectpreview
manually or use thevarwidth
option.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: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.:
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.