1

Is it possible to reference a label in another LaTeX document?

A very similar question has already been asked and answered in this forum. However, I'd like to use cleverefs and theorem lists which makes the problem more complex. Furthermore, the other question was asked 10 years ago; perhaps there are better ways to solve it now.

Consider the following LaTeX code which is copied from this answer and which I saved in a file named test.tex.

\documentclass[english]{amsart}
\usepackage{babel}
\usepackage{enumitem}  % for '\newlist' and '\setlist' macros
\usepackage[colorlinks=true]{hyperref}
%%\usepackage{amsthm}  % is loaded automatically by amsart document class
\usepackage[nameinlink]{cleveref}

\newtheorem{thm}{Theorem}[section] \crefname{thm}{Thm.}{Thms.} % singular and plural forms of label

\newtheorem{cor}[thm]{Corollary} \crefname{cor}{Cor.}{Cors.} % singular and plural forms of label

\newlist{enumthm}{enumerate}{1} % set up a dedicated enumeration env. \setlist[enumthm]{label=\upshape(\alph),ref=\upshape\thethm(\alph)} \crefalias{enumthmi}{thm} % alias 'enumthmi' counter to 'thm'

\newlist{enumcor}{enumerate}{1} % set up a second dedicated enumeration env. \setlist[enumcor]{label=\upshape(\alph),ref=\upshape\thecor(\alph)} \crefalias{enumcori}{cor} % alias 'enumcori' counter to 'cor'

\makeatletter \newcounter{subcreftmpcnt} % \newcommand\alphsubformat[1]{(\alph{#1})} %adapt .... \newcommand\subcref[2][\alphsubformat]{% \ifcsname r@#2@cref\endcsname \cref@getcounter {#2}{\mylabel}% \setcounter{subcreftmpcnt}{\mylabel}% \alphsubformat{subcreftmpcnt}% \else ?? \fi}
\makeatother \begin{document} \setcounter{section}{1} % just for this example

\begin{thm}\label{Thm:One} The following properties hold: \begin{enumthm} \item\label{Thm:One:1} (1>0) \item\label{Thm:One:2} (0<1) \end{enumthm} \end{thm}

\begin{cor}\label{Thm:Two} The following properties hold as well: \begin{enumcor} \item\label{Thm:Two:1} (2>1) \item\label{Thm:Two:2} (1<2) \end{enumcor} \begin{proof} \subcref{Thm:Two:1} follows from \ref{Thm:One:1} by adding 1 on both sides. Similarly, \subcref{Thm:Two:2} follows from \cref{Thm:One:2}. \end{proof} \end{cor}

\end{document}

I ran lualatex test twice, and both executions ended successfully. The typeset output was:

Theorem sublists

Now consider the following LaTeX code that I saved in a file named test2.tex and which is loosely modeled on this answer:

\documentclass{scrartcl}
\usepackage{xr-hyper}
\usepackage{hyperref}
\externaldocument[A-]{test}[test.pdf]
\usepackage[nameinlink]{cleveref}
\begin{document}
\ref{A-Thm:Two:1} follows from \ref{A-Thm:One:1} by adding 1 on both sides. Similarly,  \ref{A-Thm:Two:2}  follows from \ref{A-Thm:One:2}.
\end{document}

When I run lualatex test2, the execution ends successfully, and the typeset output is:

Cross referencing labels across files.

Questions

Is there a way to get the last reference in the output from test2.tex to typeset as Thm. 1.1.(b) rather than 1.1(b), in a similar manner to the way test.tex works?

Evan Aad
  • 11,066
  • 3
    xr-hyper should work I expect. If it doesn't, then make a small example and show the error you get – David Carlisle Jul 08 '21 at 08:40
  • @DavidCarlisle: I've edited my question per your comment. – Evan Aad Jul 08 '21 at 09:01
  • 1
    your example shows an error only vaguely connected to your question. You have used a command \subcref that you have not defined. – David Carlisle Jul 08 '21 at 09:04
  • @DavidCarlisle: OK. I've changed \subcref to \ref and it seems to work. Thanks. – Evan Aad Jul 08 '21 at 09:12
  • @DavidCarlisle: However, is there a way to get the output from test2.tex to typeset as Thm. 1.1.(b) rather than as 1.1(b), just like the output from test.tex? – Evan Aad Jul 08 '21 at 09:14
  • is that what your subcref command was doing? then use that (but you need its definition in that case) – David Carlisle Jul 08 '21 at 09:16
  • @DavidCarlisle: It's not \subcref its' \cref, and it's supposed to be available in package cleveref. – Evan Aad Jul 08 '21 at 09:21
  • @DavidCarlisle: Oh, OK. I've got it. Thanks. Should I post an answer or will you? – Evan Aad Jul 08 '21 at 09:26
  • @DavidCarlisle: I've posted an answer. Thank you very much! – Evan Aad Jul 08 '21 at 09:31
  • @DavidCarlisle: Just one more thing, please. Is it possible to automatically add a suffix to those references that point to another file, in order not to confuse them with references that point inside the same file? In other words, when I reference a label in another file, I'd like the output to be, say, test:1.1(b) or 1.1(b) (test.pdf) rather than simply 1.1(b). – Evan Aad Jul 08 '21 at 09:35

1 Answers1

0

I replaced the code in test2.tex by the following:

\documentclass{scrartcl}
\usepackage{xr-hyper}
\usepackage{hyperref}
\externaldocument[A-]{test}[test.pdf]
\usepackage[nameinlink]{cleveref}
\crefname{thm}{Thm.}{Thms.} % singular and plural forms of label
\begin{document}
\ref{A-Thm:Two:1} follows from \ref{A-Thm:One:1} by adding 1 on both sides. Similarly,  \ref{A-Thm:Two:2}  follows from \cref{A-Thm:One:2}.
\end{document}

and ran lualatex test2 twice. The executions terminated successfully, and the typeset output was as desired:

Using cleverefs in tandem with xr-hyperref to reference labels in a different file

Evan Aad
  • 11,066