conflict between \include and the \hyperref package

In the MWE below, an error is thrown by the command sequence pdflatex mwe ; pdflatex mwe when mwe.tex is

\documentclass{amsart}
\usepackage{hyperref}
\includeonly{}
\begin{document}
\include{refFile}
Blah blah
\ref{eq:label}
\end{document}


The refFile.aux file was generated by the file refFile.tex below:

\documentclass{amsart}
\begin{document}
\begin{align}
\label{eq:label}
x=y
\end{align}
\end{document}


The source of the problem is the \include command. If one comments that out the code compiles, but of course the \ref{eq:label} command doesn't work.

Could anybody please advise what's going on, and how to work around this?

Thanks very much, Leo

• Why are you creating an .aux file manually? This is likely to cause trouble. If it's just for demonstration purpose, please add your real refFile.tex instead – siracusa Dec 6 '18 at 5:48
• Your \newlabel command is wrong with hyperref, it is missing arguments. Use a normal \label command and then check what is written to the aux-file to see the differences. – Ulrike Fischer Dec 6 '18 at 8:18
• I've added the refFile.tex file to the question, as requested by @siracusa . It generates the \newlabel as listed in my original question, @Ulrike_Fischer – Leo Simon Dec 11 '18 at 23:08

The problem seems to be that your are compiling mwe.tex and refFile.tex as two separate documents. This is not how \include is supposed to be used. All included documents should only contain actual document contents, but no \documentclass, \usepackage, \begin{document} etc. When I try to compile both files as separate documents, the following error pops up:

Runaway argument?
{\@firstoffive }\fi  \end {document}
! File ended while scanning use of \Hy@setref@link.


So you should rewrite your files such that mwe.tex is the main document with the full document structure:

\documentclass{amsart}
\usepackage{hyperref}
%\includeonly{}

\begin{document}
\include{refFile}
Blah blah
\ref{eq:label}
\end{document}


The included sub-file refFile.tex then looks like this:

\begin{align}
\label{eq:label}
x=y
\end{align}


Note that \includeonly{} won't include any sub-files and thus leave you with undefined references. If you remove that line, the resulting document correctly references your equation.

If you really want to include full documents, have a look at the sub-documents packages at CTAN.