# Correct ordering of xr-hyper, hyperref, and cleveref

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}
$$A = b\label{eq:ab}$$
\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}
$$B = C\label{eq:bc}$$
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.

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}

$$A = b\label{eq:ab}$$

\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}

$$B = C\label{eq:bc}$$

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.