[Tex/LaTex] Can the standalone package work with sections

sectioningstandalone

While I was reading a thread on LaTeX workflows, I came across a comment in https://tex.stackexchange.com/a/22433/32782 which seemed to indicate that there are more than one person who can/could use the standalone package for book chapters. Usually people would use \includeonly rather than the standalone package for dividing a document into different tex files and activating just one tex file, but the idea of using standalone intrigued me and I thought I should give it a try, starting with an article with sections first, but my example doesn't seem to work. Here is my example:

The main tex file, flying-animals.tex:

\documentclass{article}
\usepackage{standalone}
\begin{document}
\input{dragons}
\input{birds}
\input{superman}
\end{document}

And a tex file for one section, dragons.tex:

\documentclass{standalone}
\begin{document}
\section{Theories of dragons}
Being dragons.
\subsection{Theory of angry dragons}
Being angry dragons.
\subsection{Theory of tiny dragons}
Being tiny dragons.
\end{document}

When I compile dragons.tex, I get the following error.

LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.3 \section
            {Theories of dragons}

Is it possible to make standalone work with sections?

Best Answer

Add varwidth as an option to the class in dragons.tex and this works fine. Note that if you compile dragons.tex you will get a single, long page, so this is far from ideal. standalone is mainly intended for graphics created by e.g. TikZ or PSTricks.

\documentclass[12pt]{article}
\usepackage{standalone}
\usepackage{lipsum}

\usepackage{filecontents}
% writes the following to dragons.tex
\begin{filecontents*}{dragons.tex} 
    \documentclass[varwidth]{standalone}
    \usepackage{lipsum}
    \begin{document}
    \section{Theories of dragons}
    Being dragons.
    \lipsum
    \subsection{Theory of angry dragons}
    Being angry dragons.
    \lipsum
    \subsection{Theory of tiny dragons}
    Being tiny dragons.
    \lipsum
    \end{document}
\end{filecontents*}

\begin{document}
\input{dragons}
\end{document}
Related Question