# LaTeX math whitespace

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?

-
Doesn't happen to me. May you give a real example? –  egreg Oct 9 '11 at 20:17
It would be great if you could give a complete document that acts like this. For example, what kind of "math equation" are you using? $...$? $...$? \begin{align}...\end{align}? –  Ryan Reich Oct 9 '11 at 20:18
@egreg: can easily been seen with $y$ –  Herbert Oct 9 '11 at 20:39
I only didn't post it because I didn't see it as relevant, but, here it is: \documentclass{standalone \begin{document} $\int_1^a{\frac{dx}{x}} = \ln a$ $\int{x^n} = \frac{x^{n+1}}{n+1}$ \end{document} –  mebob Oct 9 '11 at 20:42
The one work-around is to use pdfcrop, as suggested in my comment where this original problem arose. However, this may be related to some options of preview. –  Werner Oct 9 '11 at 20:43
show 4 more comments

## 3 Answers

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.

-
add comment

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:

## 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}

-
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
add comment

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 9 '11 at 22:58
add comment