7

when I try to display a math equation with LaTeX, there is always whitespace to the right and left of the math equation. I am trying it with code like this:

\documentclass{standalone}
\begin{document}
%Math equation here
\end{document}

This method works for regular text, it just doesn't seem to work for math. Is there a solution?

Martin Scharrer
  • 262,582
chbaker0
  • 267
  • 3
  • 6

3 Answers3

15

The construction \[...\] builds a whole paragraph, so that standalone is forced to leave space for it.

A formula written as

$\displaystyle <formula>$

will give the same result as \[<formula>\], but will not produce excess white space.

If you want two "centered" formulas one after the other, then

\documentclass{standalone}
\usepackage{amsmath}
\begin{document}
$\begin{gathered}
\int_1^a{\frac{dx}{x}} = \ln a \\
\int{x^n} = \frac{x^{n+1}}{n+1}
\end{gathered}$
\end{document}

is the way to go. For aligned equations you can use aligned.

David Carlisle
  • 757,742
egreg
  • 1,121,712
7

Based on the code in the comments this seems to have been an issue with how the standalone package is begin used. With the latest version of standalone, the [varwidth] option is needed. The MWE below does not produce any space around the formulas:

enter image description here

Note:

Code:

\documentclass[varwidth]{standalone} 
\usepackage{amsmath}
\begin{document} 
  \[ \int_1^a \frac{dx}{x} = \ln a \]
  \[ \int {x^n} = \frac{x^{n+1}}{n+1}\] 
\end{document}
Peter Grill
  • 223,288
  • Re varwidth: The v1.0 version uses crop by default, not preview as before, and this uses a horizontal box for the content. The \[ \] doesn't like restricted horizontal mode. The varwidth options wraps the content into a varwidth environment (from the varwidth package) which is a minipage with a variable width. Unfortunately I had to sacrifice 100% backwards compatibility in order to provide a good default setting. crop avoids a few drawbacks of preview. The default setting can be easily changed using the config file. See the manual, especially the "Version update" section – Martin Scharrer Jan 25 '12 at 17:00
3

try

\documentclass{article}
\pagestyle{empty}
\usepackage{auto-pst-pdf}
\PreviewEnvironment{displaymath}
\begin{document}
\begin{displaymath}y=f(x)\end{displaymath}
\end{document}

it creates a file <file>-pics.pdf which holds the equation as a cropped pdf

Run it with pdflatex -shell-escape <file> or alternetively with xelatex or lualatex

  • I tried to typeset your code using TeXShop first, with XeLaTeX selected and second, with LuaLaTeX selected. However, in both cases, I get "!undefined control sequence. \PreviewEnvironment". – Sony Oct 09 '11 at 22:58