# \label is Undefined when used in \afterpage?

I am trying to force a figure and an image to show up on the same page back to back. I read the solution of using the \afterpage which works great. Except now the \label is undefined and I cannot refer to them!

What I have so far:

\afterpage{
\begin{figure}
\centering
\includegraphics[scale=0.28]{PATH_TO_IMAGE}
\caption{Some Caption For My Image}
\label{myFigure}
\end{figure}
\begin{table}
\centering
\caption{Some Caption For My Table}
\label{myTable}
\includegraphics[scale=0.09]{PATH_TO_TABLE}
\end{table}
}


Then I refer to these normally, using \ref{myFigure} or \ref{myTable}. But the compiler says undefined references, and the PDF shows ??. If I remove the /afterpage, then the reference works just fine.

Why?

EDIT

This is for my dissertation paper at my university. The following template is provided by the university to be used.

The path to the main .tex file is here

• Can you please add a full minimal working example so that people can see what packages etc you need to get your code to work (but don't add code that is not needed). This makes it much easier for people to help you. – user30471 Sep 11 '14 at 1:47
• Your configuration seems to be causing the problem as I just threw together a quick MWE and the links were fine, both with and without hyperref (and \autoref). Please post a MWE! – user30471 Sep 11 '14 at 1:57
• @Andrew, thanks for the response. This is a dissertation paper and I am using the template provided by the university. I provided the links above. Is this enough as a MWE? – Kousha Sep 11 '14 at 3:06
• If you wish to keep things together on the same page back-to-back, you could include them in the same float. Use \captionof (from capt-of or caption) to provide the correct \caption for the alternative float. – Werner Sep 11 '14 at 3:45
• Heiko's answered the question although it's hard to see why you need afterpage here. – David Carlisle Sep 11 '14 at 10:57

It's a timing problem between afterpage and the end of \include. After the .tex file is read, \include calls \clearpage, which is redefined by afterpage. First the page is output, then the stuff inside \afterpage is executed. The stuff are float objects, thus they are not yet set, waiting for the next call of the output routine. In the meantime, \include closes the .aux file for the included file and finishes its job. Then, when the floats are output, the labels are written to the old .aux file for the former included file, but this .aux file is closed, thus the labels are written to the .log file and console instead. In the next LaTeX run the labels remain unknown.
Workaround: An explicit \clearpage at the end of the included file helps. Then this \clearpage triggers the afterpage hook to execute, and the \clearpage at the end of \include will trigger the floats to output before the .aux file gets closed.