# Crop equations with preview package

Is it possible to avoid left space from equations previewed with preview package?

\documentclass{minimal}
\usepackage{amsmath}
\usepackage[active,tightpage,displaymath]{preview}
\setlength\PreviewBorder{5pt}%

\begin{document}
$x=3+y^2$
\end{document}


The result is:

I would like a clipped (cropped?) equation. Is it possible?

Do you know a better solution to extract equations as images?

You can use the pdfcrop Perl script.

• I didn't know about it, thanks. It crops every page, so I still need package preview to produce pages with one mathematical expression on them. After that pdftk can split the document in individual pages. Mar 22, 2011 at 14:23
• the preview package will generate one equation per page, and pdftk can split them into one file per page. The command is pdftk latex_output.pdf burst. Sep 7, 2013 at 20:35
• Unless you absolutely require "real" displayed math and cannot use inline ones, MartinScharrer's answer is more preferable as pdfcrop is very slow can easily double the time it takes to generate the images. Sep 8, 2015 at 0:18

The left space is caused by the display style. You could use in-text style ($...$ or $$...$$) instead and use \displaystyle in it to get the same result without the space. You need to wrap the equation manually in a preview environment:

\documentclass{minimal}
\usepackage{amsmath}
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{5pt}%

\begin{document}
\begin{preview}
$\displaystyle x=3+y^2$
\end{preview}
\end{document}


Even shorter would be the use of the standalone class which uses preview internally. Note that the border option is only available since v0.4 2011/02/28. Before that you would need to set \PreviewBorder manually.

\documentclass[border=5pt]{standalone}
\begin{document}
$\displaystyle x=3+y^2$
\end{document}

• your solution works perfect for one line equations but not for multiline expressions (split, gather, ...). At least I don't know how to do it. May be my minimal working example was too minimal. Mar 22, 2011 at 14:12
• You can use aligned, gathered, and alignedat as alternatives; I don't think there's an analogue for split however. Sep 8, 2015 at 0:17
\documentclass{minimal}
\usepackage{amsmath}
\usepackage[active,tightpage,textmath]{preview}
\setlength\PreviewBorder{5pt}%

\begin{document}
$x=3+y^2$
\end{document}


Use textmath option instead of displaymath and use $...$ math environment.

Unfortunately, the tightpage option of the preview LaTeX package crops the page only vertically but doesn't crop horizontally. Even the -T tight option of dvipng doesn't help. dvipng prints the following warning: preview-latex tightpage option detected, will use its bounding box.

If you want to tightly crop the page horizontally you should use the standalone LaTeX package instead of the preview package:

\documentclass[class=minimal,preview,varwidth,border=5pt]{standalone}
\usepackage{amsmath}

\begin{document}
$x = 3 + y^2$
\end{document}


The standalone package removes the left and the right space around LaTeX formula:

If the LaTeX equation is too long and doesn't fit the page you can increase the width of the page:

\documentclass[class=minimal,preview,varwidth=1000pt,border=5pt]{standalone}


But it is only applicable to PDF documents. The width of PNG images is always unlimited.

• Welcome to TeX.SE! Nov 17, 2021 at 16:39