# Special \cref display for \externaldocument

I am using cleveref pointing to equations within my document, as well as to a technical appendix in a separate file (loaded by \externaldocument)

Setup

Lets that the the first file was in main.tex, compiled, etc.

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


And another file references this, compiled after main

\documentclass{article}
\usepackage[capitalise,noabbrev]{cleveref}
\crefname{equation}{}{}
\usepackage{xr}
\externaldocument[main:]{main} %Loads a doc for external
\begin{document}
\section{My section}\label{sec:mysection}
$$B = C\label{eq:bc}$$
local ref: \cref{sec:mysection}, external ref: \cref{main:sec:mytitle} and \cref{main:sec:sub}\\
local eq ref: \cref{eq:bc}, external ref: \cref{main:eq:ab}
\end{document}


Question: The above should work, but while reading there is no way for me to know if a reference is to the external (technical appendix) in my case, or to an internal link.

Is there any way to have \cref conditionally change the text if a file is external (e.g. set something up so that if the refered label begins with main: then add a Main to the beginning of the title, etc.) That technique should work for sections, propositions, figures, but perhaps not for equations, which have the form (1) in this setup. Ideally this would show up as Main (1) or (Main 1)... whichever works, though I think the latter is a little clearer.

Extention with xr-hyper Something different will be necessary to get this to work with the related xr-hyper and hyperref packages. I don't care of the hyperlinks actually work between documents or not, the test is:

In file main.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 in the separate file:

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


this adds the same prefix (eg main:) to cleverref's able type as used for the label so the refence is of type main:section rather than section and then you can use

\Crefname{main:section}{\TR-section}{\TR-main-sections}%


to specify the format for that type.

\documentclass{article}
\usepackage{cleveref}
\usepackage{xr}
\makeatletter
\long\def\XR@test#1#2#3#4\XR@{%
\ifx#1\newlabel
\xr@cref#2@cref\relax#3\@nil
\else\ifx#1\@input
\edef\XR@list{\XR@list#2\relax}%
\fi\fi
\ifeof\@inputcheck\expandafter\XR@aux

\def\xr@@cref{@cref}
\def\xr@cref#1@cref#2\relax#3\@nil{%
\def\tmp{#2}%
\ifx\tmp\xr@@cref
\tmp
\else
\newlabel{\XR@prefix#1}{#3}%
\fi
}
\makeatother
\protected\def\TR{TR}% hide from lowercase
\Crefname{main:section}{\TR-section}{\TR-main-sections}%
\externaldocument[main:]{maindoc} %Loads a doc for external
\begin{document}
\section{My section}\label{sec:mysection}
Linking to \cref{main:sec:mytitle} or internally \cref{sec:mysection}
\end{document}

• Thank you so much, this really helps. I posted an implementation above. At this point, the only thing messing me up is that I need to have equations referenced by (1) as is done in \cref through \crefname{equation}{}{}. But I couldn't figure out how to get the () to appear. e.g. would like it to say Main (1) instead of just Main 1. Any ideas? – jlperla May 8 '15 at 23:43
• @jlperla (it's not really the site style to put answers into the question, better to add a notnother answer) you just need to look in cleverref see what it does for equations... I don't know the package at all but it seems to have \creflabelformat{equation}{\textup{(#2#1#3)}}% which puts the () in so I guess you need something similar for main:equation – David Carlisle May 9 '15 at 8:49
• Thank you, and sorry to break style. I edited the question to extend my use case for equations a little bit. I will try out your technique in the comment.l Thank you so much. – jlperla May 9 '15 at 13:58
• I tried to adapt your code to the \creflabelformat{equation}{\textup{(#2#1#3)}}% but I fear it is outside of my competence... – jlperla May 9 '15 at 14:14
• @jlperla I just added this and it seemed to work: \Crefname{main:equation}{\TR-equation}{\TR-equations} \creflabelformat{main:equation}{\textup{(#2#1#3)}} – David Carlisle May 9 '15 at 14:30