[Tex/LaTex] Nested standalone files with standalone package

nestingstandalone

Is it possible to nest files with the standalone package? I have something like the following:

In A.tex:

\documentclass{article}
\usepackage{standalone}
\begin{document}
\input{B}
\end{document}​

B.tex:

\documentclass{standalone}
\begin{document}
\begin{table}
  \input{C}
\end{table}
\end{document}​

C.tex is

\documentclass{standalone}
\begin{document}
\begin{tabular}{ l l r r } 
    A & B & C & D
\end{tabular} 
\end{document}​

A and C compile fine, but when compiling B.tex I get errors starting with :

! LaTeX Error: Not in outer par mode.

and complaining from there.

Is what I'm trying to do — where A.tex includes B.tex includes C.tex — possible? Possible with floats?

Best Answer

Yes, you can nest standalone files, however, as with any other document which \inputs standalone files you need to load the standalone package also in this case.

Another issue is that you can't use floats like table and figure environments in normal standalone files, because of the used preview mode used. You need either switch it off using the class option preview=false or redefine the table environment to not be a float.

% B.tex
\documentclass{standalone}
\usepackage{standalone}
\renewenvironment{table}{}{}% Ignore `table` environment in standalone mode.
\begin{document}
\begin{table}
  \input{C}
\end{table}
\end{document}​

If you have a \caption you should use:

\makeatletter
\renewenvironment{table}{\def\@captype{table}}{}
\makeatother

instead, otherwise you get an error.

Update 2011/12/21

I now release standalone v1.0 which redefines floats by defaults to work inside standalone files. See the float option in the manual for more details. See also Is it possible to use the figure environment with the standalone package?