0

I have the following code:

The model is summarised in figure \ref{fig:model}.

\begin{figure}
\includegraphics{model}
\caption{HelloWorld}
\label{fig:model}
\end{figure}

When I put the figure environment early enough in the document that the figure appears before the reference in the compiled document the reference returns the correct figure number, but when I place it afterwards, as it is above, no matter how many times I compile it, I just get the ?? and undefined reference.

Could anyone please help?

EDIT I will try to make a MWE too but if it helps in the process of doing so, it suddenly worked when I changed the code from

\begin{document}

\renewcommand{\thefootnote}{\fnsymbol{footnote}}


\maketitle                  % create a title page from the preamble info
\mbox{}
\begin{romanpages}          % start roman page numbering
\end{romanpages}            % end roman page numbering
\begingroup
\let\clearpage\relax
\setlength{\parskip}{1em}
\include{AlzheimersModel}
\endgroup

\end{document}

to

\begin{document}

\renewcommand{\thefootnote}{\fnsymbol{footnote}}


\maketitle                  % create a title page from the preamble info
\mbox{}
\begin{romanpages}          % start roman page numbering
\end{romanpages}            % end roman page numbering
\begingroup
\let\clearpage\relax
\setlength{\parskip}{1em}
\section*{Alzheimer's Disease}

The model is summarised in figure \ref{fig:SpectorBiopsychosocialModel}.

\begin{figure}[H]
\includegraphics[width=\linewidth]{SpectorBiopsychosocialModel}
\caption{Spector's and Orell's biopsychosocial model reproduced from their 2010 paper}
\label{fig:SpectorBiopsychosocialModel}
\end{figure}

\endgroup

\end{document}

(i.e. I just copied the contents of AlzheimersModel.tex into the parent document.

  • 1
    Welcome to TeX.Stackexchange! Can you make a minimal working example (MWE)? That is not the default behaviour. – user36296 Apr 12 '18 at 17:07
  • 3
    The second run of a LaTeX file normally stabilizes the reference information and the reference information should print the correct value – user31729 Apr 12 '18 at 17:10
  • 1
    A working \clearpage is a requirement for \include. Otherwise, use \input instead of \include. – Heiko Oberdiek Apr 12 '18 at 17:36
  • @HeikoOberdiek nice one, thanks, that fixed it :) – whatisgoingon Apr 12 '18 at 17:44
  • 2
    did you find a tutorial that suggested \let\clearpage\relax ? If so we should try to get it changed. – David Carlisle Apr 12 '18 at 18:15
4

The \include feature requires a working \clearpage. At the start and end of \include the .aux files switches. The .aux file for the included file is opened and written during the processing of the included file. Labels also store page numbers. Because of the asynchronous output routine, the page numbers are known for sure at page output time. Therefore, the labels are written, when the page is shipped out.

If \clearpage at the end of \include is not working, then it can happen that the page with the labels is not yet output at the end of \include. Then the .aux file for the included file is closed and LaTeX switches to the main .aux file. When the page is finally is shipped out, the label cannot anymore written to the closed .aux file of the included file.

Summary: Use \input instead of \include, if the included file does not start or end at page boundaries. \include requires a working \clearpage.

Keep in mind that \clearpage is also used/needed at \end{document} or before chapters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.