8

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:

enter image description here

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

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

4 Answers 4

4

You can use the pdfcrop Perl script.

3
  • 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.
    – Ignasi
    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.
    – Rufflewind
    Sep 8, 2015 at 0:18
11

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}
2
  • 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.
    – Ignasi
    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.
    – Rufflewind
    Sep 8, 2015 at 0:17
3
\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.

0

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:

1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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