Debian Codegraph @

texmf-dist/source/latex/tools/xr.dtx

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