96

I was surprised to find that standalone package can not compile a document with an equation:

\documentclass{standalone}
\begin{document}
\begin{equation} % not working!
F(V, T) = E(V) + D(T)
\end{equation}
\end{document}

Is there an option or some workaround that allows this? The current error is something like:

! Missing $ inserted.

in line 4. (The idea of course is that the including (main) document can just \input the standalone without knowing if is going to be an equation or something else)

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
alfC
  • 14,350

3 Answers3

126

The 1.0 version of standalone changed the default option from preview to crop. The latter has several benefits for the use-case the author (me) deemed most common, but forces restricted horizontal mode, which doesn't allow for lists or paragraphs. This causes an error for certain environments which require these.

One easy way is to enable the preview mode manually by using it as a class option (\documentclass[preview]{standalone}). You can also reenable this option as default using the standalone.cfg file as described in the manual. However, with preview you get a full line-wide PDF with the equation number (1) at the right, which is not really what you want, is it?

\documentclass[preview]{standalone}
\begin{document}
\begin{equation} % works now!
F(V, T) = E(V) + D(T)
\end{equation}
\end{document}

Instead it is better to use inline math mode using $ .. $ or \( .. \), which will work with crop and doesn't produce a full line nor a number. You can add \displaystyle if you need it:

\documentclass{standalone}
\begin{document}
$\displaystyle
F(V, T) = E(V) + D(T)
$
\end{document}

There is also the varwidth option which will wrap the content in a varwidth environment (varwidth package), which is a variable width minipage. This also allows for paragraph breaks etc. and might be better for multi-line equations. varwidth takes an option length argument as text width.

Martin Scharrer
  • 262,582
  • 4
    I actually have a math option in mind for standalone, but currently no time to implement it. – Martin Scharrer Mar 31 '12 at 09:33
  • With the [preview] option now works! 1) yes, the horizontal mode must have been a dilemma. 2) Answering your question about the numbering can give you ideas on how to implement a math option in standalone: In the final/long document is likely that one wants the numbering (so one is forced to use the equation environment) but in the preview/standalone the numeration doesn't make any sense. So one can add the following to the preamble of the standalone \makeatletter \def\@eqnnum{{\ }} \makeatother Something like this can be handled by your planned math option. – alfC Apr 01 '12 at 21:54
  • @MartinScharrer, I would like to see an align equation environment in your planned math option. Perhaps I'm being self-entitled, but hear out my use case:

    I'm using sympy to output some LaTeX math code and latexmk to refresh a PDF viewer. I'm still starting out with using this kind of environment. My objective is to automate tedious symbolic mathematical manipulations, especially helpful for non-math majors who need to tangle with big math (engineers like me).

    Or perhaps a full-blown math environment compatibility for standalone?

    – Kit Nov 15 '13 at 15:43
  • 2
    @MartinScharrer — Thanks for this great package! Is there any chance that you can add a note about this to the documentation? Currently, search for “math” does not give any results… – jmc Jun 12 '14 at 11:22
  • @MartinScharrer Is there any way to output to png with math? See my question. – mcp Sep 10 '20 at 16:16
  • Yes, preview+varwidth does exactly what I need: math formulas are displayed and the page is cropped to the content even horizontally. If the content is clipped (doesn't fit the page), I can set varwidth=10000 and it is fully displayed! Thank you very much! – anton_rh Nov 17 '21 at 14:32
10

standalone uses preview internally.

\documentclass{article}
\usepackage{amsmath}
\usepackage[active,tightpage]{preview}

% if you need the equation number, remove the asterix
\PreviewEnvironment{equation*}

% if you need paddings, adjust the following
\PreviewBorder=0pt


\begin{document}
\begin{equation*} % remove the asterix if you need the equation number
F(V, T) = E(V) + D(T)
\end{equation*} % remove the asterix if you need the equation number
\end{document}
3

I tried your code with a slight adjustment. Instead of \begin{equation} and \end{equation} I wrapped the equation with dollar symbols like this:

\documentclass{standalone} 
\begin{document}
$F(V_{x}, T, \sigma) = E(V_{x}) + D(T,\sigma)$
\end{document}

This compiles without error for me. It does not provide you with equation numbering though.

zyy
  • 2,146