summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/expl3/l3xref.dtx
blob: 5c84cce4d553ad85049759305d87a71903ff21a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
% \iffalse
%% File: l3xref.dtx Copyright (C) 2006,2009 LaTeX3 project
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version.  The latest version
%% of this license is in the file
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% This file is part of the ``expl3 bundle'' (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%%    http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%%   Snapshots taken from the repository represent work in progress and may
%%   not work or may contain conflicting material!  We therefore ask
%%   people _not_ to put them into distributions, archives, etc. without
%%   prior consultation with the LaTeX Project Team.
%%
%% -----------------------------------------------------------------------
%
%<*driver|package|testfile>
\RequirePackage{l3names}
%</driver|package|testfile>
%\fi
\GetIdInfo$Id: l3xref.dtx 2151 2011-02-16 06:58:12Z will $
       {L3 Experimental cross referencing}
%\iffalse
%<*testfile>
\ProvidesFile{\jobname.tex}
  [\filedate\space v\fileversion\space\filedescription]
%</testfile>
%<*driver>
%\fi
\ProvidesFile{\filename.\filenameext}
  [\filedate\space v\fileversion\space\filedescription]
%\iffalse
\documentclass[full]{l3doc}
\begin{document}
\DocInput{\filename.\filenameext}
\end{document}
%</driver>
% \fi
%
%
% \title{The \textsf{l3xref} package\thanks{This file
%         has version number \fileversion, last
%         revised \filedate.}\\
% Cross references}
% \author{\Team}
% \date{\filedate}
% \maketitle
%
% \begin{documentation}
%
% \begin{function}{\xref_set_label:n}
%   \begin{syntax}
%     "\xref_set_label:n" \Arg{name}
%   \end{syntax}
%   Sets a label in the text. Note that this function does not do
%   anything else than setting the correct labels. In particular, it
%   does not try to fix any spacing around the write node; this is a
%   task for the \textsf{galley2} module.
% \end{function}
%
% \begin{function}{\xref_new:nn}
%   \begin{syntax}
%     "\xref_new:nn" \Arg{type} \Arg{value}
%   \end{syntax}
%   Defines a new cross reference type <type>. This defines the token
%   list variable |\l_xref_curr_|<type>|_tl| with default value
%   <value> which gets written fully expanded when |\xref_set_label:n|
%   is called.
% \end{function}
%
%
% \begin{function}{\xref_deferred_new:nn}
%   \begin{syntax}
%     "\xref_deferred_new:nn" \Arg{type} \Arg{value}
%   \end{syntax}
%   Same as |\xref_new:n| except for this one, the value written
%   happens when \TeX\ ships out the page. Page numbers use this one
%   obviously.
% \end{function}
%
% \begin{function}{\xref_get_value:nn / (EXP)}
%   \begin{syntax}
%     "\xref_get_value:nn" \Arg{type} \Arg{name}
%   \end{syntax}
%   Extracts the cross reference information of type <type> for the
%   label <name>. This operation is expandable.
% \end{function}
%
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3xref} implementation}
%
% \subsection{Internal functions and variables}
%
% \begin{variabledoc}{ \g_xref_all_curr_immediate_fields_prop |
%                   \g_xref_all_curr_deferred_fields_prop  }
% What they say they are ":)"
% \end{variabledoc}
%
% \begin{variabledoc}{ \xref_write  }
% A stream for writing cross references, although they are not required
% to be in a separate file.
% \end{variabledoc}
%
% \begin{function}{\xref_define_label:nn}
%   \begin{syntax}
%     "\xref_define_label:nn" \Arg{name} \Arg{plist contents}
%   \end{syntax}
% Define the property list for each label; used internally by 
% "\xref_set_label:n".
% \end{function}
%
% \subsection{Module code}
%
%    We start by ensuring that the required packages are loaded.
%    \begin{macrocode}
%<*package>
\ProvidesExplPackage
  {\filename}{\filedate}{\fileversion}{\filedescription}
\package_check_loaded_expl:
%</package>
%<*initex|package>
%    \end{macrocode}
%
% There are two kinds of information, namely information which is
% \emph{immediate} like a section title and then there's \emph{deferred}
% information like page numbers. Each reference type belong to one of
% these categories, which we save internally as the property lists
% |\g_xref_all_curr_immediate_fields_prop| and
% |\g_xref_all_curr_deferred_fields_prop| and the reference type
% \meta{xyz} exists as the key-info pair |\xref_|\meta{xyz}|_key|
% |{\l_xref_curr_|\meta{xyz}|_tl}| on one of these lists. This way each
% new entry type is just added as another key-info pair. 
%
% When the cross references are generated at the beginning of the
% document each will turn into a control sequence. Thus |\label{mylab}|
% will internally refer to the property list |\g_xref_mylab_prop|.
%
% The extraction of values from this property list can be done in
% several different ways but we want to keep the operation
% expandable. Therefore we use a dedicated function for each type of
% cross reference, which looks like this:
% \begin{verbatim}
% \xref_get_value_xyz_aux:w -> #1 \xref_xyz_key #2#3\q_nil{#2}
% \end{verbatim}
% This will throw away all the bits we don't need. In case |xyz| is
% the first on the |mylab| property list |#1| is empty, if it's the
% last key-info pair |#3| is empty. The value of the field can be
% extracted with the function |\xref_get_value:nn| where the first
% argument is the type and the second the label name so here it would
% be |\xref_get_value:nn| |{xyz}| |{mylab}|.
%
%  \begin{macro}{\g_xref_all_curr_immediate_fields_prop}
%  \begin{macro}{\g_xref_all_curr_deferred_fields_prop}
%    The two main property lists for storing information. They contain
%    key-info pairs for all known types.
%    \begin{macrocode}
\prop_new:N \g_xref_all_curr_immediate_fields_prop
\prop_new:N \g_xref_all_curr_deferred_fields_prop
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\xref_new:nn}
%  \begin{macro}{\xref_deferred_new:nn}
%  \begin{macro}[aux]{\xref_new_aux:nnn}
%  Setting up a new cross reference type is fairly straight forward
%  when we follow the game plan mentioned earlier.
%    \begin{macrocode}
\cs_new_nopar:Npn \xref_new:nn {\xref_new_aux:nnn{immediate}}
\cs_new_nopar:Npn \xref_deferred_new:nn {\xref_new_aux:nnn{deferred}}
\cs_new_nopar:Npn \xref_new_aux:nnn #1#2#3{
%    \end{macrocode}
%  First put the new type in the relevant property list.
%    \begin{macrocode}
  \prop_gput:ccx {g_xref_all_curr_ #1 _fields_prop}
  { xref_ #2 _key }
  { \exp_not:c {l_xref_curr_#2_tl }}
%    \end{macrocode}
% Then define the key to be a protected macro.\footnote{We could also
%   set it equal to \cs{scan_stop:} but this just feels ``cleaner''.}
%    \begin{macrocode}
  \cs_set_protected_nopar:cpn { xref_#2_key }{}
  \tl_new:cn{l_xref_curr_#2_tl}{#3}
%    \end{macrocode}
% Now for the function extracting the value of a reference. We could
% do this with a simple |\prop_if_in| thing put since we want to do
% things in an expandable way we make a separate grabber for each
% type---this is also faster. The grabber function can be defined by
% using an intricate construction of |\exp_after:wN| and other goodies
% but I prefer readable code. The end result for the input |xyz| is
% \begin{verbatim}
% \cs_set_nopar:Npn\xref_get_value_xyz_aux:w #1\xref_xyz_key #2#3\q_nil{#2}
% \end{verbatim}
%    \begin{macrocode}
  \toks_set:Nx \l_tmpa_toks {
    \exp_not:n { \cs_set_nopar:cpn {xref_get_value_#2_aux:w} ##1 }
    \exp_not:N \q_prop
    \exp_not:c { xref_#2_key }
    \exp_not:N \q_prop
  }
  \toks_use:N \l_tmpa_toks ##2 ##3\q_nil {##2}
}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%  \end{macro}
%
%
% \begin{macro}{\xref_get_value:nn}
%   Getting the correct value for a given label-type pair is a matter
%   of connecting the correct grabber functions and property list.
%    \begin{macrocode}
\cs_new_nopar:Npn \xref_get_value:nn #1#2 {
  \cs_if_exist:cTF{g_xref_#2_prop}
  {
%    \end{macrocode}
% This next expansion may look a little weird but it isn't if you
% think about it!
%    \begin{macrocode}
    \exp_args:NcNc \exp_after:wN {xref_get_value_#1_aux:w}
    \toks_use:N {g_xref_#2_prop}
%    \end{macrocode}
%  Better put in the stop marker.
%    \begin{macrocode}
    \q_nil
  }
  {??}
}
%    \end{macrocode}
%  Temporary! We expand the property list and so we can't have the
%  |\q_prop| marker just expand!
%    \begin{macrocode}
\cs_set_nopar:Npn \exp_after:cc #1#2 {
  \exp_after:wN \exp_after:wN
  \cs:w #1\exp_after:wN\cs_end: \cs:w #2\cs_end:
}
\cs_set_protected:Npn \q_prop {\q_prop}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\xref_define_label:nn}
%  \begin{macro}[aux]{\xref_define_label_aux:nn}
%    Define the property list for each label. We better do this in two
%    steps because the special catcode regime is in effect and since
%    some of the info fields are very likely to contain actual text,
%    we better make sure spaces aren't ignored! As for the meaning of
%    other characters then it is a possibility to also have a field
%    containing catcode instructions which can then be activated with
%    |\etex_scantokens:D|. 
%    \begin{macrocode}
\cs_new_protected_nopar:Npn \xref_define_label:nn {
  \group_begin:
    \char_set_catcode:nn {`\ }\c_ten
    \xref_define_label_aux:nn
}
%    \end{macrocode}
% If the label is already taken we have a multiply defined label and
% we should do something about it. For now we don't do anything
% spectacular.
%    \begin{macrocode}
\cs_new_nopar:Npn \xref_define_label_aux:nn #1#2 {
    \cs_if_free:cTF{g_xref_#1_prop}
    {\prop_new:c{g_xref_#1_prop}}{\WARNING}
    \toks_gset:cn{g_xref_#1_prop}{#2}
  \group_end:
}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%
% \begin{macro}{\xref_set_label:n}
%   Then the generic command for setting a label. We expand the
%   immediate labels fully before calling the write function but make
%   sure the deferred fields aren't expanded just yet. Due to property
%   lists being implemented as token list registers we must expand the
%   `immediate' fields twice.
%    \begin{macrocode}
\cs_set_nopar:Npn \xref_set_label:n #1{
  \cs_set_nopar:Npx \xref_tmp:w{\toks_use:N\g_xref_all_curr_immediate_fields_prop}
  \exp_args:NNx\iow_shipout_x:Nn \xref_write{
    \xref_define_label:nn {#1} { 
      \xref_tmp:w 
      \toks_use:N \g_xref_all_curr_deferred_fields_prop
    }
  }
}
%    \end{macrocode}
%  \end{macro}
%
%  That's it (for now).
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
% 
%    \begin{macrocode}
%<*showmemory>
\showMemUsage
%</showmemory>
%    \end{macrocode}
%
% \section{\pkg{l3xref} test file}
%
%    \begin{macrocode}
%<*testfile>
\documentclass{article}
\usepackage{l3xref}
\ExplSyntaxOn
\cs_set_nopar:Npn \startrecording {\iow_open:Nn \xref_write {\jobname.xref}}
\cs_set_nopar:Npn \DefineCrossReferences {
  \group_begin:
    \ExplSyntaxNamesOn
    \InputIfFileExists{\jobname.xref}{}{}
  \group_end:
}
\AtBeginDocument{\DefineCrossReferences\startrecording}

\xref_new:nn {name}{}
\cs_set_nopar:Npn \setname{\tl_set:Nn\l_xref_curr_name_tl}
\cs_set_nopar:Npn \getname{\xref_get_value:nn{name}}

\xref_deferred_new:nn {page}{\thepage}
\cs_set_nopar:Npn \getpage{\xref_get_value:nn{page}}

\xref_deferred_new:nn {valuepage}{\number\value{page}}
\cs_set_nopar:Npn \getvaluepage{\xref_get_value:nn{valuepage}}

\cs_set_eq:NN \setlabel \xref_set_label:n

\ExplSyntaxOff
\begin{document}
\pagenumbering{roman}

Text\setname{This is a name}\setlabel{testlabel1}.  More
text\setname{This is another name}\setlabel{testlabel2}. \clearpage

Text\setname{This is a third name}\setlabel{testlabel3}. More
text\setname{Hello World!}\setlabel{testlabel4}. \clearpage

\pagenumbering{arabic}

Text\setname{Name 5}\setlabel{testlabel5}. More text\setname{Name
  6}\setlabel{testlabel6}. \clearpage

Text\setname{Name 7}\setlabel{testlabel 7}. More text\setname{Name
  8}\setlabel{testlabel8}. \clearpage

Now let's extract some values. \getname{testlabel1} on page
\getpage{testlabel1} with value \getvaluepage{testlabel1}.

Now let's extract some values. \getname{testlabel 7} on page
\getpage{testlabel 7} with value \getvaluepage{testlabel 7}.
\end{document}
%</testfile>
%    \end{macrocode}
%
% \end{implementation}
% \PrintIndex
%
% \endinput