making external references stick out

I am splitting some text into several files, so there are external references. These I would like to emphasize in some way, for printing, like: "theorem 7 (from OtherPaper)". Since there is a lot of such references, and the splitting is not stable, I would like an automatic way to achieve this. What I have so far is:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xr-hyper}
\usepackage[hypertex]{hyperref}
\begin{document}
\def\ifUnDefinedCs#1{\expandafter\ifx\csname#1\endcsname\relax}  %Victor Eijkhout, "TeX by Topic", p.143
\newcommand{\myautoref}[1]{\ifUnDefinedCs{r@#1} {\pending{#1}}\else\autoref{#1}\fi}
\newcommand{\pending}[1]{\color{red}\autoref{#1} from "file.tex"}
% \externaldocument{file}[../file]  %(A)
Using \myautoref{sec:vis} and \myautoref{sec:vis},...
% \externaldocument{file}[../file]  %(B)
\end{document}


As soon as \externaldocument{file}[../file] is issued, all labels in file.aux are added immediately, if I got that right. So if line (A) is used, the references upon them shall not "look any different" in print than any local references. If line (B) is used, the labels are added kinda too late, and repeated compilation does not change anything.
My question: Is there a possibility to enforce a "second pass", i.e. activate extraneous labels after their references have been marked?

• Do you use a bibliography? – Romain Picot Oct 8 '15 at 16:19
• yes, but what is your drift? – user89322 Oct 8 '15 at 16:24
• If you cite document from other paper that are in your bibliography, you can use \cite[Theorem 7]{OtherPaper} to make the ref you want. Does correspond to what you want? – Romain Picot Oct 8 '15 at 16:26
• well thank you, I didn't know that, but it does not quite solve the problem: I would like \myautoref{lem:bigdeal} uniformly in the documents, independant of actual theorem names/numbers and of actual splitting – user89322 Oct 8 '15 at 16:38
• If you want only to reference from other LaTeX file perhaps the xr package is what you need. – Romain Picot Oct 8 '15 at 16:43

I am not sure this is what the O.P. requests: The xr package allows to specifiy a label prefix for the external document, say A-.

It's necessary (in my point of view) to check first whether this \r@A-#2 command exists at all, if this is not the case, the reference is considered to be a local one and \autoref{...} is used then.

By the way: \ifUnDefinedCs is not necessary, since the LaTeX core provides \@ifundefined{}{}{} already.

\documentclass{article}
\usepackage{xcolor}
\usepackage{xr-hyper}

\usepackage{hyperref}

\externaldocument[A-]{extfile}  %(A)
\makeatletter

\newcommand{\myautoref}[2][A-]{%
\@ifundefined{r@#1#2}{% Nope A-#2 isn't there
\@ifundefined{r@#2}{%
}{%
\autoref{#2}%
}%
}{%
{\pending{#1#2}}%
}%
}
\newcommand{\pending}[1]{\color{red}\autoref{#1} from "file.tex"}

\makeatother

\begin{document}
Using \myautoref{sec:vis} and \myautoref{sec:vis}, but \myautoref{localsection}

\section{Local section}\label{localsection}
\end{document}


extfile.tex

\documentclass{article}

\begin{document}
\section{A section} \label{sec:vis}
\end{document}


• a small addendum: instead of doing nothing if the label is nowhere defined, one might use \@ifundefined{r@#2}{\refdangling{#2}}{\autoref{#2}} with \newcommand{\refdangling}[1]{{\color{magenta}\IDontKnow\{#1\}}} or just use \autoref{#2} without checking r@#2 – user89322 Oct 9 '15 at 12:35