# LaTeX - Standalone - figure reference in nested subfile error

for a few days I've been stacked with a problem which no manual or documentation resolved, please help:

I have my 'main.tex' report document file, where I use the 'standalone' package to load the chapter contents, something like this:

\documentclass[12pt,a4paper,oneside]{report}
\usepackage{standalone}
\begin{document}
\end{document}


Inside the 'chapter-about-this.tex' sub-file, I use the 'standalone' package too, this one is to load the charts with plots or another pictures taking hundreds of source code rows:

\documentclass{standalone}
\usepackage{standalone}
\usepackage{pgfplots}
\begin{document}
\chapter{Very important chapter}
Blah blah, look at \ref{fig:super-plot}.
\input{superplot-source-code}
\end{document}


And this nested 'superplot-source-code.tex' sub-file brings the plot:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{figure}
\label{fig:super-plot}
\caption{Very strange plot}
...plot...
\end{figure}
\end{document}


When I compile the chapter itself, figure reference is built with no problem. The problem comes, when I try to compile the main report file. Figure's caption has its ordered number, but the referencing text inside the chapter looks like 'Blah blah, look at ??.' - with two question mark instead of 1.2 or something like that.

For example, with the bibliography reference input with \cite command, there is no problem, it gives the number, although the source code with bibliography is also sub-filed with this package (but in 1st level include).

I found out, that compilation with 'latex' has to be done for two or three times to reach all the references. But I'm developing with TeXworks editor and compiling with pdfLaTeX+MakeIndex+BibTex with synctex enabled, which - I hope - should run latex for as many times as needed. So where is the point?

-
Welcome to TeX.SE! –  Jubobs Mar 14 '13 at 0:03
If I understand correctly, you have a \documentclass command in each of the files you input into main.tex. You cannot do that. There can be only one \documentclass in the whole document. Also, packages can only be loaded in the preamble of the document (i.e. before \begin{document}). –  Jubobs Mar 14 '13 at 0:06
@Jubobs that is true unless you use standalone class and package –  JLDiaz Mar 14 '13 at 0:12
@Dix Labels should appear after captions, not before. I don't know if this could be related with your problem though. –  JLDiaz Mar 14 '13 at 0:15
@JLDiaz I didn't know that. Thanks. –  Jubobs Mar 14 '13 at 0:17

Just compile more than once, I got the expected result.

\documentclass[12pt,a4paper,oneside]{report}
\usepackage{filecontents}

\documentclass{standalone}
\usepackage{standalone}
\usepackage{pgfplots}
\begin{document}
\chapter{Very important chapter}
Blah blah, look at \ref{fig:super-plot}
\input{superplot-source-code}
\end{document}
\end{filecontents*}

\begin{filecontents*}{superplot-source-code.tex}
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\label{fig:super-plot}
\caption{Very strange plot}\centering
\includegraphics[scale=0.5]{example-image}
\end{figure}
\end{document}
\end{filecontents*}

\usepackage{standalone}
\begin{document}