Debian Codegraph @

texmf-dist/source/latex/hyperref/xr-hyper.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 1993-2026 David Carlisle
4 %
5 % This file is part of the `Hyperref Bundle'.
6 % -------------------------------------------
7 %
8 % This work may be distributed and/or modified under the
9 % conditions of the LaTeX Project Public License, either version 1.3
10 % of this license or (at your option) any later version.
11 % The latest version of this license is in
12 % http://www.latex-project.org/lppl.txt
13 % and version 1.3 or later is part of all distributions of LaTeX
14 % version 2005/12/01 or later.
15 %
16 % This work has the LPPL maintenance status `maintained'.
17 %
18 % The Current Maintainer of this work is the LaTeX Project.
19 %
20 % The list of all files belonging to the `Hyperref Bundle' is
21 % given in the file `manifest.txt'.
22 %
23 % \fi
24 % \iffalse
25 %% File: xr-hyper.dtx Copyright (C) 1993-2025 David Carlisle
26 %
27 %<package>\NeedsTeXFormat{LaTeX2e}
28 %<package>\ProvidesPackage{xr-hyper}
29 %<package> [2026-04-24 v7.01q eXternal References (DPC)]
30 %
31 %<*driver>
32 \documentclass{l3doc}
33 \usepackage{xr-hyper}
34 \GetFileInfo{xr-hyper.sty}
35 \begin{document}
36 \title{The \textsf{xr-hyper} package\thanks{This file
37 has version number \fileversion, last
38 revised \filedate.}}
39 \author{David Carlisle}
40 \date{\filedate}
41 \maketitle
42 \DocInput{xr-hyper.dtx}
43 \end{document}
44 %</driver>
45 % \fi
46 %
47 % \changes{v5.00}{1993/07/07}
48 % {First DPC version (by agreement with J-PD). New mechanism
49 % (\cs{read} instead of \cs{input}). (xr)}
50 %
51 % \changes{v5.01}{1993/07/20}{Fix bug added by DPC, v5.00 did not import
52 % aux files of \cs{include}'ed files. (Reported by J-PD) (xr)}
53 %
54 % \changes{v5.02}{1994/05/28}{Update for LaTeX2e}
55 % \changes{v5.03}{2018/10/01}{Fix for conditionals in aux file (xr)}
56 % \changes{v5.05}{2019/07/20}{include xcite (xr)}
57 %
58 % \changes{v6.00 beta2}{1997/10/06}
59 % {}
60 %
61 % \changes{v6.00 beta3}{1999/09/01}
62 % {Definitions of \cs{XR@addURL} corrected (Heiko Oberdiek)}
63 %
64 % \changes{v6.00 beta 4}{2000/03/22}
65 % {\cmd\xdef changed to \cs{protected@xdef}}
66 %
67 % \changes{v6.1}{2019/10/03}
68 % {Incorporate updates to \pkg{xr} (including citation support)}
69 %
70 % \changes{v7.00e}{2020-05-14}
71 % {add additional braces to \cs{r@}\meta{label} only if \pkg{hyperref} is not detected in the aux
72 % of the source document (https://github.com/latex3/hyperref/issues/118)}
73 %
74 % \changes{v7.00z}{2023-05-15}
75 % {unified and simplified the brace handling: all defined references have now
76 % five brace groups. Together with the planned change in LaTeX 2023-06-01 this
77 % will improve the combination of hyperref/non-hyperref documents.}
78 %
79 % \changes{v7.01g}{2023-11-26}
80 % {Always store the URL/file name of the external document to allow file links
81 % even if it has no destinations.}
82 %
83 % \changes{v7.01h}{2024-01-20}
84 % {do not expand the labels when reading, (https://github.com/latex3/hyperref/issues/327)}
85 %
86 % \changes{v7.01h}{2024-01-20}
87 % {support \LaTeX\ properties.}
88 %
89 % This package implements a system for eXternal References.
90 %
91 % It is an extension of the \pkg{xr} package. It was developed to support the
92 % extended label syntax of the \pkg{hyperref} package and to enable active links
93 % to the external documents.
94 %
95 % In the \LaTeX{} release 2023-06-01 the label syntax of \pkg{hyperref}
96 % and the \LaTeX{} kernel have been synchronized and there is no longer
97 % a need for two packages. \pkg{xr-hyper} already works with all documents --
98 % it is not required to load \pkg{hyperref} -- and its code will move in the
99 % next \LaTeX{} release into the \pkg{xr} package. Then \pkg{xr-hyper} can be
100 % deprecated.
101 %
102 % \section{Usage}
103 %
104 % \begin{syntax}
105 % \cs{externaldocument}\oarg{prefix}\texttt[nocite\texttt]\marg{document}\oarg{url}
106 % \end{syntax}
107 %
108 % If one document needs to refer to sections of another, say |aaa.tex|,
109 % then this package may be loaded in the main file, and the command
110 % \begin{verbatim}
111 % \externaldocument{aaa}
112 % \end{verbatim}
113 % given in the preamble.
114 %
115 % Then you may use |\ref| and |\pageref| (or |\nameref| if the
116 % package \pkg{nameref} has been loaded to refer to anything which has
117 % been given a |\label| in either |aaa.tex| or the main document.
118 % You may declare any number of such external documents.
119 %
120 % If any of the external documents, or the main document, use the same
121 % |\label| then an error will occur as the label will be multiply
122 % defined. To overcome this problem |\externaldocument| has an optional
123 % argument \meta{prefix}.
124 % If you declare |\externaldocument[A-]{aaa}|, then all
125 % references from |aaa| are prefixed by |A-|. So for instance, if a
126 % section of |aaa| had |\label{intro}|, then this could be referenced
127 % with |\ref{A-intro}|. The prefix need not be |A-|, it can be any
128 % string chosen to ensure that all the labels imported from external
129 % files are unique. Note however that the prefix is expanded and
130 % so should not contain commands that are not safe in this context.
131 %
132 % As first suggested in Enrico Gregorio's |xcite| package, the current version
133 % also allows |\cite| to reference |\bibitem| in the external document.
134 % For compatibility with |xcite|, |\externalcitedocument| is made available
135 % as an alias for |\externaldocument|
136 %
137 % Many packages have variant citation commands (natbib,
138 % biblatex,....) and the external document may or may not have used
139 % hyperref. Because of these differences the citation linking may not
140 % always work, it can be disabled by specifying \texttt{[nocite]} after the
141 % \meta{prefix}:
142 % \begin{verbatim}
143 % \externaldocument[][nocite]{aaa}
144 % \end{verbatim}
145 %
146 % The `document' referred to by the main argument \meta{document} is the file
147 % \file{document.aux} which must be somewhere on TeX's input path.
148 % Some packages (eg hyperref) really need to know the location of the
149 % final document rather than the aux file. By default this is assumed
150 % to be \file{document.pdf}. A package may redefine the command \cs{XR@ext} to
151 % change this default extension. However sometimes the final
152 % document may be in a position unrelated to the aux file, or the
153 % browser may not be able to find files at an arbitrary point in
154 % TeX's input path, so the final optional argument \meta{url} allows a full
155 % URL to the final document to be specified.
156 % \begin{verbatim}
157 % \externaldocument{aaa}[http://here.xxx.edu/this/path/to/aaa.pdf]
158 % \end{verbatim}
159 %
160 % The package stores the url of the external document in the label data. If can
161 % e.g. be retrieved with the \pkg{refcount} package
162 %
163 % \begin{verbatim}
164 % \usepackage{refcount,xr-hyper}
165 % \externaldocument{aaa}
166 % ...
167 % \getrefbykeydefault{intro}{url}{??} %prints aaa.pdf or ??
168 % \end{verbatim}
169 %
170 % \pkg{xr-hyper} supports also the properties introduced in \LaTeX{} 2023-11-01.
171 % Here the url of the external document is stored in the \texttt{xr-url} property.
172 %
173 % \begin{verbatim}
174 % \usepackage{xr-hyper}
175 % \externaldocument{aaa} %aaa contains \RecordProperties{intro}{page}
176 % ...
177 % \RefProperty{intro}{page} %gives page number
178 % \RefProperty{intro}{xr-url} %gives aaa.pdf
179 % \end{verbatim}
180 %
181 %
182 % \MaybeStop{}
183 %
184 % \section{The macros}
185 %
186 % \begin{macrocode}
187 %<*package>
188 % \end{macrocode}
189 %
190 % Check for the optional argument.
191 % \begin{macrocode}
192 \def\externaldocument{\@testopt\XR@cite{}}
193 \let\externalcitedocument\externaldocument
194 \def\XR@cite[#1]{\@testopt{\XR@[#1]}{}}
195 \def\XR@[#1][#2]#3{\@testopt{\XR@@{#1}{#2}{#3}}{#3.\XR@ext}}
196 % \end{macrocode}
197 %
198 % \subsection{helper definitions}
199 % To test the second optional argument
200 % \begin{macrocode}
201 \def\XR@@nocite{nocite}
202 % \end{macrocode}
203 % Needed in the processing
204 % \begin{macrocode}
205 \long\def\@gobblefour #1#2#3#4{}
206 \long\def\@firstoffour #1#2#3#4{#1}
207 \long\def\@secondoffour#1#2#3#4{#2}
208 \long\def\@thirdoffour #1#2#3#4{#3}
209 \long\def\@fourthoffour #1#2#3#4{#4}
210 % \end{macrocode}
211 % The url is added as fifth argument. The command used here is
212 % \cs{XR@addURL}. The command is more complicated as needed
213 % as it tries to handle also older documents with
214 % \cs{newlabel}'s with two arguments.
215 % \begin{macrocode}
216 \def\XR@addURL#1{\XR@@dURL#1{}{}{}{}\\}
217 \def\XR@@dURL#1#2#3#4#5\\{%
218 \unexpanded{{#1}{#2}{#3}{#4}}{\XR@URL}%
219 }%
220 % \end{macrocode}
221 %
222 % \subsection{Variables}
223 %
224 % Default file extension:
225 % \begin{macrocode}
226 \providecommand\XR@ext{pdf}
227 % \end{macrocode}
228 %
229 % \subsection{Processing}
230 % Save the optional prefix. Start processing the first |aux| file.
231 % Version beta2 also added another improvement unrelated
232 % to the hyperref support. Olivier Michel pointed out that
233 % if the aux file was not on texinputs you could not always go
234 % \cs{externaldocument}{/some/path/to/file}
235 % specifically that worked if file.aux was a `simple' document with
236 % one aux file, but if \cs{include} had been used, the `sub' aux files
237 % would not be found by xr in the remote directory.
238 % This version calls \cs{filename@parse} to get the directory name of the
239 % remote directory, which is then explicitly prepended to the names of
240 % any included aux files.
241 % \changes{v5.06}{2020-05-10}{Remove leading and trailing sapces from
242 % the filename (gh/2223) (xr)}
243 % \begin{macrocode}
244 \def\XR@@#1#2#3[#4]{{%
245 \makeatletter
246 \def\XR@prefix{#1}%
247 \def\XR@nocite{#2}%
248 \ifx\XR@nocite\XR@@nocite
249 \let\XR@bibcite\vadjust
250 \else
251 \let\XR@bibcite\bibcite
252 \fi
253 \def\XR@URL{#4}%
254 \set@curr@file{#3}%
255 \filename@parse\@curr@file
256 \XR@next\@[email protected]\relax\\}}
257 % \end{macrocode}
258 %
259 % Process the next |aux| file in the list and remove it from the head of
260 % the list of files to process.
261 % \begin{macrocode}
262 \def\XR@next#1\relax#2\\{%
263 \edef\XR@list{#2}%
264 \XR@loop{#1}}
265 % \end{macrocode}
266 %
267 % Check whether the list of |aux| files is empty.
268 % \begin{macrocode}
269 \def\XR@aux{%
270 \ifx\XR@list\@empty\else\expandafter\XR@explist\fi}
271 % \end{macrocode}
272 %
273 % Expand the list of aux files, and call |\XR@next| to process the first
274 % one.
275 % \begin{macrocode}
276 \def\XR@explist{\expandafter\XR@next\XR@list\\}
277 % \end{macrocode}
278 %
279 % If the |aux| file exists, loop through line by line, looking for
280 % |\newlabel| and |\@input|. Otherwise process the next file in the
281 % list.
282 % \changes{v5.06}{2020-05-10}{Add braces around the filename to
283 % support filenames with spaces (gh/223) (xr)}
284 % \changes{v7.01h}{2024-01-20}{Add braces around the filename to
285 % support filenames with spaces (gh/223) similar to xr}
286 % \begin{macrocode}
287 \def\XR@loop#1{\openin\@inputcheck{#1}\relax
288 \ifeof\@inputcheck
289 \PackageWarning{xr}{^^JNo file #1^^JLABELS NOT IMPORTED.^^J}%
290 \expandafter\XR@aux
291 \else
292 \PackageInfo{xr}{IMPORTING LABELS FROM #1}%
293 \expandafter\XR@read\fi}
294 % \end{macrocode}
295 %
296 % Read the next line of the aux file.
297 % \begin{macrocode}
298 \def\XR@read{%
299 \read\@inputcheck to\XR@line
300 % \end{macrocode}
301 % The |...| make sure that |\XR@test| always has sufficient arguments.
302 % \begin{macrocode}
303 \expandafter\XR@test\XR@line...\XR@}
304 % \end{macrocode}
305 %
306 % Look at the first token of the line.
307 % If it is |\newlabel|, define \cs{r@}\meta{label}, ensure that it has
308 % five label data argument and add the url as the last one.
309 % If it is |\@input|, add the
310 % filename to the list of files to process.
311 % If it is |\bibcite|, call a |\bibcite|.
312 % If it is |\new@label@record| add the url and then call it.
313 % Otherwise ignore.
314 % Go around the loop if not at end of file. Finally process the next
315 % file in the list.
316 % Make sure the arguments are handled outside the |\ifx| test,
317 % \begin{macrocode}
318 \long\def\XR@test#1#2#3#4\XR@{%
319 \let\XR@tempa\@gobblefour
320 \ifx#1\newlabel
321 \let\XR@tempa\@firstoffour
322 \else\ifx#1\XR@bibcite
323 \let\XR@tempa\@secondoffour
324 \else\ifx#1\@input
325 \let\XR@tempa\@thirdoffour
326 \else\ifx#1\new@label@record
327 \let\XR@tempa\@fourthoffour
328 \fi\fi\fi\fi
329 \XR@tempa
330 {%
331 \expandafter\protected@xdef\csname r@\XR@prefix#2\endcsname{\XR@addURL{#3}}%
332 }%
333 {\expandafter\bibcite\expandafter{\XR@prefix#2}{#3}}%
334 {\edef\XR@list{\XR@list\filename@area#2\relax}}%
335 {%
336 \edef\next{\noexpand\new@label@record{\XR@prefix#2}{\unexpanded{#3}{xr-url}{\XR@URL}}}%
337 \next
338 }
339 \ifeof\@inputcheck\expandafter\XR@aux
340 \else\expandafter\XR@read\fi}
341 % \end{macrocode}
342 %
343 % \begin{macrocode}
344 %</package>
345 % \end{macrocode}
346 %
347 % \Finale
348 %