[Tex/LaTex] Standalone: class vs package

standalone

What is the difference between the standalone class vs the package? From what I understand, the usage is different by \documentclass{report} or \usepackage{standalone} respectively. Is the class for the sub files and the package is for the main file?

I have a subfile:

\documentclass{report}
\usepackage[numbers]{natbib}

\begin{document}

dummyref \cite{GSMAIntelligence2016} 
\bibliographystyle{unsrtnat}
\bibliography{ref}

\end{document}

This compiles fine with the pdflatex + bibtex + pdflatex (x2) command however if I change it to documentclass{standalone} and run the same command I get the following errors:

! LaTeX Error: Something's wrong--perhaps a missing \item.
! LaTeX Error: Something's wrong--perhaps a missing \item.
! Missing \endgroup inserted.
! Missing } inserted.
! LaTeX Error: \begin{document} ended by \end{thebibliography}.
! Extra \endgroup.
! Too many }'s.

Best Answer

The standalone class is designed to create individual snippets of content whose size adapts to the content itself. This is implemented with the [crop=true] option, which is enabled by default. The [crop=true] option assumes that the content is a single paragraph. This is the source of the error you see when you compile your sample document with the standalone class.

There are two ways to solve this problem, with different effects:

  1. Use option [varwidth=true] which will allow multiple paragraphs, but will still crop the image to the size of its content.

  2. Use option [crop=false], which will not crop the document to the size of the content, and instead render it as a single page.

The class also allows you to specify an alternative class to compile the document in "within" the standalone compilation. The default us the article class. Changing this to the report class as you mentioned in the comments isn't actually doing any work here; it's only the crop=false option that is making the content fill a page.

Difference between the class and the package

The standalone class is useful especially if you are generating images for inclusion into other documents, especially because it can also do automatic conversion to other image formats.

On the other hand, the usefulness of the package version of standalone is less clear to me. In its most basic use, the standalone package allows you to use \input in a main document to insert standalone class documents into a larger document. I don't have a concrete use case for this, and I can't see an obvious use for it. Perhaps others who use it in this mode can comment on how they actually use it.

Related Question