[Tex/LaTex] Correct ordering of xr-hyper, hyperref, and cleveref

cleverefhyperrefxr

I want to use \cref, external documents, and hyperrefs. Take the following file, label it main1.tex and compile it:

\documentclass{article}
\usepackage[capitalise,noabbrev]{cleveref}
\crefname{equation}{}{}     
\begin{document}
    \section{MyTitle}\label{sec:mytitle}            
\begin{equation}
A = b\label{eq:ab}
\end{equation}
  \end{document}

Now, take another file which references it:

\documentclass{article}
\usepackage{xr-hyper}
%\usepackage{hyperref} %Want to turn this on!!!!!
\usepackage[capitalise,noabbrev]{cleveref}
\externaldocument[main:]{main1} %If you comment this off, can turn on the hyperref

\begin{document}
    \section{My section}\label{sec:mysection}
       \begin{equation}
       B = C\label{eq:bc}
       \end{equation} 
     local ref: \cref{sec:mysection}, external ref: \cref{main:sec:mytitle}\\
     local eq ref: \cref{eq:bc}, external ref: \cref{main:eq:ab}\\
\end{document}

If you compile this above, it will work and generate the correct references to the local and external files. However, if you turn on the hyperref, it doesn't compile. Conversely. If you comment out the \externaldocument and turn on the hyperref, it works fine (though it doesn't find the remove references, of course).

Question:
I want to use all of these together so that I can get hyperrefs in my PDF, use \cref for the references, and load in external documents. How can I get this to work? Or am I missing something obvious?

I don't need for hyperlinks between the documents themselves, so if that is the source of the problem and something I can turn off, then that is fine.

Best Answer

All of these packages are changes the way references are written in the .aux file. The correct load order is

\usepackage{xr-hyper}
\usepackage{hyperref}
\usepackage{cleveref}

but you need this in both the main and the external document. So

external document main1.tex:

\documentclass{article}

\usepackage{xr-hyper}
\usepackage{hyperref}
\usepackage[capitalise,noabbrev]{cleveref}
\crefname{equation}{}{}     

\begin{document}

\section{MyTitle}\label{sec:mytitle}            

\begin{equation}
  A = b\label{eq:ab}
\end{equation}

\end{document}

and main document

\documentclass{article}

\usepackage{xr-hyper}
\usepackage{hyperref}
\usepackage[capitalise,noabbrev]{cleveref}
\externaldocument[main:]{main1}

\begin{document}

\section{My section}\label{sec:mysection}

\begin{equation}
  B = C\label{eq:bc}
\end{equation} 

Local ref: \cref{sec:mysection}, external ref: \cref{main:sec:mytitle}

Local eq ref: \cref{eq:bc}, external ref: \cref{main:eq:ab}

\end{document}

will work providing hyperrefs everywhere.