Debian Codegraph @

texmf-dist/source/latex/knowledge/knowledge-code.dtx

raw
1 % \iffalse
2 % !TEX encoding = UTF-8 Unicode
3 % !TEX TS-program = pdflatex
4 %
5 %
6 %
7 % meta-comment
8 %
9 % Copyright (C) 2017-2025 by Thomas Colcombet
10 % -----------------------------------
11 %
12 % This file may be distributed and/or modified under the
13 % conditions of the LaTeX Project Public License, either version 1.2
14 % of this license or (at your option) any later version.
15 % The latest version of this license is in:
16 %
17 % http://www.latex-project.org/lppl.txt
18 %
19 % and version 1.2 or later is part of all distributions of LaTeX
20 % version 1999/12/01 or later.
21 %
22 % \fi
23 %
24 % \iffalse
25 %<head>\usepackage{expl3}
26 %<head>\ProvidesExplPackage{knowledge}{2026/04/02}{1.33}{}
27 %<*driver>
28 \def\knowledgecode{active}
29 \input{knowledge.tex}
30 %</driver>
31 % \fi
32 %
33 % \CheckSum{0}
34 %
35 %
36 % \changes{1.01}{2017/06/11}{Initial version} %
37 % \changes{1.02}{2017/06/28}{bug fixes} %
38 % \changes{1.03}{2017/07/27}{scoping,bug fixes} %
39 % \changes{1.04}{2017/07/28}{minor changes,bug fixes} %
40 % \changes{1.05}{2017/09/26}{minor changes,bug fixes} %
41 % \changes{1.06}{2017/10/10}{bug correction} %
42 % \changes{1.07}{2017/10/15}{minor improvements} %
43 % \changes{1.08}{2017/10/17}{minor changes and reorganization of the files} %
44 % \changes{1.09}{2017/10/31}{minor changes} %
45 % \changes{1.10}{2018/02/17}{bug fixes} %
46 % \changes{1.11}{2018/05/17}{bug fixes} %
47 % \changes{1.12}{2018/07/26}{bug fixes} %
48 % \changes{1.13}{2018/11/21}{bug fixes} %
49 % \changes{1.14}{2018/11/22}{bug fix} %
50 % \changes{1.15}{2019/01/23}{doc change} %
51 % \changes{1.16}{2019/02/15}{correct anchor point locations} %
52 % \changes{1.17}{2019/10/03}{improvements} %
53 % \changes{1.18}{2019/10/03}{bug fix/improvement} %
54 % \changes{1.19}{2019/11/19}{bug fix} %
55 % \changes{1.20}{2019/12/03}{bug fix} %
56 % \changes{1.21}{2020/01/26}{bug fix} %
57 % \changes{1.22}{2020/03/05}{improvements} %
58 % \changes{1.23}{2020/04/25}{minor change} %
59 % \changes{1.24}{2020/09/22}{silent option} %
60 % \changes{1.25}{2021/03/31}{bug correction} %
61 % \changes{1.26}{2021/12/20}{enhanced math+other functionalities+extra options} %
62 % \changes{1.27}{2022/01/12}{doc changes} %
63 % \changes{1.28}{2022/02/12}{imakeidx support} %
64 % \changes{1.29}{2024/03/10}{typographic bug corrected} %
65 % \changes{1.30}{2025/02/20}{bug correction} %
66 % \changes{1.31}{2025/05/09}{bug correction} %
67 % \changes{1.32}{2025/06/26}{new features} %
68 % \changes{1.33}{2026/04/02}{bug correction} %
69 % \GetFileInfo{knowledge.sty} %
70 % \DoNotIndex{}
71 %
72 %
73 %\let\ifcode\iffalse
74 %
75 %%%%%%%%%%
76 %
77 % \section{Implementation}
78 % \label{section:code}
79 %
80 % \subsection{README.md}
81 %
82 %<*readme>
83 % \begin{macrocode}
84 This directory contains the package
85
86 name: knowledge
87 license: LaTeX Project Public License version 1.2 or above
88 version: v1.33
89 date: 2026/04/02
90 author: Thomas Colcombet
91 mail: [email protected]
92 web: https://ctan.org/pkg/knowledge
93
94 ### Purpose:
95 The knowledge package for LaTeX offers a systematic way to handle
96 notions/concepts/terms throughout a document. In combination with
97 `hyperref`, it is used for easily handling links inside a document (every
98 use of a concept is linked to its introduction), or outside the document
99 (for centralizing the url's in a separate part). It also helps building an
100 index, and deals with scoping of concepts as well as mathematics.
101 It is designed to--as much as possible--not interfere with the writing of
102 the document.
103
104 ### Install:
105 It is sufficient to have the file knowledge.sty accessible by LaTeX.
106 It can be produced by `make knowledge.sty` if necessary.
107 The documentation is in the file `knowledge.pdf`.
108 A starting example can be found in `knowledge-example.tex`.
109
110 ### Content of knowledge-sources.zip:
111 - README.md: this file generated while compiling knowledge.ins,
112 the content is in knowledge-code.dtx
113 - knowledge.sty: the package file (generated using knowledge.ins)
114 - knowledge.pdf: the user documentation (generated by compiling
115 knowledge.tex)
116 - knowledge.tex: the tex file producing the knowledge.pdf.
117 - makefile: the makefile. Use 'make all' to generate knowledge.sty
118 and knowledge.pdf. It can also: clean the directory, make zip
119 version of the sources, or ready for CTAN.
120 - knowledge.ins: is the file generating knowledge.sty and
121 README.md from knowledge-code.dtx (using docstrip).
122 - knowledge-code.dtx: most of the code. Can be compiled using
123 LaTeX to provide a longer documentation with code.
124 - knowledge-configuration.dtx: file containing part of the configuration.
125 - knowledge-utils.dtx: file containing several pieces of code not really
126 related specifically to knowledge
127 - knowledge-example.tex: an example file.
128
129 % \end{macrocode}
130 %</readme>
131 % \subsection{Code preparation}
132 %<*head>
133 % \begin{macrocode}
134 \RequirePackage{l3keys2e}
135 \RequirePackage{etoolbox}
136 \RequirePackage{xparse}
137 \bool_if_exist:NTF\knowledge_package_loaded_bool
138 {\endinput}
139 {\bool_new:N\knowledge_package_loaded_bool
140 \bool_set_true:N\knowledge_package_loaded_bool}
141 % \end{macrocode}
142 % \begin{macrocode}
143 % \end{macrocode}
144 %</head>
145 %\begin{center}
146 % The content of \texttt{knowledge-utils.sty} is inserted here.\\
147 %\end{center}
148 %<*package>\\
149 % Macros to skip pieces of code depending on some bool. This can certainly be done differently.
150 % \begin{macrocode}
151 \cs_new:Nn\knowledge_begin_module_if:n
152 {\bool_if:NF#1\knowledge_skip_module:}
153 \cs_new:Npn\knowledge_skip_module:#1\knowledge_end_module:
154 {}
155 \cs_new:Nn\knowledge_end_module:
156 {}
157 % \end{macrocode}
158 %
159 %
160 %\AP One defines the code of \csintro{\klparse_ifnextchar:NTF}
161 % which is a link to \cs{\@ifnextchar},
162 % as well as the constant \csintro{\klparse_novalue_tl} which contains
163 % the token list corresponding to a non-given optional argument
164 % as used in the \kl(package){xparse} package.
165 %
166 % \begin{macrocode}
167 \cs_set_eq:Nc\klparse_ifnextchar:NTF{@ifnextchar}
168 % \end{macrocode}
169 % \begin{macrocode}
170 \tl_new:N\klparse_novalue_tl
171 \NewDocumentCommand\klparse_setnovalue{o}
172 {\tl_set:Nn\klparse_novalue_tl{#1}}
173 \klparse_setnovalue\relax
174 % \end{macrocode}
175 % \begin{macrocode}
176 \cs_new:Npn\klparse_if_novalue:NTF
177 {\tl_if_eq:NNTF\klparse_novalue_tl}
178 % \end{macrocode}
179 % \begin{macrocode}
180 \cs_generate_variant:Nn\tl_to_str:n{V}
181 % \end{macrocode}
182 %
183 % \begin{macrocode}
184 \tl_new:N\char_escape_tl
185 \tl_set:Nx\char_escape_tl
186 {\expandafter\tl_head:n\expandafter{\string\escape}}
187 % \end{macrocode}
188 %
189 % \begin{macrocode}
190 \cs_new:Nn\debug_vars:nn{
191 \iow_term:n{---~{#1}}
192 \tl_map_inline:nn{#2}
193 {\iow_term:x{\space\space\exp_not:N##1=\space\tl_if_exist:NTF##1
194 {{\exp_not:V##1}}
195 {undefined}}}
196 }
197 % \end{macrocode}
198 % \AP One also defines \csintro{\knowledge_invisible_begin:} and \csintro{\knowledge_invisible_end:}
199 % as shorthands for the standard \cs{\@bsphack} and \cs{\@esphack} macros.
200 % \begin{macrocode}
201 \makeatletter
202 \cs_new_eq:NN\knowledge_invisible_begin:\@bsphack
203 \cs_new_eq:NN\knowledge_invisible_end:\@esphack
204 \makeatother
205 % \end{macrocode}
206 %
207 % \subsection{Messages of the package}
208 %
209 %\AP First, some generic messages that are produced using \csintro{\knowledge_info:n},
210 % \csintro{\knowledge_warning:n} and \csintro{\knowledge_error:n}
211 % as well as the argument expending versions.
212 % These are mainly for development purposes.
213 % \begin{macrocode}
214 \cs_new:Nn\knowledge_info:n
215 {\msg_info:nnn{knowledge}{knowledge~info}{#1}}
216 \cs_new:Nn\knowledge_warning:n{
217 \msg_warning:nnn{knowledge}{knowledge~warning}{#1}
218 }
219 \cs_new:Nn\knowledge_error:n
220 {\msg_error:nnn{knowledge}{knowledge~error}{#1}}
221 % \end{macrocode}
222 % The command \csintro{\knowledge_werror:Nn} takes a boolean as argument, and behaves like \cs{\knowledge_error:n} if it is true, and \cs{\knowledge_warning:n} otherwise.
223 % \begin{macrocode}
224 \cs_new:Npn\knowledge_werror:Nn#1
225 {\bool_if:NTF#1\knowledge_error:n\knowledge_warning:n}
226 % \end{macrocode}
227 % \begin{macrocode}
228 \msg_new:nnnn{knowledge}{knowledge~info}{#1}{#2}
229 \msg_new:nnnn{knowledge}{knowledge~warning}{#1}{#2}
230 \msg_new:nnnn{knowledge}{knowledge~error}{#1}{#2}
231 % \end{macrocode}
232 % \begin{macrocode}
233 \cs_generate_variant:Nn\knowledge_info:n{x}
234 \cs_generate_variant:Nn\knowledge_warning:n{x}
235 \cs_generate_variant:Nn\knowledge_error:n{x}
236 % \end{macrocode}
237 %
238 % \begin{macrocode}
239 \cs_new:Npn\knowledge_warning:nn{
240 \bool_if:NTF\knowledge_warningsilent_bool
241 \use_none:nn
242 \msg_warning:nn
243 }
244 \cs_new:Npn\knowledge_warning:nnn{
245 \bool_if:NTF\knowledge_warningsilent_bool
246 \use_none:nnn
247 \msg_warning:nnn
248 }
249 \cs_new:Npn\knowledge_warning:nnnn{
250 \bool_if:NTF\knowledge_warningsilent_bool
251 \use_none:nnnn
252 \msg_warning:nnnn
253 }
254 \cs_generate_variant:Nn\knowledge_warning:nnnn{nnxx}
255 \cs_new:Npn\knowledge_warning:nnnnn{
256 \bool_if:NTF\knowledge_warningsilent_bool
257 \use_none:nnnnn
258 \msg_warning:nnnnn
259 }
260 \cs_generate_variant:Nn\knowledge_warning:nnnnn{nnxxx}
261 % \end{macrocode}
262 %
263 %
264 %\AP We provide now the `official' messages \csintro{knowledge~recursion}, \csintro{knowledge~unknown},
265 % \csintro{knowledge~unknown~in~scope}, \csintro{knowledge~unknown~label}, \csintro{package~required} and \csintro{knowledge~exists}.
266 % \begin{macrocode}
267 \msg_new:nnn{ knowledge }{ knowledge~recursion }
268 { The~knowledges~are~probably~cyclic,~starting~from\\<#1>~(namespace~'#2'),~
269 currently~at~'#3'.}
270 % \end{macrocode}
271 % \begin{macrocode}
272 \msg_new:nnn{ knowledge }{ knowledge~unknown }
273 {The~knowledge~'#1'~could~not~be~found~in~scopes\\<#3>~(namespace~'#2').}
274 % \end{macrocode}
275 % \begin{macrocode}
276 \msg_new:nnn{ knowledge }{ knowledge~unknown~in~scope }
277 {The~knowledge~'#1'~could~not~be~found~in~scope\\<#3>~(namespace~'#2').}
278 % \end{macrocode}
279 % \begin{macrocode}
280 \msg_new:nnn{ knowledge }{ knowledge~unknown~label }
281 {The~label~'#2'~could~not~be~found~while~searching~for:\\<#1>\\Possibly~rerun~latex.}
282 % \end{macrocode}
283 % \begin{macrocode}
284 \msg_new:nnn{ knowledge }{ package~required }
285 {In~order~to~use~directive~'#1',~please~load~package~or~use~option~'#2'.}
286 % \end{macrocode}
287 % \begin{macrocode}
288 \msg_new:nnn{ knowledge }{ knowledge~exists }
289 { The~knowledge~'#1'~in~scope~'#2'~(namespace~'#3')~already~exists. }
290 % \end{macrocode}
291 % \begin{macrocode}
292 \msg_new:nnn{ knowledge }{ knowledgedirective~redifinition }
293 { Directive~'#1'~already~exists~(\knowledgedirective~macro). }
294 % \end{macrocode}
295 %
296 %\AP The messages when there is a problem with scoping are \csintro{area~exists}, \csintro{area~not~exist},
297 % \csintro{empty~pop}, \csintro{no~pushing~here}, \csintro{no~popping},
298 % \csintro{no~popping~here}, \csintro{unknown~label} and \csintro{area~not~in~context}.
299 % \begin{macrocode}
300 \msg_new:nnn{scope}{area~exists}{Area~'#1'~already~exists.}
301 \msg_new:nnn{scope}{area~not~exist}{Area~'#1'~does~not~exist.}
302 \msg_new:nnn{scope}{empty~pop}{Attempt~to~pop~an~empty~area~stack.}
303 \msg_new:nnn{scope}{no~pushing~here}
304 {Not~allowed~to~open~'\scope_pushed_area_tl'~here.~
305 Current~stack~is~<\seq_use:Nn\scope_instances_stack_seq{::}>.}
306 \msg_new:nnn{scope}{no~popping}{Not~allowed~to~close~'\scope_popped_area_tl'.}
307 \msg_new:nnn{scope}{no~popping~here}
308 {Not~allowed~to~close~'\scope_popped_area_tl'~here~(at~'\scope_top_area_tl').}
309 \msg_new:nnn{scope}{unknown~label}
310 {\tl_if_empty:nTF{#2}{}{\exp_not:n{#2}}:
311 ~Unknown~label~`#1'.~Possibly~rerun~latex.}
312 \msg_new:nnn{scope}{area~not~in~context}
313 {\tl_if_empty:nTF{#2}{}{\exp_not:n{#2}}:
314 ~The~area~`#1'~can't~be~found~in~context.~Possibly~rerun~latex.}
315 % \end{macrocode}
316 %\AP We define new the final messages, that are thrown at the end of the compilation as a recap that some problems occured. These are: \csintro{recap~unknown~knowledge}, \csintro{recap~autoref~introduced~twice} and \csintro{recap~autoref~not~introduced}.
317 % \begin{macrocode}
318 \exp_args:Nnnx\msg_new:nnn{knowledge}{recap~unknown~knowledge}
319 {Some~knowledge~are~not~defined.~See~the~'\c_sys_jobname_str.diagnose'~file~for~more~information.}
320 \exp_args:Nnnx\msg_new:nnn{knowledge}{recap~autoref~introduced~twice}
321 {Some~knowledge~is~introduced~twice.~See~the~'\c_sys_jobname_str.diagnose'~file~for~more~information.}
322 \exp_args:Nnnx\msg_new:nnn{knowledge}{recap~autoref~not~introduced}
323 {Some~knowledge~is~used~but~not~introduced.~See~the~'\c_sys_jobname_str.diagnose'~file~for~more~information.}
324 % \end{macrocode}
325 %
326 %
327 % \subsection{Option and packages}
328 %
329 %
330 % \subsubsection{Configuration option}
331 %
332 % The options exist in two forms, the one that usable during the loading of the package,
333 % and the one that can be used in \cs{\knowledgeconfigure}, i.e., at some other locations in the document.
334 % The configuration options can be used when loading the package, however, these are first
335 % appended to the tl variable \csintro{\knowledge_configuration_pending_tl}, and executed at the end of the loading.
336 % This is important, since automatic configuration needs, for instance, to know if the \kl{composition mode}, or the \kl{paper mode} is used right now, but one cannot expect the user to declare the options in this particular order.
337 %
338 % \ToImprove{I do not know why, but \cs{\ProcessKeysOptions} removes the spaces in the parameters.
339 % So we have to do the same if we want to load options this way. This does not seem canonical.}
340 %
341 % We first provide a token list that stores the configurations (which are different from the options).
342 % \begin{macrocode}
343 \tl_clear_new:N\knowledge_configuration_pending_tl
344 % \end{macrocode}
345 %
346 %
347 % \paragraph{Configuration options}
348 % We provide here the code that is used to create new options for \cs{\knowledgeconfigure}.
349 % At the same time, it sets at package loading these options to be `posponed' (these will be executed)
350 % when the package loading is completed.
351 % \begin{macrocode}
352 \newrobustcmd\knowledgeconfigure[1]{
353 \keys_set:nn{ knowledge~configuration~options }{#1}
354 }
355 % \end{macrocode}
356 %
357 % \begin{quote}
358 % \csintro{\KnowledgeConfigureBooleanOption}[\cs{optional_bool}]\{option name\}\par
359 % \csintro{\KnowledgeConfigureBooleanOptionTF}[\cs{optional_bool}]\{option name\}\par
360 % ~~~~~~~~\{true code\}\{false code\}
361 % \end{quote}
362 % It takes an optional bool (of \kl(package){expl3}) and the name of the configuration option.
363 % The option is defined both in \cs{knowledge~package~options},
364 % and in \cs{knowledge~configuration~options}.
365 % In the former case, the option is simply appended to \cs{\knowledge_configuration_pending_tl} for
366 % being executed at the end of package loading.
367 % \begin{macrocode}
368 \NewDocumentCommand\KnowledgeConfigureBooleanOption{om}
369 {\KnowledgeConfigureBooleanOptionTF[#1]{#2}{}{}}
370 \NewDocumentCommand\KnowledgeConfigureBooleanOptionTF{ommm}{
371 \tl_set:Nn\l_tmpa_tl{#2}
372 \tl_remove_all:Nn\l_tmpa_tl{~}
373 \IfNoValueTF{#1}
374 {\bool_new:c{knowledge_configuration_#2_bool}
375 \exp_args:NcnV\__knowledge_KnowledgeConfigureBooleanOption:Nnnnn
376 {knowledge_configuration_#2_bool}{#2}\l_tmpa_tl{#3}{#4}}
377 {\exp_args:NNnV\__knowledge_KnowledgeConfigureBooleanOption:Nnnnn
378 #1{#2}\l_tmpa_tl{#3}{#4}}
379 }
380 \cs_new:Nn\__knowledge_KnowledgeConfigureBooleanOption:Nnnnn{
381 \keys_define:nn{ knowledge~package~options }{
382 #3 .choice:,
383 #3 / true .code:n = {\tl_put_right:Nn\knowledge_configuration_pending_tl{,#2=true}},
384 #3 / false .code:n = {\tl_put_right:Nn\knowledge_configuration_pending_tl{,#2=false}},
385 #3 .default:n = {true}
386 }
387 \keys_define:nn{ knowledge~configuration~options }{
388 #2 .choice:,
389 #2 / true .code:n = {\bool_if:NF#1{\bool_set_true:N#1#4}},
390 #2 / false .code:n = {\bool_if:NT#1{\bool_set_false:N#1#5}},
391 #2 .default:n = {true}
392 }}
393 % \end{macrocode}
394 %\begin{quote}\tt
395 % \csintro{\KnowledgeConfigureTrigger}[parameter]\{option name\}\{code\}
396 %\end{quote}
397 % triggers the code when the corresponding configuration option is required.
398 % If no optional parameter is given, the trigger name is used instead (with spaces removed).
399 % \begin{macrocode}
400 \NewDocumentCommand\KnowledgeConfigureTrigger{omm}{
401 \IfNoValueTF{#1}
402 {\tl_set:Nn\l_tmpa_tl{#2}
403 \tl_remove_all:Nn\l_tmpa_tl{~}
404 \exp_args:NNV\KnowledgeConfigureTrigger[{\l_tmpa_tl}]{#2}{#3}}
405 {\keys_define:nn { knowledge~configuration~options }
406 { #2 .code:n = {#3}, #2 .default:V = \klparse_novalue_tl }
407 \keys_define:nn { knowledge~package~options }
408 { #1 .code:n = {\tl_put_right:Nn\knowledge_configuration_pending_tl{,#2={##1}} }}
409 }
410 }
411 % \end{macrocode}
412 %
413 %
414 %
415 % \paragraph{Package loading options}
416 %
417 % \begin{quote}\tt
418 % \csintro{\KnowledgePackageTrigger}\{package option\}\{code\}
419 % \end{quote}
420 % is similar to \cs{\KnowledgeConfigureTrigger} but only valid at package loading and not in \cs{\knowledgeconfigure}.
421 % \begin{macrocode}
422 \newrobustcmd\KnowledgePackageTrigger[2]
423 { \keys_define:nn{ knowledge~package~options }{ #1 .code:n = {#2} }
424 \keys_define:nn{ knowledge~configuration~options }{
425 #1 .code:n = {\knowledge_error:n
426 {The~configuration~directive~`#1'~can~only~be~used~at~package~loading.}}
427 }}
428 % \end{macrocode}
429 % \begin{quote}\tt
430 % \csintro{\KnowledgePackageBooleanOption}[\cs{optional_bool}]\{optionname\}
431 % \end{quote}
432 % defines a Boolean package option, the value of which being stored in \cs{optional_bool}.
433 % If no \cs{optional_bool} is given, then \cs{\knowledge_configuration_optioname_bool}
434 % is chosen.
435 % \begin{macrocode}
436 \NewDocumentCommand\KnowledgePackageBooleanOption{om}{
437 \tl_set:Nn\l_tmpa_tl{#2}
438 \tl_remove_all:Nn\l_tmpa_tl{~}
439 \IfNoValueTF{#1}
440 {\bool_new:c{knowledge_configuration_#2_bool}
441 \exp_args:NcnV\knowledge_KnowledgePackageBooleanOption:Nnn
442 {knowledge_configuration_#2_bool}{#2}\l_tmpa_tl}
443 {\exp_args:NNnV\knowledge_KnowledgePackageBooleanOption:Nnn
444 #1{#2}\l_tmpa_tl}
445 }
446 \cs_new:Nn\knowledge_KnowledgePackageBooleanOption:Nnn{
447 \keys_define:nn{ knowledge~package~options }{
448 #3 .choice:,
449 #3 / true .code:n = {\bool_set_true:N#1},
450 #3 / false .code:n = {\bool_set_false:N#1},
451 #3 .default:n = {true}
452 }
453 \keys_define:nn{ knowledge~configuration~options }{
454 #2 .code:n = {\knowledge_error:n
455 {The~configuration~directive~`#2'~can~only~be~used~at~package~loading.}}
456 }}
457 % \end{macrocode}
458 %
459 %
460 % \begin{macrocode}
461 \cs_new:Nn\knowledge_option_bool_new:nN{
462 \keys_define:nn{ knowledge~package~options } {
463 #1 .choice: ,
464 #1 / true .code:n = { \bool_gset_true:N#2 },
465 #1 / false .code:n = { \bool_gset_false:N#2 },
466 #1 .default:n = {true}
467 }
468 }
469 \cs_new:Nn\knowledge_option_bool_new:n
470 {\bool_new:c{knowledge_option_#1_bool}
471 \exp_args:Nnc\knowledge_option_bool_new:nN{#1}{knowledge_option_#1_bool}}
472 % \end{macrocode}
473 %
474 %
475 % \subsubsection{List of options that have to be prepared}
476 %
477 % Code for a single feature. It takes the name of the trigger followed by the code.
478 %
479 % \begin{macrocode}
480 \bool_new:N\knowledge_warningstrict_bool
481 \bool_set_false:N\knowledge_warningstrict_bool
482 \KnowledgeConfigureBooleanOption[\knowledge_warningstrict_bool]{strict}
483 % \end{macrocode}
484 % \begin{macrocode}
485 \bool_new:N\knowledge_warningsilent_bool
486 \bool_set_false:N\knowledge_warningsilent_bool
487 \KnowledgeConfigureBooleanOption[\knowledge_warningsilent_bool]{silent}
488 % \end{macrocode}
489 % \begin{macrocode}
490 \bool_new:N\knowledge_configuration_quotation_bool
491 \KnowledgeConfigureBooleanOptionTF[\knowledge_configuration_quotation_bool]{quotation}{\klactivatequotation}{\kldeactivatequotation}
492 \KnowledgeConfigureTrigger{protect~quotation}{\kl_protect_environment_quotation:n{#1}}
493 % \end{macrocode}
494 %
495 % Activates or deactivates the line numbering in the "diagnose file".
496 % \begin{macrocode}
497 \bool_new:N\knowledge_configuration_diagnoseline_bool
498 \bool_set_false:N\knowledge_configuration_diagnoseline_bool
499 \KnowledgeConfigureBooleanOption[\knowledge_configuration_diagnoseline_bool]{diagnose~line}
500 % \end{macrocode}
501 % Activates or deactivates the help in the "diagnose file".
502 % \begin{macrocode}
503 \bool_new:N\knowledge_configuration_diagnosehelp_bool
504 \bool_set_true:N\knowledge_configuration_diagnosehelp_bool
505 \KnowledgeConfigureBooleanOption[\knowledge_configuration_diagnosehelp_bool]{diagnose~help}
506 % \end{macrocode}
507 %
508 % \begin{macrocode}
509 \bool_new:N\knowledge_configuration_diagnosebar_bool
510 \bool_gset_true:N\knowledge_configuration_diagnosebar_bool
511 \KnowledgeConfigureBooleanOption[\knowledge_configuration_diagnosebar_bool]{bar~suggestion}%backward compatibility
512 \KnowledgeConfigureBooleanOption[\knowledge_configuration_diagnosebar_bool]{diagnose~bar}
513 % \end{macrocode}
514 %
515 % \begin{macrocode}
516 \bool_new:N\knowledge_configuration_visibleAP_bool
517 \KnowledgeConfigureBooleanOption[\knowledge_configuration_visibleAP_bool]{visible~anchor~points}\KnowledgeConfigureBooleanOption[\knowledge_configuration_visibleAP_bool]{visible~AP}
518 % \end{macrocode}
519 %
520 % \begin{macrocode}
521 \bool_new:N\knowledge_hyperlinks_active_bool
522 \bool_set_true:N\knowledge_hyperlinks_active_bool
523 % \end{macrocode}
524 %
525 % Modes are stored in \csintro{\knowledge_option_mode_tl}.
526 % \begin{macrocode}
527 \tl_clear_new:N\knowledge_option_mode_tl
528 \KnowledgePackageTrigger{ paper }{\tl_set:Nn\knowledge_option_mode_tl{paper}}
529 \KnowledgePackageTrigger{ electronic }{\tl_set:Nn\knowledge_option_mode_tl{electronic}}
530 \KnowledgePackageTrigger{ composition }{\tl_set:Nn\knowledge_option_mode_tl{composition}}
531 % this one is for compatibility
532 \KnowledgePackageTrigger{ final }{\tl_set:Nn\knowledge_option_mode_tl{final}}
533 % \end{macrocode}
534 % Modes are stored in \csintro{\knowledge_option_mode_tl}.
535 % \begin{macrocode}
536 \bool_new:N\knowledge_option_texpatch_bool
537 \bool_set_true:N\knowledge_option_texpatch_bool
538 \KnowledgePackageTrigger{ no patch }{\bool_set_false:N\knowledge_option_texpatch_bool}
539 % \end{macrocode}
540 % \begin{macrocode}
541 \tl_new:N\knowledge_option_knowledgeAP_command_tl
542 \tl_set:Nn\knowledge_option_knowledgeAP_command_tl{}
543 \tl_new:N\knowledge_option_complexityAP_command_tl
544 \tl_set:Nn\knowledge_option_complexityAP_command_tl{}
545 \KnowledgePackageTrigger{ knowledge AP }{
546 \tl_if_single_token:nTF{#1}
547 {\tl_set:Nn\knowledge_option_knowledgeAP_command_tl{#1}}
548 {\knowledge_error:n{Package~option~'knowledge~AP'~must~be~followed~by~a~single~control~sequence.}}
549 }
550 \KnowledgePackageTrigger{ complexity AP }{
551 \tl_if_single_token:nTF{#1}
552 {\tl_set:Nn\knowledge_option_complexityAP_command_tl{#1}}
553 {\knowledge_error:n{Package~option~'complexity~AP'~must~be~followed~by~a~single~control~sequence.}}
554 }
555 % \end{macrocode}
556 %
557 %
558 % \begin{macrocode}
559 \tl_new:N\knowledge_option_anchor_point_color_tl
560 \tl_set:Nn\knowledge_option_anchor_point_color_tl{red}
561 \KnowledgeConfigureTrigger{anchor~point~color}
562 {\tl_set:Nn\knowledge_option_anchor_point_color_tl{#1}}
563 \KnowledgeConfigureTrigger{AP~color}
564 {\tl_set:Nn\knowledge_option_anchor_point_color_tl{#1}}
565 \tl_new:N\knowledge_option_anchor_point_shift_tl
566 \tl_set:Nn\knowledge_option_anchor_point_shift_tl{(0,0)}
567 \KnowledgeConfigureTrigger{anchor~point~shift}
568 {\tl_set:Nn\knowledge_option_anchor_point_shift_tl{(#1)}}
569 \KnowledgeConfigureTrigger{AP~shift}
570 {\tl_set:Nn\knowledge_option_anchor_point_shift_tl{(#1)}}
571
572 \cs_new:Nn\knowledge_anchor_point_corner:n{\line(0,-1){#1}\line(1,0){#1}}
573 \cs_new:Nn\knowledge_anchor_point_cross:n{
574 \put(0,0){\line(0,-1){#1}}\put(0,0){\line(0,1){#1}}\put(0,0){\line(-1,0){#1}}\put(0,0){\line(1,0){#1}}}
575 \tl_new:N\knowledge_option_anchor_point_shape_tl
576 \tl_set:Nn\knowledge_option_anchor_point_shape_tl{\knowledge_anchor_point_corner:n{0.2}}
577
578 \tl_const:Nn\knowledge_APshape_name_corner_tl{corner}
579 \tl_const:Nn\knowledge_APshape_name_tinycorner_tl{tiny~corner}
580 \tl_const:Nn\knowledge_APshape_name_smallcorner_tl{small~corner}
581 \tl_const:Nn\knowledge_APshape_name_largecorner_tl{large~corner}
582 \tl_const:Nn\knowledge_APshape_name_cross_tl{cross}
583 \tl_const:Nn\knowledge_APshape_name_tinycross_tl{tiny~cross}
584 \tl_const:Nn\knowledge_APshape_name_smallcross_tl{small~cross}
585 \tl_const:Nn\knowledge_APshape_name_largecross_tl{large~cross}
586
587 \KnowledgeConfigureTrigger{AP~shape}
588 {\knowledge_set_anchor_point_shape:n{#1}}
589 \KnowledgeConfigureTrigger{anchor~point~shape}
590 {\knowledge_set_anchor_point_shape:n{#1}}
591 \cs_new:Nn \knowledge_set_anchor_point_shape:n{
592 \tl_set:Nn\l_tmpa_tl{#1}
593 \tl_case:NnTF\l_tmpa_tl{
594 \knowledge_APshape_name_corner_tl
595 {\tl_set:Nn\knowledge_option_anchor_point_shape_tl{\knowledge_anchor_point_corner:n{0.5}}}
596 \knowledge_APshape_name_tinycorner_tl
597 {\tl_set:Nn\knowledge_option_anchor_point_shape_tl{\knowledge_anchor_point_corner:n{0.1}}}
598 \knowledge_APshape_name_smallcorner_tl
599 {\tl_set:Nn\knowledge_option_anchor_point_shape_tl{\knowledge_anchor_point_corner:n{0.2}}}
600 \knowledge_APshape_name_largecorner_tl
601 {\tl_set:Nn\knowledge_option_anchor_point_shape_tl{\knowledge_anchor_point_corner:n{0.8}}}
602 \knowledge_APshape_name_cross_tl
603 {\tl_set:Nn\knowledge_option_anchor_point_shape_tl{\knowledge_anchor_point_cross:n{0.5}}}
604 \knowledge_APshape_name_tinycross_tl
605 {\tl_set:Nn\knowledge_option_anchor_point_shape_tl{\knowledge_anchor_point_cross:n{0.1}}}
606 \knowledge_APshape_name_smallcross_tl
607 {\tl_set:Nn\knowledge_option_anchor_point_shape_tl{\knowledge_anchor_point_cross:n{0.2}}}
608 \knowledge_APshape_name_largecross_tl
609 {\tl_set:Nn\knowledge_option_anchor_point_shape_tl{\knowledge_anchor_point_cross:n{0.8}}}
610 }{}{
611 \tl_set:Nn\knowledge_option_anchor_point_shape_tl{#1}
612 }
613 }
614 % \end{macrocode}
615 %
616 %
617 % \begin{macrocode}
618 \KnowledgeConfigureTrigger{ fix~hyperref~twocolumn }
619 {\knowledgeFixHyperrefTwocolumn}
620 \KnowledgeConfigureTrigger{ notion }
621 {
622 \knowledge_directives_require_aci_package:nn{notion}{hyperref}
623 \IfNoValueTF{#1}
624 {
625 \KnowledgeConfigureNotion[blue]{notion}
626 \knowledgesetvariant\kl{suggestion=notion}
627 \knowledgesetvariant\intro{suggestion=notion}
628 \knowledgesetvariant\phantomintro{suggestion=notion}
629 \knowledgesetvariant\reintro{suggestion=notion}
630 }
631 {
632 \KnowledgeConfigureNotion[blue]{#1}
633 }
634 }
635 \KnowledgeConfigureTrigger{ protect~links }
636 {\kl_kl_nesting_incr:}
637 \KnowledgeConfigureTrigger{ unprotect~links }
638 {\kl_kl_nesting_decr:}
639 % \end{macrocode}
640 %
641 %
642 %
643 % \subsubsection{External package options}
644 %
645 %\csintro{\knowledge_cleveref_active_bool},
646 %\csintro{\knowledge_cleveref_syntax_bool},
647 %\csintro{\knowledge_hyperref_active_bool},
648 %\csintro{\knowledge_hyperref_syntax_bool},
649 %\csintro{\knowledge_makeidx_active_bool},
650 %\csintro{\knowledge_makeidx_syntax_bool},
651 %\csintro{\knowledge_imakeidx_active_bool},
652 %\csintro{\knowledge_imakeidx_syntax_bool}.
653 %\csintro{\knowledge_xcolor_active_bool},
654 % \csintro{\knowledge_xcolor_syntax_bool},
655 %
656 %
657 %
658 % First, one tests the package available.
659 % \csintro{\package_if_loaded:nTF} tests if a package is loaded.
660 % \csintro{\knowledge_option_aci_test_loaded:n} uses it for setting the default values of the booleans
661 % \begin{compactitem}
662 % \item \csintro{\knowledge_packagename_active_bool} that sets all the functionalities linked to the corresponding package to be active.
663 % \item \csintro{\knowledge_packagename_syntax_bool} that states that the syntax is allowed.
664 % \end{compactitem}
665 % \begin{macrocode}
666 \cs_new_eq:Nc\package_if_loaded:nTF{@ifpackageloaded}
667 \cs_new:Nn\knowledge_option_aci_test_loaded:n{
668 \bool_new:c{knowledge_#1_active_bool}
669 \bool_new:c{knowledge_#1_syntax_bool}
670 \package_if_loaded:nTF{#1}
671 {\bool_set_true:c{knowledge_#1_active_bool}
672 \bool_set_true:c{knowledge_#1_syntax_bool}}
673 {\bool_set_false:c{knowledge_#1_active_bool}
674 \bool_set_false:c{knowledge_#1_syntax_bool}}
675 }
676 \knowledge_option_aci_test_loaded:n{hyperref}
677 \knowledge_option_aci_test_loaded:n{xcolor}
678 \knowledge_option_aci_test_loaded:n{makeidx}
679 \knowledge_option_aci_test_loaded:n{imakeidx}
680 \knowledge_option_aci_test_loaded:n{cleveref}
681 % \end{macrocode}
682 %\begin{quote}\tt
683 %\csintro{\knowledge_option_aci_define:n}\{package name\}
684 %\end{quote}
685 % defines the different possible ways to use the package.
686 % \begin{macrocode}
687 \cs_new:Nn\knowledge_option_aci_define:n{
688 \keys_define:nn{ knowledge~package~options } {
689 #1 .choice: ,
690 #1 / active .code:n = {
691 \bool_set_true:c{knowledge_#1_active_bool}
692 \bool_set_true:c{knowledge_#1_syntax_bool}},
693 #1 / compatibility .code:n = {
694 \bool_set_false:c{knowledge_#1_active_bool}
695 \bool_set_true:c{knowledge_#1_syntax_bool}},
696 #1 / inactive .code:n = {
697 \bool_set_false:c{knowledge_#1_active_bool}
698 \bool_set_false:c{knowledge_#1_syntax_bool}},
699 #1 .default:n = {active}
700 }}
701 % \end{macrocode}
702 % \begin{quote}\tt
703 % \csintro{\knowledge_option_aci_autoload:nn}\{package name\}\{options for the package\}
704 % \end{quote}
705 % is called for each package after the options have been processed.
706 % It loads the package if required, possibly with options.
707 % \begin{macrocode}
708 \cs_new:Nn\knowledge_option_aci_autoload:nnn{
709 \bool_if:cTF{knowledge_#1_active_bool}
710 {\package_if_loaded:nTF{#1}{}
711 {\RequirePackage[#2]{#1}{#3}}}
712 {}
713 }
714 \cs_new:Nn\knowledge_option_aci_autoload:nn
715 {\knowledge_option_aci_autoload:nnn{#1}{#2}{}}
716 % \end{macrocode}
717 %
718 % \paragraph{The real options}
719 %
720 % \begin{macrocode}
721 \knowledge_option_bool_new:n{scope}
722 \knowledge_option_aci_define:n{hyperref}
723 \knowledge_option_aci_define:n{xcolor}
724 \knowledge_option_aci_define:n{makeidx}
725 \knowledge_option_aci_define:n{imakeidx}
726 \knowledge_option_aci_define:n{cleveref}
727 % \end{macrocode}
728 %
729 % \subsubsection{Executing the package options}
730 %
731 % \begin{macrocode}
732 \ProcessKeysOptions{knowledge~package~options}
733 % \end{macrocode}
734 %
735 % \subsubsection{Options post treatment for the "writing mode"}
736 %
737 % \begin{macrocode}
738
739 \bool_new:N\knowledge_option_composition_bool
740 \bool_set_false:N\knowledge_option_composition_bool
741 \bool_new:N\knowledge_option_electronic_bool
742 \bool_set_false:N\knowledge_option_electronic_bool
743 \bool_new:N\knowledge_option_paper_bool
744 \bool_set_false:N\knowledge_option_paper_bool
745 % \end{macrocode}
746 % Testing if the "writing mode" has been chosen by setting \cs{\knowledgepackagemode}.
747 % \begin{macrocode}
748 \tl_if_exist:NTF\knowledgepackagemode
749 {}
750 {\tl_new:N\knowledgepackagemode
751 \tl_set:Nn\knowledgepackagemode{}}
752 % \end{macrocode}
753 % \begin{macrocode}
754 \tl_if_empty:NTF\knowledgepackagemode
755 {\tl_if_empty:NTF\knowledge_option_mode_tl
756 {\tl_set:Nn\knowledge_option_mode_tl{composition}}
757 {}}
758 {\tl_if_empty:NTF\knowledge_option_mode_tl
759 {\tl_set_eq:NN\knowledge_option_mode_tl\knowledgepackagemode}
760 {\knowledge_info:n{Knowledge~mode~overriden~by~user.}
761 \tl_set_eq:NN\knowledge_option_mode_tl\knowledgepackagemode
762 }}
763 % \end{macrocode}
764 %
765 %
766 % \begin{macrocode}
767 \keys_define:nn{ knowledge~package~modes }{
768 paper .code:n = {
769 \bool_set_true:N\knowledge_option_paper_bool
770 \bool_set_false:N\knowledge_configuration_visibleAP_bool
771 },
772 electronic .code:n = {
773 \bool_set_true:N\knowledge_option_electronic_bool
774 \bool_set_false:N\knowledge_configuration_visibleAP_bool
775 },
776 composition .code:n = {
777 \bool_set_true:N\knowledge_option_composition_bool
778 \bool_set_true:N\knowledge_configuration_visibleAP_bool
779 },
780 final .code:n = { \knowledge_warning:x{Mode~`final`~is~deprecated,~
781 and~should~be~replaced~by~mode~`paper`.}
782 \tl_set:Nn\knowledge_option_mode_tl{paper}
783 \keys_set:nn{knowledge~package~modes}{paper}},
784 unknown .code:n =
785 {\knowledge_warning:x{Unknown~mode~`\l_keys_key_tl`.}
786 \tl_set:Nn\knowledge_option_mode_tl{ composition }
787 \keys_set:nV{ knowledge~package~modes }{ composition }}
788 }
789 \keys_set:nV{ knowledge~package~modes }\knowledge_option_mode_tl
790 % \end{macrocode}
791 %
792 %
793 % \subsubsection{Options post-treatment for the other packages}
794 % \begin{macrocode}
795 \knowledge_option_aci_autoload:nnn{ hyperref }{}{}
796 \knowledge_option_aci_autoload:nn{ xcolor }{}
797 \knowledge_option_aci_autoload:nn{ makeidx }{}
798 \knowledge_option_aci_autoload:nn{ imakeidx }{}
799 \knowledge_option_aci_autoload:nn{ cleveref }{}
800 % \end{macrocode}
801 %
802 %
803 %
804 %
805 % \subsection{Scope implementation}
806 %
807 % The two strings that are used as (hopefully) unique prefixes in the namespace.
808 % \begin{macrocode}
809 \tl_const:Nn\scope_tl{SC@PE}
810 \tl_const:Nn\scopept_tl{SC@PE2}
811 % \end{macrocode}
812 % \csintro{\scope_instances_stack_seq} contains the stack of instances of areas.
813 % \csintro{\scope_top_instance_tl} contains always the topmost instance of the stack.
814 % \csintro{\scope_top_area_tl} contains the corresponding \kl{area}.
815 % \begin{macrocode}
816 \seq_new:N\scope_instances_stack_seq
817 \tl_new:N\scope_top_instance_tl
818 \tl_new:N\scope_top_area_tl
819 % \end{macrocode}
820 %
821 %
822 % \subsubsection{Data structures}
823 %
824 % Several `data structures' are used internally by the package.
825 % \begin{description}
826 % \item[\kl{areas}] are zones structuring valid documents. These are typically
827 % `\texttt{document}', `\texttt{scope}', `\texttt{section}', `\texttt{subsection}', `\texttt{theorem}', `\texttt{itemize}', `\texttt{item}' and so on.
828 % \item[\kl{categories}] are collections of \kl{areas} that share some common behaviours, for instance, the \kl{category} `\texttt{itemize-like}' will aggregate
829 % `\texttt{itemize}', `\texttt{enumerate}' \kl{areas}. In particular an `\texttt{item}'
830 % \kl{area} is required to have a parent that belongs to the \kl{category} `\texttt{itemize-like}'.
831 % \item[\intro{instances}] are the realization of \kl{areas} in a document. Hence `section' is an \kl{area},
832 % but each section of a document is an \kl{instance}.
833 % \item[\intro{scopept instances}] stands for `phase two'. These are functions that are activated during
834 % the second compilation. (and also, under implementation, in the preamble)
835 % \item[\kl{knowledges}] are the central notion
836 % \end{description}
837 %
838 % \paragraph{Areas}
839 %
840 % An \intro{area} is an environment unit such as `\texttt{theorem}', `\texttt{section}', and so on\dots{}
841 % Special realization of these \kl{areas} are called `\kl{instances}'.
842 % Hence, there is an \kl{area} named `\texttt{section}', and there are potentially
843 % many \kl{instances} of this area in the document.
844 % Part of the machinery is used for the allocation of \kl{instances}.
845 % In some sense, this is a weak form of grammar describing how these can be used.
846 % For instance, a subsection has to occur below a section, and opening a section immediately
847 % closes the current subsection if it exists.
848 %
849 % To help the definition, the areas can be grouped into categories in order to share part of the code/definitions.
850 % Each time an \kl{area} is defined, a corresponding \kl{category} with same name is also defined.
851 %
852 % An \kl{area} has some fields. The field X of \kl{area} A
853 % can be found in the token of name |\scope_tl.area[A].X|.
854 % \begin{compactdesc}
855 % \item[\csintro{nesting_int}] is an int counting the nesting level of the area at the current moment.
856 % \item[\csintro{count_int}] is an int counting the instances of the area opened so far. It is used (for the moment)
857 % in order to uniquely name the instances. It is also used in some tests. It is incremented each time
858 % a new \kl{instance} of this area is created.
859 % \item[\csintro{name_tl}] is the text name of the \kl{area}.
860 % \item[\csintro{pushcode_tl}] is code to be executed when an \kl{instance} of the \kl{area} is created.
861 % \item[\csintro{popcode_tl}] is code to be executed just before an \kl{instance} of the \kl{area} is closed.
862 % \item[\csintro{autoclose_bool}] is a boolean which, when set to true,
863 % means that \kl{instances} of this area may be implicitly closed. This is the case for instance
864 % for a `subsection', which can be implicitly closed when another `subsection' is started, or when
865 % a `section' is started, or when the document finishes.
866 % \item[\csintro{recursive_bool}] is a boolean describing whether such an environment can be opened inside itself.
867 % This is more for making the error messages explicit.
868 % \item[\csintro{multiple_bool}] is a boolean that tells whether several instances of this area can be defined
869 % in the document. This is also mainly for making more explicit the error messages.
870 % \item[\csintro{forces_tl}] is the name of an area that has to be opened if one wants to use this area.
871 % It is opened if necessary.
872 % \item[\csintro{acceptsknowledge_bool}] is a boolean telling whether a knowledge can be associated to an instance of this area.
873 % \item[\csintro{labelizable_bool}] tells whether a label can point to such an environment.
874 % \item[\csintro{attractsknowledge_bool}] is a boolean that tells whether this area
875 % spontaneously catpures the \kl{knowledges} defined in its scope.
876 % Hence, a section does not attract knowledges: the document does.
877 % However, the user can force a knowledge to only be valid in a section since `section's accept knowledges.
878 % \item[\csintro{mandatorybase_seq}] is a \texttt{seq} containing the list of categories that are allowed to
879 % be the parent of this area. For instance, \cs{\item}
880 % would have \texttt{itemize}, \texttt{compactdesc} and \texttt{enumerate}.
881 % \end{compactdesc}
882 %
883 % \begin{macrocode}
884 \tl_new:N\l_self_tl
885 % \end{macrocode}
886 % \begin{macrocode}
887 \cs_new:Nn\scope_area_show:n{
888 \group_begin:
889 \scope_area_if_exist:nTF{#1}
890 { \tl_set:Nx\l_self_tl{\scope_tl.area[#1]}
891 \tl_show:x
892 {Area~name:~\use:c{\l_self_tl.name_tl},~
893 autoclose=\bool_if:cTF{\l_self_tl.autoclose_bool}TF,~
894 recursive=\bool_if:cTF{\l_self_tl.recursive_bool}TF,~
895 multiple=\bool_if:cTF{\l_self_tl.multiple_bool}TF,~
896 scope=\bool_if:cTF{\l_self_tl.scope_bool}TF,~
897 environment=\bool_if:cTF{\l_self_tl.environment_bool}TF,~
898 forces={\use:c{\l_self_tl.forces_tl}},~
899 accepts~knowledge=\bool_if:cTF{\l_self_tl.accepts_knowledge_bool}TF,~
900 attracts~knowledge=\bool_if:cTF{\l_self_tl.attracts_knowledge_bool}TF,~
901 accepts~labels=\bool_if:cTF{\l_self_tl.labelizable_bool}TF.
902 }
903 }
904 {\knowledge_error:n{Unknown~area~`#1'~in~`\noexpand\scope_area_show:n'.}}
905 \group_end:
906 }
907 % \end{macrocode}
908 %
909 %
910 %
911 % Areas are created empty by \cs{\scope_area_create:n}.
912 % \begin{macrocode}
913 \cs_new:Nn\scope_area_create:n
914 {\group_begin:
915 \tl_set:Nx\l_self_tl{\scope_tl.area[#1]}
916 %
917 \scope_category_add_area:nn{all}{#1}
918 %
919 \tl_new:c{\l_self_tl.name_tl}
920 \tl_gset:cn{\l_self_tl.name_tl}{#1}
921 \seq_new:c{\l_self_tl.category_seq}
922 \seq_gpush:cn{\l_self_tl.category_seq}{all}
923 \seq_gpush:cn{\l_self_tl.category_seq}{#1}
924 \bool_new:c{\l_self_tl.autoclose_bool}
925 \bool_gset_false:c{\l_self_tl.autoclose_bool}
926 \bool_new:c{\l_self_tl.environment_bool}
927 \bool_gset_true:c{\l_self_tl.environment_bool}
928 \bool_new:c{\l_self_tl.multiple_bool}
929 \bool_gset_true:c{\l_self_tl.multiple_bool}
930 \bool_new:c{\l_self_tl.attractsknowledge_bool}
931 \bool_gset_true:c{\l_self_tl.attractsknowledge_bool}
932 \bool_new:c{\l_self_tl.labelizable_bool}
933 \bool_gset_true:c{\l_self_tl.labelizable_bool}
934 \bool_new:c{\l_self_tl.scope_bool}
935 \bool_gset_false:c{\l_self_tl.scope_bool}
936 \bool_new:c{\l_self_tl.acceptsknowledge_bool}
937 \bool_gset_true:c{\l_self_tl.acceptsknowledge_bool}
938 \bool_new:c{\l_self_tl.recursive_bool}
939 \tl_gclear_new:c{\l_self_tl.pushcode_tl}
940 \tl_gclear_new:c{\l_self_tl.popcode_tl}
941 \tl_gclear_new:c{\l_self_tl.forces_tl}
942 \seq_new:c{\l_self_tl.mandatorybase_seq}
943 \int_new:c{\l_self_tl.count_int}
944 \int_gset:cn{\l_self_tl.count_int}{0}
945 \int_new:c{\l_self_tl.nesting_int}
946 \int_gset:cn{\l_self_tl.nesting_int}{0}
947 %
948 \scope_category_if_exist:nTF{#1}
949 {}{\scope_category_create:n{#1}
950 \seq_gpush:cn{\scope_tl.category[#1].areas_seq}{#1}}
951 \group_end:
952 }
953 % \end{macrocode}
954 %
955 %
956 % \cs{\scope_area_if_exist:nTF}, \cs{\scope_area_if_in_scope:nTF}.
957 % \begin{macrocode}
958 \cs_new:Npn\scope_area_if_exist:nTF#1
959 {\tl_if_exist:cTF{\scope_tl.area[#1].name_tl}}
960 % \end{macrocode}
961 % \begin{macrocode}
962 \cs_new:Npn\scope_if_triggers_area:nTF#1{
963 \scope_area_if_exist:nTF{#1}
964 {\bool_if:cTF{\scope_tl.area[#1].environment_bool}
965 \use_i:nn
966 \use_ii:nn}
967 \use_ii:nn
968 }
969 % \end{macrocode}
970 % \begin{macrocode}
971 \cs_new:Npn\scope_area_if_in_scope:nTF#1
972 {\int_compare:nNnTF
973 {\use:c{\scope_tl.area[#1].nesting_int}}>0}
974 % \end{macrocode}
975 %
976 % \AP The commands \cs{\scope_area_set:nn}
977 % introduces (if necessary) and changes the parameters of areas.
978 % The first argument is a comma separated list of area names, and the second
979 % argument is a comma separated list of key=val commands. The keys available by default are the following:
980 %
981 % \AP The base command for configuring a scope is \csintro{\ScopeConfigure}\{area~list\}\{directives\}.
982 % \begin{macrocode}
983 \newrobustcmd\ScopeConfigure[2]
984 {\scope_area_set:nn{#1}{#2}}
985 % \end{macrocode}
986 % \begin{macrocode}
987 \cs_new:Nn\scope_area_set:nn
988 {\clist_map_inline:nn{#1}
989 {\scope_area_set_i:nn{##1}{#2}}}
990 % \end{macrocode}
991 % \begin{macrocode}
992 \tl_new:N\scope_area_tl
993 \cs_new:Nn\scope_area_set_i:nn {
994 \scope_area_if_exist:nTF{#1}
995 {}{
996 \scope_area_create:n{#1}}
997 \group_begin:
998 \tl_set:Nx\scope_area_tl{#1}
999 \tl_set:Nx\l_self_tl{\scope_tl.area[\scope_area_tl]}
1000 \keys_set:nn{ scope~area~set }{#2}
1001 \group_end:}
1002 % \end{macrocode}
1003 % \begin{macrocode}
1004 % \scope_area_tl contains the name of the area under definition
1005 \keys_define:nn{ scope~area~set } {
1006 category .code:n =
1007 {\clist_map_inline:nn{#1}
1008 {\scope_category_ensure:n{##1}
1009 \seq_gpush:cV
1010 {\scope_tl.category[##1].areas_seq}\scope_area_tl
1011 }},
1012 autoclose .choice:,
1013 autoclose / false .code:n =
1014 {\bool_gset_false:c{\l_self_tl.autoclose_bool}},
1015 autoclose / true .code:n =
1016 {\bool_gset_true:c{\l_self_tl.autoclose_bool}},
1017 parents .code:n =
1018 {\seq_gset_from_clist:cn{\l_self_tl.mandatorybase_seq}{#1}},
1019 push~code .code:n =
1020 {\tl_gput_right:cn{\l_self_tl.pushcode_tl}{#1}},
1021 pop~code .code:n =
1022 {\tl_gput_right:cn{\l_self_tl.popcode_tl}{#1}},
1023 occurrences .choice:,
1024 occurrences / once .code:n =
1025 {\bool_gset_false:c{\l_self_tl.multiple_bool}
1026 \bool_gset_false:c{\l_self_tl.recursive_bool}},
1027 occurrences / multiple .code:n = {
1028 \bool_gset_true:c{\l_self_tl.multiple_bool}
1029 \bool_gset_false:c{\l_self_tl.recursive_bool}},
1030 occurrences / recursive .code:n = {
1031 \bool_gset_true:c{\l_self_tl.multiple_bool}
1032 \bool_gset_true:c{\l_self_tl.recursive_bool}},
1033 scope .choice:,
1034 scope / true .code:n = {
1035 \bool_gset_true:c{\l_self_tl.scope_bool}},
1036 scope / false .code:n = {
1037 \bool_gset_false:c{\l_self_tl.scope_bool}},
1038 environment .choice:,
1039 environment / true .code:n = {
1040 \bool_gset_true:c{\l_self_tl.environment_bool}},
1041 environment / false .code:n = {
1042 \bool_gset_false:c{\l_self_tl.environment_bool}},
1043 environment .default:n = {true},
1044 label .choice:,
1045 label / none .code:n = {
1046 \bool_gset_false:c{\l_self_tl.labelizable_bool}},
1047 label / accepts .code:n = {
1048 \bool_gset_true:c{\l_self_tl.labelizable_bool}},
1049 knowledge .choice:,
1050 knowledge / none .code:n = {
1051 \bool_gset_false:c{\l_self_tl.acceptsknowledge_bool}
1052 \bool_gset_false:c{\l_self_tl.attractsknowledge_bool}},
1053 knowledge / accepts .code:n = {
1054 \bool_gset_true:c{\l_self_tl.acceptsknowledge_bool}
1055 \bool_gset_false:c{\l_self_tl.attractsknowledge_bool}},
1056 knowledge / attracts .code:n = {
1057 \bool_gset_true:c{\l_self_tl.acceptsknowledge_bool}
1058 \bool_gset_true:c{\l_self_tl.attractsknowledge_bool}},
1059 forces .code:n =
1060 {\tl_gset:cn{\l_self_tl.forces_tl}{#1}}
1061 }
1062 % \end{macrocode}
1063 %
1064 % \paragraph{Categories}
1065 %
1066 %\begin{compactdesc}
1067 % \item[\csintro{\scope_category_if_exist:nTF}] tests if a \kl{category} does exist.
1068 % \item[\csintro{\scope_category_ensure:n}] guarantees that a \kl{category} exists, possibly creating it if necessary.
1069 % \item[\csintro{\scope_category_create:n}] creates a \kl{category}.
1070 % \item[\csintro{\scope_category_add_area:nn}] adds an \kl{area} to a \kl{category}.
1071 % \item[\csintro{\scope_category_use:n}] displays the \kl{category} as a comma separated list of \kl{area} names (expandable).
1072 % \item[\csintro{\scope_category_show:n}] shows the content of a \kl{category} on the console.
1073 % \end{compactdesc}
1074 % \iffalse
1075 % \begin{macrocode}
1076 \cs_new:Npn\scope_category_if_exist:nTF#1
1077 {\tl_if_exist:cTF{\scope_tl.category[#1].name_tl}}
1078 % \end{macrocode}
1079 % \begin{macrocode}
1080 \cs_new:Nn\scope_category_ensure:n
1081 {\scope_category_if_exist:nTF
1082 {#1}{}{\scope_category_create:n{#1}}}
1083 % \end{macrocode}
1084 % \begin{macrocode}
1085 \cs_new:Nn\scope_category_create:n
1086 {\tl_new:c{\scope_tl.category[#1].name_tl}
1087 \tl_gset:cn{\scope_tl.category[#1].name_tl}{#1}
1088 \seq_new:c{\scope_tl.category[#1].areas_seq}}
1089 % \end{macrocode}
1090 % \begin{macrocode}
1091 \cs_new:Nn\scope_category_add_area:nn
1092 {\scope_category_ensure:n{#1}
1093 \seq_gpush:cn{\scope_tl.category[#1].areas_seq}{#2}}
1094 % \end{macrocode}
1095 % \begin{macrocode}
1096 \cs_new:Nn\scope_category_use:n
1097 {\seq_use:cn{\scope_tl.category[#1].areas_seq},}
1098 % \end{macrocode}
1099 % \begin{macrocode}
1100 \cs_new:Nn\scope_category_show:n
1101 {\seq_show:c{\scope_tl.category[#1].areas_seq}}
1102 % \end{macrocode}
1103 %\fi
1104 %
1105 %\paragraph{Instances}
1106 %
1107 % We now turn to define the fields involved in an instance.
1108 % There are two series of objects. The ones defined in the first pass of compilation,
1109 % and used for maintaining the structure of the scopes, and the ones
1110 % that are used in the second pass, which gather information throughout the
1111 % whole file. The first one are prefixed by \texttt{scope},
1112 % the others by \texttt{kaux}.
1113 % \begin{compactdesc}
1114 % \item[\intro{instance.area}] is the name of the area it is an instance of.
1115 % \item[\intro{instance.parent}] is the name of the parent instance.
1116 % \end{compactdesc}
1117 % \begin{compactdesc}
1118 % \item[\csintro{\scope_instance_if_exist:nTF}]
1119 % \item[\csintro{\scopept_instance_if_exist:nTF}]
1120 % \item[\csintro{\scopept_label_if_exist:nTF}]
1121 % \end{compactdesc}
1122 %\iffalse
1123 % \begin{macrocode}
1124 \cs_new:Npn\scope_instance_if_exist:nTF#1
1125 {\tl_if_exist:cTF{\scope_tl.instance[#1].area_tl}}
1126 % \end{macrocode}
1127 % \begin{macrocode}
1128 \cs_new:Npn\scopept_instance_if_exist:nTF#1
1129 {\tl_if_exist:cTF{\scopept_tl.instance[#1].area_tl}}
1130 % \end{macrocode}
1131 % \begin{macrocode}
1132 \cs_new:Npn\scopept_label_if_exist:nTF#1
1133 {\tl_if_exist:cTF{\scopept_tl.label[#1].instance_tl}}
1134 % \end{macrocode}
1135 %\fi
1136 %
1137 % \subsubsection{Pushing and popping}
1138 %
1139 % Introduces the macros \csintro{\scope_area_push:n} and \csintro{\scope_area_pop:n}.
1140 %
1141 % The token lists \csintro{\scope_popped_area_tl} and \csintro{\scope_pushed_area_tl}
1142 % contain the area that triggered the push / the pop. These lists are only meaningful in these macros.
1143 % \begin{macrocode}
1144 \tl_new:N\scope_pushed_area_tl
1145 \tl_new:N\scope_popped_area_tl
1146 % \end{macrocode}
1147 % \begin{macrocode}
1148 \cs_new:Nn\scope_area_pop:n {
1149 \tl_gset:Nn\scope_popped_area_tl{#1}
1150 \bool_if:cTF{\scope_tl.area[\scope_popped_area_tl].autoclose_bool}
1151 {\msg_error:nn{scope}{no~popping}}
1152 {\scope_area_pop_i:}}
1153 % \end{macrocode}
1154 % \begin{macrocode}
1155 \cs_new:Nn\scope_area_pop_i: {
1156 \tl_if_eq:NNTF\scope_top_area_tl\scope_popped_area_tl
1157 {\scope_internal_pop:}
1158 {\bool_if:cTF{\scope_tl.area[\scope_top_area_tl].autoclose_bool}
1159 {\scope_internal_pop:
1160 \scope_area_pop_i:}
1161 {\msg_error:nn{scope}{no~popping~here}}}}
1162 % \end{macrocode}
1163 % \begin{macrocode}
1164 \cs_new:Nn\scope_internal_pop: {
1165 \use:c{\scope_tl.area[\scope_top_area_tl].popcode_tl}
1166 \seq_gpop:NNTF\scope_instances_stack_seq\l_tmpa_tl
1167 {\tl_gset_eq:NN\scope_top_instance_tl\l_tmpa_tl
1168 \int_gdecr:c{\scope_tl.area[\scope_top_area_tl].nesting_int}
1169 \knowledge_pop_visible_instances_to_mark:
1170 \seq_get:NN\scope_instances_stack_seq\l_tmpa_tl
1171 \tl_gset_eq:NN\scope_top_instance_tl\l_tmpa_tl
1172 \tl_gset:Nx\scope_top_area_tl
1173 {\tl_use:c{\scope_tl.instance[\scope_top_instance_tl].area_tl}}}
1174 {\msg_error:nnn{scope}{empty~pop}{}}
1175 }
1176 % \end{macrocode}
1177 % \begin{macrocode}
1178 \cs_new:Nn\scope_area_push:n {
1179 \tl_gset:Nn\scope_pushed_area_tl{#1}
1180 \scope_area_push_i:n{#1}}
1181 % \end{macrocode}
1182 % \begin{macrocode}
1183 \cs_new:Nn\scope_area_push_i:n {
1184 \tl_if_empty:cTF{\scope_tl.area[#1].forces_tl}
1185 {}
1186 {\exp_args:Nx\scope_area_if_in_scope:nTF
1187 {\tl_use:c{\scope_tl.area[#1].forces_tl}}
1188 {}
1189 {\exp_args:Nx\scope_area_push_i:n
1190 {\tl_use:c{\scope_tl.area[#1].forces_tl}}}}
1191 \scope_area_push_ii:n{#1}
1192 }
1193 % \end{macrocode}
1194 % \begin{macrocode}
1195 \cs_new:Npn\scope_if_area_in_areaclist:nnTF#1
1196 {\exp_args:NnV\clist_if_in:nnTF{#1}\scope_top_area_tl}
1197
1198 \cs_new:Nn\scope_category_expand:n
1199 {\clist_use:x{
1200 \clist_map:nN{#1}{
1201 \seq_use:c{\scope_tl.category[#1].areas_seq},}}}
1202
1203 \cs_new:Nn\scope_categories_expand:n
1204 {\clist_map_function:nN{#1}\scope_category_expand:n}
1205
1206 \cs_new:Nn\scope_if_area_in_categoryclist:nnTF
1207 {\group_begin:
1208 \tl_set:Nx\l_tmpa_tl
1209 {\clist_map_function:nN{#2}\scope_category_areas_use:n}
1210 \bool_set_false:N\l_tmpa_bool
1211 \clist_map_inline:nn{#2}
1212 {\scope_category_areas_if_in:nnTF{##1}{#1}
1213 {\bool_set_true:N\l_tmpa_bool\seq_map_break:}{}}
1214 \bool_if:NTF\l_tmpa_bool
1215 {\group_end:#3}
1216 {\group_end:#4}
1217 }
1218
1219
1220 \cs_new:Npn\scope_category_if_area_in:nnTF#1#2
1221 {\exp_args:Nnx\seq_if_in:cnTF
1222 {\scope_tl.category[#1].areas_seq}{#2}}
1223
1224 \cs_new:Nn\scope_categoryseq_if_area_in:NnTF
1225 { \seq_if_empty:NTF#1
1226 {#3}
1227 {\group_begin:
1228 \bool_set_false:N\l_tmpa_bool
1229 \seq_map_inline:Nn#1
1230 {\seq_if_in:cnTF{\scope_tl.category[##1].areas_seq}{#2}
1231 {\bool_set_true:N\l_tmpa_bool\seq_map_break:}{}}
1232 \bool_if:NTF\l_tmpa_bool
1233 {\group_end:#3}
1234 {\group_end:#4}
1235 }}
1236
1237 \cs_new:Npn\scope_area_mandatorybase_if_area_in:nnTF#1
1238 {\exp_args:Nc\scope_categoryseq_if_area_in:NnTF
1239 {\scope_tl.area[#1].mandatorybase_seq}}
1240
1241 \cs_new:Nn\scope_categoryseq_pop_to:N
1242 {\exp_args:NNx\scope_categoryseq_if_area_in:NnTF#1{\scope_top_area_tl}
1243 {}{\scope_auto_pop:\scope_categoryseq_pop_to:N#1}}
1244
1245 \cs_new:Nn\scope_categoryseq_pop_to:n
1246 {\group_begin:
1247 \seq_set_from_clist:Nn\l_tmpa_seq{#1}
1248 \scope_categoryseq_pop_to:N\l_tmpa_seq
1249 \group_end:}
1250
1251 \cs_new:Nn\scope_area_push_ii:n {
1252 \seq_if_empty:cTF{\scope_tl.area[#1].mandatorybase_seq}
1253 {\scope_internal_push:n{#1}}
1254 {\exp_args:Nnx\use:n
1255 {\scope_area_mandatorybase_if_area_in:nnTF{#1}}
1256 {\scope_top_area_tl}
1257 {\scope_internal_push:n{#1}}
1258 {\scope_auto_pop:
1259 \scope_area_push_ii:n{#1}}}
1260 }
1261 % \end{macrocode}
1262 % \begin{macrocode}
1263 \cs_new:Nn\scope_internal_push:n {
1264 \ScopeActivate
1265 \tl_gset:Nn\scope_top_area_tl{#1}
1266 \int_gincr:c{\scope_tl.area[\scope_top_area_tl].nesting_int}
1267 \int_gincr:c{\scope_tl.area[\scope_top_area_tl].count_int}
1268 % storing the previous top instance
1269 \tl_set_eq:NN\l_tmpa_tl\scope_top_instance_tl
1270 %naming the new instance
1271 \bool_if:cTF{\scope_tl.area[\scope_top_area_tl].multiple_bool}
1272 {\tl_gset:Nx\scope_top_instance_tl
1273 {\scope_top_area_tl-\int_to_arabic:n{\int_use:c{\scope_tl.area[\scope_top_area_tl].count_int}}}}
1274 {\tl_gset_eq:NN\scope_top_instance_tl\scope_top_area_tl}
1275 %
1276 \knowledge_push_visible_instance_mark:
1277 \exp_args:Nx\knowledge_push_visible_instance:n{\scope_top_instance_tl}
1278 \seq_gpush:Nx\scope_instances_stack_seq{\scope_top_instance_tl}
1279 %
1280 \tl_new:c
1281 {\scope_tl.instance[\scope_top_instance_tl].area_tl}
1282 \tl_gset:cx
1283 {\scope_tl.instance[\scope_top_instance_tl].area_tl}
1284 {\scope_top_area_tl}
1285 \tl_new:c
1286 {\scope_tl.instance[\scope_top_instance_tl].parent_tl}
1287 \tl_gset:cx
1288 {\scope_tl.instance[\scope_top_instance_tl].parent_tl}
1289 {\l_tmpa_tl}
1290 %
1291 \seq_if_exist:cTF{\scopept_tl.instance[\l_tmpa_tl].context_seq}
1292 {\seq_set_eq:Nc\l_tmpa_seq{\scopept_tl.instance[\l_tmpa_tl].context_seq}}
1293 {\seq_clear_new:N\l_tmpa_seq}
1294 \exp_args:NNx\seq_push:Nn
1295 \l_tmpa_seq{\scope_top_instance_tl}
1296 \seq_new:c
1297 {\scopept_tl.instance[\scope_top_instance_tl].context_seq}
1298 \seq_gset_eq:cN
1299 {\scopept_tl.instance[\scope_top_instance_tl].context_seq}
1300 \l_tmpa_seq
1301 %
1302 \KAuxWriteX
1303 {\KAuxScopeNewInstance{\scope_top_area_tl}{\scope_top_instance_tl}{\l_tmpa_tl}}
1304 %
1305 \tl_use:c{\scope_tl.area[\scope_top_area_tl].pushcode_tl}
1306 }
1307 % \end{macrocode}
1308 % \begin{macrocode}
1309 \cs_new:Nn\scope_auto_pop: {
1310 \bool_if:cTF{\scope_tl.area[\scope_top_area_tl].autoclose_bool}
1311 {\scope_internal_pop:}
1312 {\msg_error:nn{scope}{no~pushing~here}}
1313 }
1314 % \end{macrocode}
1315 %
1316 %
1317 % \subsubsection{Inspecting}
1318 %
1319 % \csintro{\scope_get_attractive_instance:N} takes a \kl{token list variable},
1320 % and sets it to contain an
1321 % instance able to receive knowledges.
1322 % This is done by inspecting the scope stack until reaching an instance
1323 % that has \kl{instance.attractsknowledge_bool} set to true (by default, this is `base').
1324 % \begin{macrocode}
1325 \cs_new:Nn\scope_get_attractive_instance:N
1326 {\tl_set:Nn#1{base}
1327 \seq_map_inline:Nn\scope_instances_stack_seq
1328 {\bool_if:cTF
1329 {\scope_tl.area[\tl_use:c{\scope_tl.instance[##1].area_tl}].attractsknowledge_bool}
1330 {\tl_set:Nn#1{##1}\seq_map_break:}{}}
1331 }
1332 % \end{macrocode}
1333 % \csintro{\scope_get_labelizable_instance:N} takes a token list variable as input for holding the result.
1334 % It inspects the current stack of instances \cs{\scope_instances_stack_seq}, searching for an "instance"
1335 % of a "scope" which is labelizable, and returns it. The default is `base'.
1336 % \begin{macrocode}
1337 \cs_new:Nn\scope_get_labelizable_instance:N
1338 {\tl_set:Nn#1{base}
1339 \seq_map_inline:Nn\scope_instances_stack_seq
1340 {\bool_if:cTF
1341 {\scope_tl.area[\tl_use:c{\scope_tl.instance[##1].area_tl}].labelizable_bool}
1342 {\tl_set:Nn#1{##1}\seq_map_break:}{}}
1343 }
1344 % \end{macrocode}
1345 % The command \csintro{\knowledge_scope_track_instance:Nn} is the internal part od the "scope=" directive.
1346 % It takes as argument a token list variable for holding the result, and a text to be looked for.
1347 % If the text corresponds to a "scope" name (such as section, theorem, \dots), then it inspects the
1348 % content of the stacks for finding the target.
1349 % Otherwise, it uses it as a label.
1350 % and if it fails, it tries to find a label of this name that provides an instance.
1351 % \begin{macrocode}
1352 %\cs_new:Nn\knowledge_scope_track_instance:Nn{
1353 % \tl_show:n{#1:#2}
1354 % \scope_area_if_exist:nTF{#2}
1355 % {\knowledge_scope_track_instance_from_area:Nn#1{#2}}
1356 % {\knowledge_scope_track_instance_from_scopetag:Nn#1{#2}}
1357 %}
1358 \cs_new:Nn\knowledge_scope_track_instance_from_area:Nn{
1359 \tl_set:Nn#1{}
1360 \seq_map_inline:Nn\scope_instances_stack_seq
1361 {\exp_args:Nxx\tl_if_eq:nnTF
1362 {\csname \scope_tl.instance[##1].area_tl\endcsname}
1363 {#2}
1364 {\tl_set:Nn#1{##1}\seq_map_break:}
1365 {}}
1366 \tl_if_empty:NTF#1
1367 {\msg_error:nnxx{scope}{area~not~in~context}{#2}{scope=}}
1368 {}
1369 }
1370 % \end{macrocode}
1371 % \AP The command \cs{\knowledge_scope_track_scope:n}
1372 % is used to set the variable \cs{\knowledge_knowledge_instance_tl}.
1373 % If the argument is a scope name, then it is searched for in the context, and the corresponding "instance" is used.
1374 % Otherwise it is used as a "scopetag"; this "scopetag" being potentially
1375 % defined if necessary using \cs{\knowledge_scopetag_warn_if_unknown:n}.
1376 % \begin{macrocode}
1377 \cs_new:Nn\knowledge_scope_track_scope:n{
1378 \scope_area_if_exist:nTF{#1}
1379 {\knowledge_scope_track_instance_from_area:Nn\knowledge_knowledge_instance_tl{#1}}
1380 {\tl_set:Nn\knowledge_knowledge_instance_tl{explicit-scope:#1}
1381 \knowledge_scopetag_warn_if_unknown:n{#1}}
1382 }
1383 % \end{macrocode}
1384 % \begin{macrocode}
1385 \bool_new:N\scopept_map_break_bool
1386 \cs_new:Nn\scopept_map_break:
1387 {\bool_set_true:N\scopept_map_break_bool}
1388
1389 \cs_new:Nn\scopept_map_instance_ancestors:nN
1390 {\bool_set_false:N\scopept_map_break_bool
1391 \scope_map_instance_ancestors_i:nN{#1}#2
1392 }
1393 \cs_new:Nn\scopept_map_instance_ancestors_i:nN
1394 { \tl_if_empty:nTF{#1}
1395 {}
1396 {\scopept_instance_if_exist:nTF{#1}{
1397 #2{#1}
1398 \bool_if:NTF\scopept_map_break_bool
1399 {}
1400 {\exp_args:Nx\scopept_map_instance_ancestors_i:nN
1401 {\scopept_instance_parent_at:Nn\tl_use:N{#1}}#2}}
1402 {}}}
1403 \cs_new:Nn\scopept_map_instances_seq_ancestors:NN
1404 {\seq_map_inline:Nn#1
1405 {\scopept_map_instance_ancestors_i:nN{##1}#2
1406 \bool_if:NTF\scopept_map_break_bool:{\seq_map_break:}{}}
1407 }
1408 % \end{macrocode}
1409 %
1410 %
1411 % \subsubsection{Scope tags}
1412 %
1413 % \AP A ""scopetag"" is a name that can be used to identify a family of "instances" that correspond to it.
1414 % It gets to have two variables attached to it:
1415 % \begin{description}
1416 % \itemAP[\cs{\knowledge_scopetag_tl}\texttt{{[TAG]}\_bool}] which tells whether it
1417 % has been officially declared (it is false if it has been used but not declared by a \cs{\knowledgescope}
1418 % or a \cs{\label} command).
1419 % \itemAP[\cs{\knowledge_scopetag_tl}\texttt{{[TAG]}\_seq}] is a sequence of
1420 % "instances" that are attached to it. It always contains (as soon as created) \textbf{explicit-scope:}\texttt{TAG}.
1421 % \end{description}
1422 %
1423 % In order to identify scopes, the command~\cs{\knowledgescope} (or \cs{\label} which is overloaded for this purpose).
1424 %
1425 % \par\noindent\AP\csintro{\knowledge_scopetag_tl} is a unique string for identifying "scopetags".
1426 % \par\noindent\AP\csintro{\knowledge_scopetag_if_exist:nTF} tests the existence of a "scopetag".
1427 % \par\noindent\AP\csintro{\knowledge_scopetag_ensure:n} guarantees that the "scopetag" is defined (by default, the boolean is set to false).
1428 % \par\noindent\AP\csintro{\knowledge_scopetag_warn_if_unknown:n} writes \cs{\KAuxUndeclaredScopeTag} to the "kaux file" if the "scopetag" was never met before, then calls \cs{\knowledge_scopetag_ensure:n}.
1429 % \par\noindent\AP\csintro{\knowledge_scopetag_declares:n} creates the "scopetag" if necessary, and if it was not known before, declares and puts a \cs{\KAuxDeclaredScopeTag} command in the "kaux file".
1430 % \begin{macrocode}
1431 \tl_const:Nn\knowledge_scopetag_tl{ScpT@g}
1432 % \end{macrocode}
1433 % \begin{macrocode}
1434 \cs_new:Npn\knowledge_scopetag_if_exist:nTF#1
1435 {\tl_if_exist:cTF{\knowledge_scopetag_tl[#1]_bool}}
1436 \cs_new:Nn\knowledge_scopetag_ensure:n{
1437 \knowledge_scopetag_if_exist:nTF{#1}{}
1438 {\bool_new:c{\knowledge_scopetag_tl[#1]_bool}
1439 \bool_gset_false:c{\knowledge_scopetag_tl[#1]_bool}
1440 \seq_new:c{\knowledge_scopetag_tl[#1]_seq}
1441 \seq_gput_right:cn{\knowledge_scopetag_tl[#1]_seq}{explicit-scope:#1}}}
1442 % \end{macrocode}
1443 % \begin{macrocode}
1444 \cs_new:Nn\knowledge_scopetag_warn_if_unknown:n{
1445 \knowledge_scopetag_if_exist:nTF{#1}{}
1446 {\KAuxWriteX*{\exp_not:N\KAuxUndeclaredScopeTag{#1}}
1447 \knowledge_scopetag_ensure:n{#1}}
1448 }
1449 % \end{macrocode}
1450 % \begin{macrocode}
1451 \cs_new:Nn\knowledge_scopetag_declares:n{
1452 \knowledge_scopetag_ensure:n{#1}
1453 \bool_if:cTF{\knowledge_scopetag_tl[#1]_bool}
1454 {}
1455 {\bool_gset_true:c{\knowledge_scopetag_tl[#1]_bool}}
1456 \KAuxWriteX*{\exp_not:N\KAuxDeclaredScopeTag{#1}}
1457 }
1458 % \end{macrocode}
1459 % \AP The command \csintro{\knowledgescope} associates an "instance" to one or more "scopetags":
1460 % \begin{macrocode}
1461 \NewDocumentCommand\knowledgescope{ >{\SplitList{,}}m }{
1462 \kl_hide_begin:\group_begin:
1463 \ProcessList{#1}\knowledge_knowledgescope_one:n
1464 \group_end:\kl_hide_end:
1465 }
1466 % \end{macrocode}
1467 % In the following code, now the name of the instance is expanded before being written to the "kaux file".
1468 % \begin{macrocode}
1469 \cs_new:Nn\knowledge_knowledgescope_one:n{
1470 \knowledge_scopetag_declares:n{#1}
1471 \scope_get_labelizable_instance:N\l_tmpb_tl
1472 \KAuxWriteX*{\exp_not:N\KAuxNewLinkScopetagInstance{#1}{\l_tmpb_tl}}
1473 }
1474 % \end{macrocode}
1475 %
1476 % \paragraph{Modified labels}
1477 % \AP The boolean \csintro{\knowledge_configuration_label_autoscope_bool}
1478 % activates or deactivates the automatic calling to \cs{knowlegescope} when \cs{\label} is used.
1479 % \begin{macrocode}
1480 \bool_new:N\knowledge_configuration_label_autoscope_bool
1481 \bool_gset_true:N\knowledge_configuration_label_autoscope_bool
1482 \KnowledgeConfigureBooleanOption[\knowledge_configuration_label_autoscope_bool]{patch~label}
1483 \KnowledgeConfigureBooleanOption[\knowledge_configuration_label_autoscope_bool]{label~scope}
1484 % \end{macrocode}
1485 % One first changes the behavior of \cs\label\ in the preamble.
1486 % \begin{macrocode}
1487 \global\let\knowledge_label_before_loading_package:n\label
1488 \cs_gset:Npn\label{
1489 \mode_if_preamble:TF
1490 \knowledge_label_in_preamble:n
1491 \knowledge_label_before_loading_package:n
1492 }
1493 \cs_new:Npn\knowledge_label_in_preamble:n{
1494 \bool_if:NTF
1495 \knowledge_configuration_label_autoscope_bool
1496 \knowledge_knowledgescope_one:n
1497 \use_none:n
1498 }
1499 % \end{macrocode}
1500 % \AP In order to circumvent this overloading, the normal \cs{\label} command
1501 % can be used using \csintro{\knowledge_latex_label:n}:
1502 % \begin{macrocode}
1503 \cs_new:Npn\knowledge_latex_label:n{
1504 \cs_if_exist:NTF\LaTeXlabel\LaTeXlabel\label
1505 }
1506 % \end{macrocode}
1507 % \par\noindent\csintro{\knowledge_scopetag_warn_if_unknown:n} takes a "scopetag" as input.
1508 % If it does not exist, it defines, and sets its boolean to false.
1509 % \begin{macrocode}
1510 \NewKAuxCommand\KAuxUndeclaredScopeTag{m}
1511 {Package~scope:~States~that~a~labelname~is~used~in~a~knowledge~definition.}
1512 \DeclareKAuxPhaseCommand\KAuxUndeclaredScopeTag{init}{}
1513 \NewKAuxCommand\KAuxDeclaredScopeTag{m}
1514 {Package~scope:~States~that~a~labelname~has~been~declared.}
1515 \DeclareKAuxPhaseCommand\KAuxDeclaredScopeTag{init}{
1516 \knowledge_scopetag_if_exist:nTF{#1}{}
1517 {\bool_new:c{\knowledge_scopetag_tl[#1]_bool}}
1518 \bool_gset_true:c{\knowledge_scopetag_tl[#1]_bool}
1519 }
1520 \NewKAuxCommand\KAuxNewLinkScopetagInstance{mm}
1521 {Package~scope:~Declares~that~a~label~is~declared~in~an~instance~of~a~scope.}
1522 \DeclareKAuxPhaseCommand\KAuxNewLinkScopetagInstance{init}{
1523 \knowledge_scopetag_ensure:n{#1}
1524 \seq_gput_right:cn{\knowledge_scopetag_tl[#1]_seq}{#2}
1525 }
1526 % \end{macrocode}
1527 %
1528 %
1529 %\subsubsection{Processing the \kl{kaux file}}
1530 %
1531 % When reading the file, scope outputs commands that will be processed
1532 % at the next iteration. It shares the output files \kl{jobname.kaux} for that.
1533 % Commands that may be used in this process are defined here.
1534 % \begin{macrocode}
1535 \NewKAuxCommand\KAuxScopeNewInstance{mmm}
1536 {Package~scope:~Declares~a~new~instance~of~a~scope.}
1537 \DeclareKAuxPhaseCommand\KAuxScopeNewInstance{init}
1538 {\tl_new:cn{\scopept_tl.instance[#2].area_tl}{#1}
1539 \tl_new:cn{\scopept_tl.instance[#2].parent_tl}{#3}}
1540 % \end{macrocode}
1541 % \begin{macrocode}
1542 \NewKAuxCommand\KAuxScopeTag{mm}
1543 {Package~scope:~Links~a~scopetag~to~an~instance~of~a~scope.}
1544 \DeclareKAuxPhaseCommand\KAuxScopeTag{init}
1545 {\tl_if_exist:cTF{\scopept_tl.label[\tl_to_str:n{#1}].instance_tl}{}
1546 {\tl_new:cn{\scopept_tl.label[\tl_to_str:n{#1}].instance_tl}{#2}}}
1547 % \end{macrocode}
1548 % \begin{macrocode}
1549 \cs_new:Npn\scopept_label_instance_if_exist:nTF#1
1550 {\tl_if_exist:cTF{\scopept_tl.label[#1].instance_tl}}
1551 \cs_new:Nn\scopept_label_instance_use:n
1552 {\tl_use:c{\scopept_tl.label[#1].instance_tl}}
1553 % \end{macrocode}
1554 %
1555 % \subsubsection{Commands}
1556 %
1557 % The command \cs{\knowledgeconfigureenvironment} declares a
1558 % standard "latex" environment to behave like the \kl(env){scope} environment
1559 % (while keeping its standard display).
1560 % \begin{macrocode}
1561 \newcommand\knowledgeconfigureenvironment\KnowledgeConfigureEnvironment
1562 \NewDocumentCommand\KnowledgeConfigureEnvironment{t?t!mm}
1563 {\clist_map_inline:nn{#3}{
1564 \IfBooleanTF{#2}
1565 {\scope_area_environment_new:nn{##1}{#4}}
1566 {\cs_if_exist:cT{##1}
1567 {\cs_if_exist:cT{end##1}
1568 {\scope_area_environment_new:nn{##1}{#4}
1569 \use_none:n}}
1570 {\IfBooleanF{#1}
1571 {\knowledge_error:n{Unknown~environment~`##1'~in~\KnowledgeConfigureEnvironment.}}}
1572 }}}
1573 \newcommand\scope_activate_environment[2][environment]{
1574 \scope_area_environment_new:nn
1575 {#2}{category=#1}}
1576 \cs_new:Nn\scope_area_environment_new:nn
1577 {\scope_area_set:nn{#1}{
1578 parents = all,
1579 autoclose = false,
1580 environment = true,
1581 occurrences = recursive,
1582 knowledge = attracts,
1583 category = environment,
1584 #2
1585 }}
1586 % \end{macrocode}
1587 %
1588 %
1589 % \subsubsection{Activation of the \kl{scope option}}
1590 %
1591 % \begin{macrocode}
1592 \NewDocumentCommand\ScopeHackEnvironments{}
1593 { \scope_area_push:n{document}
1594 \kutils_secure_latex_command:N\begin
1595 \global\let\SUPERbegin\begin
1596 \global\def\begin##1
1597 {\scope_if_triggers_area:nTF{##1}
1598 {\scope_area_push:n{##1}}{}
1599 \SUPERbegin{##1}}
1600 \kutils_secure_latex_command:N\end
1601 \global\let\SUPERend\end
1602 \global\def\end##1
1603 {\SUPERend{##1}
1604 \scope_if_triggers_area:nTF{##1}
1605 {\scope_area_pop:n{##1}}
1606 {}}
1607 }
1608 \AtBeginDocument{
1609 \kutils_secure_latex_command:N\label
1610 \RenewDocumentCommand\label{om}{\bool_if:NT
1611 \knowledge_configuration_label_autoscope_bool
1612 {\knowledge_knowledgescope_one:n{#2}}
1613 % if cleveref is loaded, label can use an optional type argument
1614 \IfNoValueTF{#1}{\LaTeXlabel{#2}}{\LaTeXlabel[{#1}]{#2}}}
1615 }
1616 % \end{macrocode}
1617 %
1618 % This command will activate the scope package.
1619 % The idea is that this package, if activated too early, then
1620 % it may not yet have all its features usable. However, it
1621 % must be loaded before being used, e.g., by commands
1622 % like knowledge.
1623 % \begin{macrocode}
1624 \NewDocumentCommand\ScopeActivate{}{
1625 \cs_gset_eq:NN\ScopeActivate\relax
1626 \scope_internal_push:n{base}
1627 }
1628 \AtBeginDocument{\ScopeActivate}
1629 % \end{macrocode}
1630 %
1631 % \begin{macrocode}
1632 \AtBeginDocument{\ScopeHackEnvironments}
1633 \AtEndDocument{\scope_area_pop:n{document}}
1634 % \end{macrocode}
1635 %
1636 % \begin{macrocode}
1637 \scope_area_set:nn{document,base,body} {
1638 environment=false
1639 }
1640 \scope_area_set:nn{base}
1641 {occurrences = once,
1642 autoclose = false}
1643 \scope_area_set:nn{document}
1644 {autoclose = false,
1645 occurrences = once,
1646 parents = base}
1647 \scope_area_set:nn{body}
1648 {autoclose = true,
1649 occurrences = once,
1650 parents = document}
1651 \scope_area_set:nn
1652 {base,document}{knowledge=accepts}
1653 \scope_area_set:nn
1654 {base,document}{knowledge=attracts}
1655 \scope_area_set:nn
1656 {body}{knowledge=none}
1657 %
1658 \newenvironment{scope}
1659 {\mode_if_preamble:TF{\ScopeActivate\scope_area_push:n{scope}}{}}
1660 {\mode_if_preamble:TF{\scope_area_pop:n{scope}}{}}
1661 \knowledgeconfigureenvironment{scope}{}
1662 % \end{macrocode}
1663 %
1664 % \subsubsection{Importing and exporting scopes}
1665 % \begin{macrocode}
1666 \scope_area_set:nn{knowledge~context~internal}
1667 {
1668 occurrences = recursive,
1669 autoclose = false,
1670 parents= {base,all},
1671 knowledge=attracts
1672 }
1673 \scope_area_set:nn{knowledge~import~internal}
1674 {
1675 occurrences = recursive,
1676 autoclose = false,
1677 parents= {base,all},
1678 knowledge = accepts
1679 }
1680 \NewDocumentEnvironment{knowledge~import}{m}
1681 {\ScopeActivate\scope_area_push:n{knowledge~import~internal}
1682 \clist_map_inline:nn{#1}{\knowledgeimport{##1}}}
1683 {\scope_area_pop:n{knowledge~import~internal}}
1684 \NewDocumentEnvironment{knowledge~context}{m}
1685 {\ScopeActivate\scope_area_push:n{knowledge~context~internal}
1686 \knowledgescope{#1}}
1687 {\scope_area_pop:n{knowledge~context~internal}}
1688 % \end{macrocode}
1689 % \subsection{Files}
1690 %
1691 %
1692 % \subsubsection{KAux file}
1693 %
1694 % Code to be executed before, at initialization and after the writing of the \kl{kaux file}.
1695 % It tests before is the file was properly written during the last compilation using |\IfKAuxReadyTF|.
1696 % \begin{macrocode}
1697 \KAuxBefore{\IfKAuxReadyTF{\KAuxProcess{init}}{}}
1698 \KAuxInit{}
1699 \KAuxAfter{\KnowledgeDiagnoseOutput}
1700 % \end{macrocode}
1701 %
1702 % \subsubsection{Diagnose file}
1703 %
1704 % \csintro{\knowledge_diagnose_lastline_tl} contains the text
1705 % of the last line that has been diagnosed. If it did not change, it will
1706 % not be output again.
1707 % \begin{macrocode}
1708 \iow_new:N\knowledge_diagnosefile_iow
1709 \cs_new:Nn\knowledge_diagnose:n
1710 {\iow_now:Nn\knowledge_diagnosefile_iow{#1}}
1711 \cs_generate_variant:Nn\knowledge_diagnose:n{x,V}
1712
1713 \tl_new:N\knowledge_diagnose_lastline_tl
1714 \cs_new:Nn\knowledge_diagnose_reset_line:
1715 {\tl_gset:Nn\knowledge_diagnose_lastline_tl{...}}
1716 \knowledge_diagnose_reset_line:
1717
1718 \cs_new:Nn\knowledge_diagnose_line:
1719 {\bool_if:NT\knowledge_configuration_diagnoseline_bool
1720 {\tl_set:Nx\l_tmpa_tl
1721 {\c_percent_str\space\kauxCurrentFile : \kauxCurrentLine}
1722 \tl_if_eq:NNTF\l_tmpa_tl\knowledge_diagnose_lastline_tl
1723 {}
1724 {\tl_gset_eq:NN\knowledge_diagnose_lastline_tl\l_tmpa_tl
1725 \knowledge_diagnose:V\knowledge_diagnose_lastline_tl}}}
1726
1727 \NewDocumentCommand\KnowledgeDiagnoseOutput{}{
1728 \iow_open:Nn\knowledge_diagnosefile_iow{\c_sys_jobname_str.diagnose}
1729 % compute first the list of warnings and important information
1730 %
1731 \knowledge_diagnose_reset_line:
1732 \KAuxProcess{diagnose-digest}
1733 %
1734 \knowledge_diagnose:n{***********}
1735 \knowledge_diagnose:n{*~Summary~*}
1736 \knowledge_diagnose:n{***********}
1737 \knowledge_diagnose:n{}
1738 \knowledge_diagnose:x{\int_use:N\kl_digest_unknownknowledge_int\space undefined~knowledge(s).}
1739 %\knowledge_diagnose:x{\int_use:N\kl_digest_unknownlabel_int\space label(s)~are~used~as~scopes~but~not~introduced.}
1740 \knowledge_diagnose:x{\int_use:N\kl_digest_autoreftwiceintro_int\space autoreference(s)~are~introduced~twice.}
1741 \knowledge_diagnose:x{\int_use:N\kl_digest_autorefnointro_int\space autoreference(s)~are~used~but~not~introduced.}
1742 \knowledge_diagnose:n{}
1743 \knowledge_diagnose:x{\int_use:N\kl_digest_autorefused_int\space autoreference(s)~are~properly~used.}
1744 \knowledge_diagnose:x{\int_use:N\kl_digest_autorefunused_int\space autoreference(s)~are~defined~but~not~used.}
1745 \knowledge_diagnose:n{}
1746 \knowledge_diagnose:n{}
1747 %
1748 \bool_if:NTF\knowledge_configuration_diagnosehelp_bool
1749 {
1750 \knowledge_diagnose:n{********}
1751 \knowledge_diagnose:n{*~Help~*}
1752 \knowledge_diagnose:n{********}
1753 \knowledge_diagnose:n{}
1754 \knowledge_diagnose:x{\use:c{@backslashchar}\cs_to_str:N\knowledgeconfigure{diagnose~bar=false}~deactivate~`|'-notation~in~diagnose~file.}
1755 \knowledge_diagnose:x{\use:c{@backslashchar}\cs_to_str:N\knowledgeconfigure{diagnose~help=false}~deactivate~long~help~in~the~diagnose~file.}
1756 \knowledge_diagnose:x{\use:c{@backslashchar}\cs_to_str:N\knowledgeconfigure{diagnose~line=true}~add~line~numbers~to~diagnose~file.}
1757 \knowledge_diagnose:n{}
1758 }{
1759 \knowledge_diagnose:x{\use:c{@backslashchar}\cs_to_str:N\knowledgeconfigure{diagnose~help=true}~to~activate~help~in~the~diagnose~file.}
1760 \knowledge_diagnose:n{}
1761 }
1762 %
1763 \int_compare:nNnT\kl_digest_unknownknowledge_int>0{
1764 \knowledge_diagnose:n{************************}
1765 \knowledge_diagnose:n{*~Undefined~knowledges~*}
1766 \knowledge_diagnose:n{************************}
1767 \knowledge_diagnose:n{}
1768 %
1769 \knowledge_diagnose_reset_line:
1770 \bool_if:NT\knowledge_configuration_diagnosebar_bool
1771 {\knowledge_diagnose:x{\use:c{@backslashchar}knowledge{ignore}}}
1772 \KAuxProcess{diagnose-unknown-suggest}
1773 %
1774 \knowledge_diagnose:n{}
1775 \knowledge_diagnose:n{}
1776 %
1777 \msg_warning:nn{knowledge}{recap~unknown~knowledge}
1778 }
1779 %
1780 \int_compare:nNnT\kl_digest_autoreftwiceintro_int>0{
1781 \knowledge_diagnose:n{****************************}
1782 \knowledge_diagnose:n{*~autoref-introduced-twice~*}
1783 \knowledge_diagnose:n{****************************}
1784 \knowledge_diagnose:n{}
1785 %
1786 \knowledge_diagnose_reset_line:
1787 \KAuxProcess{autoref-introduced-twice}
1788 %
1789 \knowledge_diagnose:n{}
1790 \knowledge_diagnose:n{}
1791 %
1792 \msg_warning:nn{knowledge}{recap~autoref~introduced~twice}
1793 }
1794 %
1795 \int_compare:nNnT\kl_digest_autorefnointro_int>0{
1796 \knowledge_diagnose:n{******************************}
1797 \knowledge_diagnose:n{*~Autoref~used~without~intro~*}
1798 \knowledge_diagnose:n{******************************}
1799 \knowledge_diagnose:n{}
1800 %
1801 \knowledge_diagnose_reset_line:
1802 \KAuxProcess{autoref-not-introduced}
1803 %
1804 \knowledge_diagnose:n{}
1805 \knowledge_diagnose:n{}
1806 %
1807 \msg_warning:nn{knowledge}{recap~autoref~not~introduced}
1808 }
1809 \int_compare:nNnT\kl_digest_autorefunused_int>0{
1810 \knowledge_diagnose:n{***********************************}
1811 \knowledge_diagnose:n{*~Autoref~introduced~but~not~used~*}
1812 \knowledge_diagnose:n{***********************************}
1813 \knowledge_diagnose:n{}
1814 %
1815 \knowledge_diagnose_reset_line:
1816 \KAuxProcess{autoref-introduced-not-used}
1817 %
1818 \knowledge_diagnose:n{}
1819 \knowledge_diagnose:n{}
1820 %
1821 }
1822 %
1823 \iffalse
1824 \knowledge_diagnose:n{***********************}
1825 \knowledge_diagnose:n{*~Exported~knowledges~*}
1826 \knowledge_diagnose:n{***********************}
1827 \knowledge_diagnose:n{}
1828 \KAuxProcess{diagnose-export}
1829 \knowledge_diagnose:n{}
1830 \fi%
1831 \iow_close:N\knowledge_diagnosefile_iow
1832 }
1833 % \end{macrocode}
1834 %
1835 %
1836 %
1837 %
1838 % \subsection{Accessing knowledges}
1839 %
1840 % We define now the basic macros that are used for accessing knowledges.
1841 % Each knowledge is identified by three parameters:
1842 % \begin{itemize}
1843 % \itemAP the namespace name (no arobas in it), each different use of the "knowledge@@package" package can use a different namespace. For the moment, this is always `default'.
1844 % \itemAP the \kl{instance} name (defining the scope, i.e. the region in which it is defined; no arobas in it). "Instances" are automatically generated, and the used should never has access to it.
1845 % \itemAP the \kl{knowledge} text itself.
1846 % \end{itemize}
1847 %
1848 % The variable \cs{\knowledge_namespace_tl}
1849 % stores the current namespace, i.e. the ones that is used by default.
1850 % It is sometimes temporarily modified.
1851 % \begin{macrocode}
1852 \tl_new:Nn\knowledge_default_namespace_tl {default}
1853 % \end{macrocode}
1854 % The macro
1855 % \begin{code}
1856 % \csintro{\knowledge_id:nnn}\{namespace\}\{instance\}\{knowledge key\}
1857 % \end{code}
1858 % takes these three arguments in
1859 % this order (namespace/instance/scope) and produces a unique string that can be used to access
1860 % (using \cs{\csname}, or \texttt{:c} in LaTeX3) the tuple.
1861 % \begin{macrocode}
1862 \makeatother
1863 \cell_new_type:nnn{knowledge}{nnnn}{KL@#2@#3@\tl_to_str:n{#4}_#1}
1864 \cell_new_type:nnn{knowledge}{nnn}{\knowledge_id:nnnn{}{#1}{#2}{#3}}
1865 \cs_generate_variant:Nn\knowledge_id:nnn{nnV,nVV,nVn,VVV}
1866 \cs_generate_variant:Nn\knowledge_at:Nnnn{NnnV}
1867 \cs_new:Npn\knowledge_at:c{\exp_args:Nc\knowledge_at:N}
1868 % \end{macrocode}
1869 %
1870 % For convenience, when the same knowledge has to be accessed many times, one uses
1871 % \begin{code}
1872 % \csintro{\knowledge_sets_to:nnn}\{namespace\}\{knowledge key\}\{scope\}
1873 % \end{code}
1874 % which is followed by the same three arguments.
1875 % It defines
1876 % \begin{code}
1877 % \csintro{\knowledge_at:N}\cs{\token}
1878 % \end{code} that executes \cs{\token} with as
1879 % first parameter the token pointing to the knowledge.
1880 % \begin{macrocode}
1881 \cs_new:Nn\knowledge_sets_to:nnn
1882 {\cs_set:Nx\knowledge_at:N
1883 {##1\exp_not:c{\knowledge_id:nnn{#1}{#2}{#3}}}}
1884 % \end{macrocode}
1885 % Finally, one creates a new kind of tuples, called \cs{ktuples}
1886 % for storing the \kl{knowledges}.
1887 % \begin{macrocode}
1888 \tuple_new:n{ktuple}
1889 % \end{macrocode}
1890 % Some more code for testing if a "knowledge" is defined:
1891 % \begin{code}
1892 % \csintro{\knowledge_knowledge_if_exist:nnnTF}\{namespace\}\{instance\}\{key\}\par
1893 % ~~~~~~~\{true code\}\{false code\}
1894 % \end{code}
1895 % \begin{macrocode}
1896 \cs_new:Npn\knowledge_knowledge_if_exist:nnnTF#1#2#3{
1897 \exp_args:Nc\ktuple_if_exist:NTF{\knowledge_id:nnn{#1}{#2}{#3}}
1898 }
1899 \cs_generate_variant:Nn
1900 \knowledge_knowledge_if_exist:nnnTF{VVVTF,VnVTF}
1901 % \end{macrocode}
1902 %
1903 %
1904 % \subsection{Knowledge code}
1905 %
1906 %
1907 % \subsubsection{Generalities}
1908 %
1909 % The \cs{\knowledge} command is dealt in several steps.
1910 % \begin{asparaitem}
1911 % \item In a first phase, the "directives" are parsed using the keys \csintro{knowledge~directives}.
1912 % During this phase, all the information concerning the target is fixed, as well as other pieces of informations
1913 % such as the real reference of the "autoref" directive. The result is gathered in a second comma separated
1914 % list of directives
1915 % \item Then, depdnging on the targetting information, the
1916 % \end{asparaitem}
1917 % Essentially, the \cs{\keys_define:nn}\verb|{ knowledgenow~directives }|(...)
1918 % commands used throughout the code are here to add new functionalities to it.
1919 %
1920 %
1921 % \subsubsection{Defining directives}
1922 %
1923 %
1924 % The commands used for introducing new directives.
1925 % The command
1926 % \begin{code}
1927 % \csintro{\NewKnowledgeParamBool}[*]\{name\}\cs{\token}
1928 % \end{code}
1929 % creates a new boolean directive of name `name'.
1930 % When the knowledge is executed, the value will be stored in the \cs{\token}.
1931 % By default, the directive is only usable in \kl{knowledgenow directives}.
1932 % If the optional star is used, then the \kl{directive} is usable in the \cs{\knowledge} command, and its value is propagated automatically. Hence, no-star means internal booleans used by the
1933 % system, and star means booleans accessible to the user.
1934 % \iffalse
1935 % \begin{macrocode}
1936 \NewDocumentCommand\NewKnowledgeParamBool{smm}{
1937 \ktuple_new_bool_index:n{#2}
1938 \bool_new:N#3
1939 \use:c{ktuple_#2_expands_to:TF}
1940 {\bool_set_true:N#3}{\bool_set_false:N#3}
1941 %
1942 \keys_define:nn { knowledgenow~directives }{
1943 #2 .choice:,
1944 #2 / false .code:n =
1945 { \knowledge_at:c{ktuple_#2_gset_false:N}},
1946 #2 / true .code:n =
1947 { \knowledge_at:c{ktuple_#2_gset_true:N} },
1948 #2 / default .code:n =
1949 { \knowledge_at:c{ktuple_#2_gdel:N}},
1950 #2 .default:n = {true}
1951 }
1952 \IfBooleanTF{#1}{\KnowledgeTransferBool{#2}}{}
1953 }
1954 \NewDocumentCommand\KnowledgeTransferBool{m}{
1955 \clist_map_inline:nn{ #1 }{
1956 \keys_define:nn { knowledge~directives }{
1957 ##1 .choice:,
1958 ##1 / false .code:n = { \knowledge_delay_directive:n{##1= false}},
1959 ##1 / true .code:n = { \knowledge_delay_directive:n{##1= true}},
1960 ##1 / default .code:n = { \knowledge_delay_directive:n{##1= default}},
1961 ##1 .default:n = true
1962 }}
1963 }
1964 % \end{macrocode}
1965 % \fi
1966 % \AP The command \cs{\NewKnowledgeParamTl} is similar to \cs{\NewKnowledgeParamBool},
1967 % but creates a directive that stores text.
1968 % \begin{code}
1969 % \csintro\NewKnowledgeParamTl*[\cs\token]\{name\}
1970 % \end{code}\noindent
1971 % \iffalse
1972 % \begin{macrocode}
1973 \NewDocumentCommand\NewKnowledgeParamTl{som}{
1974 \IfNoValueTF{#2}
1975 { \tl_set:Nn\l_tmpa_tl{#3}
1976 \tl_remove_all:Nn\l_tmpa_tl{~}
1977 \use:x{
1978 \exp_not:N\NewKnowledgeParamTl
1979 \IfBooleanT{#1}{*}
1980 [\exp_not:c{knowledge_kl_\l_tmpa_tl _tl}]
1981 {#3}
1982 }}
1983 {\cs_if_exist:cTF{ktuple_#3_expands_to:n}
1984 {}
1985 {\ktuple_new_tl_index:n{#3}}
1986 \tl_if_exist:NTF#2{}
1987 {\tl_new:N#2}
1988 \use:c{ktuple_#3_expands_to:n}
1989 {\tl_set:Nn#2{##1}}
1990 %
1991 \keys_define:nn { knowledgenow~directives }{
1992 #3 .code:n =
1993 { \knowledge_at:c{ktuple_#3_gset:Nn}{##1} },
1994 #3~X .code:n =
1995 { \knowledge_at:c{ktuple_#3_gset:Nx}{##1} },
1996 no~#3 .code:n =
1997 { \knowledge_at:c{ktuple_#3_gdel:N} }}
1998 \IfBooleanTF{#1}{\KnowledgeTransferTl{#3}}{}
1999 }}
2000 % \end{macrocode}
2001 % \begin{macrocode}
2002 \NewDocumentCommand\KnowledgeTransferTl{m}
2003 {\keys_define:nn { knowledge~directives }
2004 {#1 .code:n = {\knowledge_delay_directive:n{#1={##1}}}}}
2005 % \end{macrocode}
2006 % \fi
2007 % The command \cs{\NewKnowledgeParamCode} is similar to \cs{\NewKnowledgeParamBool},
2008 % but creates a directive that stores code. There is no optional star for code (never directly accessible to the user).
2009 % \begin{code}\AP
2010 % \csintro\NewKnowledgeParamCode\{name\}\cs{\token}
2011 % \end{code}\noindent
2012 % \begin{macrocode}
2013 \NewDocumentCommand\NewKnowledgeParamCode{mm}{
2014 \ktuple_new_tl_index:n{#1}
2015 \tl_new:N#2
2016 \use:c{ktuple_#1_expands_to:n}
2017 {\tl_set:Nn#2{##1}}
2018 %
2019 \keys_define:nn { knowledgenow~directives }{
2020 #1 .code:n =
2021 { \knowledge_at:c
2022 {ktuple_#1_gset:Nn}{##1} },
2023 #1+ .code:n =
2024 { \knowledge_at:c
2025 {ktuple_#1_gput_right:Nn}{##1} },
2026 +#1 .code:n =
2027 { \knowledge_at:c
2028 {ktuple_#1_gput_left:Nn}{##1} }
2029 }}
2030 % \end{macrocode}
2031 %
2032 %\par
2033 % \AP The command \cs{\NewKnowledgeParamPackageError} takes a comma separated
2034 % list of directives as first argument, and a package name as second argument.
2035 % After that, when one of these \kl{directives} is used, an error suggesting to load the
2036 % corresponding package is thrown.
2037 % \begin{code}
2038 % \csintro\NewKnowledgeParamPackageError\{directive list\}\{package name\}
2039 % \end{code}\noindent
2040 % \begin{macrocode}
2041 \NewDocumentCommand\NewKnowledgeParamPackageError{mm}
2042 % \end{macrocode}
2043 % \ifcode
2044 % \begin{macrocode}
2045 { \clist_map_inline:nn{#1}{
2046 \keys_define:nn { knowledge~directives }{
2047 ##1 .code:n = {
2048 \msg_error:nnnn{ knowledge }{ package~required }{##1}{#2}}
2049 }}}
2050 % \end{macrocode}
2051 %
2052 % \fi
2053 %
2054 %
2055 %
2056 % \subsubsection{Scope basics}
2057 %
2058 %
2059 % \kl{Knowledges} are attached to \kl{instances}, which identify
2060 % the region in which these have been defined and also where they can be used.
2061 % For instance, if the user mentions `\kl{scope=}section' when defining a knowledge,
2062 % this means that the knowledge will only be visible in this
2063 % area\footnote{Though it can be imported and become visible elsewhere using appropriate commands.}.
2064 % Technically, instances are not directly handled by the user, nor by the "knowledge@@package" package.
2065 %
2066 % In this part of the code, we provide a small number of commands for dealing with instances.
2067 % The real code that computes the current context of scope if later.
2068 %
2069 %
2070 % \begin{compactdesc}
2071 % \item[\csintro{\knowledge_current_instance_tl}] contains the current
2072 % instance to be used.
2073 % \item[\cs{\knowledge_visible_instances_seq}] contains the sequence
2074 % of instances that are visible right now.
2075 % \item[\csintro{\knowledge_scope_track_instance:Nn}\cs\tokenlist\{scope name\}]
2076 % sets \cs{\tokenlist} to the innermost instance of the scope name.
2077 % Typically, if the request is `\texttt{subsection}', the answer will be `\texttt{subsection-3.1}',
2078 % identifying the subsection currently in scope.
2079 % \end{compactdesc}
2080 % \begin{macrocode}
2081 \tl_new:N\knowledge_current_instance_tl
2082 \tl_gset:Nn\knowledge_current_instance_tl{base}
2083 % \end{macrocode}
2084 %
2085 % \AP The sequence \csintro{\knowledge_visible_instances_seq} contains the
2086 % "instances" that are visible right now. This are searched through when
2087 % a knowledge is used. The leftmost instance is the most important one (innermost scope).
2088 % Note that \cs{\knowledge_visible_instances_seq} may only be correct after one compilation.
2089 % \begin{compactdesc}
2090 % \item[\csintro{\knowledge_push_visible_instance:n}]
2091 % \item[\csintro{\knowledge_push_visible_instance_mark:}] Inserts in \cs{\knowledge_visible_instances_seq} a special marker,
2092 % which is used when pushing a scope.
2093 % \item[\csintro{\knowledge_pop_visible_instances_to_mark:}] Pops everything till the special mark.
2094 % \item[\csintro{\knowledge_pop_visible_instances_to:n}]
2095 % \item[\csintro{\knowledge_show_visible_instances:}]
2096 % \end{compactdesc}
2097 %
2098 % \begin{macrocode}
2099 \seq_new:N\knowledge_visible_instances_seq
2100 % \end{macrocode}
2101 % Special marks are introduced for remembering till where to pop when a "scope" ends.
2102 % \begin{macrocode}
2103 \cs_new:Nn\knowledge_push_visible_instance:n
2104 {\seq_gpush:Nn\knowledge_visible_instances_seq{#1}}
2105 \cs_new:Nn\knowledge_push_visible_instance_mark:
2106 {\seq_gpush:Nn\knowledge_visible_instances_seq{|}}
2107 % \end{macrocode}
2108 %
2109 % \begin{macrocode}
2110 \cs_new:Nn\knowledge_pop_visible_instances_to_mark:
2111 {\seq_gpop:NNTF\knowledge_visible_instances_seq\l_tmpa_tl
2112 {\exp_args:NV\tl_if_eq:nnTF\l_tmpa_tl{|}
2113 {}
2114 {\knowledge_pop_visible_instances_to_mark:}}
2115 {\KNOWLEDGE_INTERNAL_ERROR:_stack_of_visible_instances_is_empty_while_popping}}
2116 % \end{macrocode}
2117 % \begin{macrocode}
2118 \cs_new:Nn\knowledge_show_visible_instances:
2119 {\tl_show:x
2120 {Visible~instances~<\seq_use:Nn\knowledge_visible_instances_seq{,}>}}
2121 % \end{macrocode}
2122 % \begin{macrocode}
2123 \NewDocumentCommand\knowledgeimport{ >{\SplitList{,}}m }{
2124 \kl_hide_begin:
2125 \ProcessList{#1}\knowledge_knowledgeimport:n
2126 \kl_hide_end:
2127 }
2128 % \end{macrocode}
2129 % \begin{macrocode}
2130 \cs_set:Nn\knowledge_knowledgeimport:n{
2131 \knowledge_scopetag_warn_if_unknown:n{#1}
2132 \knowledge_scopetag_ensure:n{#1}
2133 \exp_args:Nc\seq_map_function:NN
2134 {\knowledge_scopetag_tl[#1]_seq}
2135 \knowledge_push_visible_instance:n
2136 }
2137 % \end{macrocode}
2138 %
2139 %
2140 % \subsubsection{The knowledge targeting}
2141 %
2142 % We define now the "directives" that are used for choosing how the "knowledge" should be defined.
2143 %
2144 % \begin{macrocode}
2145 \keys_define:nn { knowledge~directives } {
2146 namespace .code:n = { \tl_set:Nn\knowledge_knowledge_namespace_tl{#1} },
2147 append .choice:,
2148 append / false .code:n = {\bool_set_false:N\knowledge_knowledge_append_bool},
2149 append / true .code:n = {\bool_set_true:N\knowledge_knowledge_append_bool},
2150 append .default:n = false,
2151 clear .choice:,
2152 clear / false .code:n = {\bool_set_false:N\knowledge_knowledge_clear_bool},
2153 clear / true .code:n = {\bool_set_true:N\knowledge_knowledge_clear_bool},
2154 clear .default:n = false,
2155 export .choice:,
2156 export / false .code:n = {\bool_set_false:N\knowledge_knowledge_export_bool},
2157 export / true .code:n = {\bool_set_true:N\knowledge_knowledge_export_bool},
2158 export .default:n = false,
2159 scope .code:n =
2160 { \knowledge_scope_track_scope:n{#1}
2161 \tl_gput_right:Nn\knowledge_knowledge_immediate_synonym_directives_tl{,scope = {#1}}},
2162 synonym .code:n =
2163 { \bool_set_true:N\knowledge_knowledge_is_synonym_bool
2164 \exp_args:Nnx\keys_set:nn { knowledge~directives }
2165 {link={\exp_not:V\knowledge_knowledge_synonymlink_tl},
2166 link~instance={\exp_not:V\knowledge_knowledge_synonyminstance_tl}}},
2167 }
2168 % \end{macrocode}
2169 %
2170 %
2171 % The \csintro{\KAuxKnowledge} command is added to the \kl{kaux file} when
2172 % \cs{\knowledge} delays its definition to the next phase of compilation.
2173 % \begin{macrocode}
2174 \NewKAuxCommand\KAuxKnowledge{mmmm}
2175 {Package~knowledge:~Declares~a~knowledge.~
2176 Takes~the~{namespace}{instance}{knowledge}{directives}.}
2177 % \end{macrocode}
2178 % \begin{macrocode}
2179 \DeclareKAuxPhaseCommand\KAuxKnowledge{init}
2180 {\knowledge_set_now:nnnn{#1}{#2}{#3}{#4}}
2181 % \end{macrocode}
2182 % \begin{macrocode}
2183 \newrobustcmd\KAuxExport[4]
2184 {Package~knowledge:~undefined~so~far.}
2185 % \end{macrocode}
2186 %
2187 %\AP \csintro{\knowledge_set_now:nnnn}\cs{{}} takes the three parameters defining a knowledge and a list of \kl{knowledgenow directives}, checks that it was not existing before, and
2188 % then calls the \cs{\keys_set:nn}\{knowledgenow directives\} for defining it.
2189 % \begin{macrocode}
2190 \cs_new:Nn\knowledge_new_now:nnnn
2191 {\knowledge_sets_to:nnn{#1}{#2}{#3}
2192 \knowledge_at:N\ktuple_if_exist:NTF
2193 {\bool_if:NTF\knowledge_knowledgestyle_bool
2194 {\knowledge_at:N\ktuple_gclear:N}
2195 {\bool_if:NF\knowledge_knowledge_append_bool
2196 {\bool_if:NF\knowledge_knowledge_clear_bool
2197 \knowledge_werror:Nn\knowledge_warningstrict_bool
2198 {knowledge~'#3'~(namespace=#1,~scope=#2)~is~redefined.}}}
2199 }
2200 {\knowledge_at:N\ktuple_new:N}
2201 \keys_set:nn { knowledgenow~directives } { #4 }
2202 }
2203 % \end{macrocode}
2204 % \begin{macrocode}
2205 \cs_new:Nn\knowledge_set_now:nnnn
2206 {\knowledge_sets_to:nnn{#1}{#2}{#3}
2207 \knowledge_at:N\ktuple_if_exist:NTF
2208 {\bool_if:NTF\knowledge_knowledgestyle_bool
2209 {\knowledge_at:N\ktuple_gclear:N}
2210 {}}
2211 {\knowledge_at:N\ktuple_new:N}
2212 \keys_set:nn { knowledgenow~directives } { #4 }
2213 }
2214 % \end{macrocode}
2215 % \begin{macrocode}
2216 \cs_generate_variant:Nn\knowledge_new_now:nnnn{nnVn}
2217 \cs_generate_variant:Nn\knowledge_set_now:nnnn{nnVn}
2218 % \end{macrocode}
2219 %
2220 %
2221 %\AP We can now define the \cs{\knowledge} command itself.
2222 % It starts by writing the line number in the \kl{kaux file}
2223 % using \cs{\KAuxWriteLocation}, and then proceeds with \cs{\__knowledge_knowledge}.
2224 % \begin{macrocode}
2225 \NewDocumentCommand\knowledge{}
2226 {\KAuxWriteLocation\__knowledge_knowledge_init:w}
2227 % \end{macrocode}
2228 % The command \csintro{\__knowledge_knowledge} is the center of the \cs{\knowledge}
2229 % command. It parses its arguments (in particular separating the synonyms using the "xparse@@package"), and executes \cs{\knowledge_knowledge_process:nn}
2230 % \begin{macrocode}
2231 \NewDocumentCommand\__knowledge_knowledge_init:w{t! t+ m m}{
2232 \kl_hide_begin:
2233 \group_begin:
2234 %
2235 \IfBooleanTF{#1}{%
2236 \bool_set_false:N\knowledge_knowledge_append_bool
2237 \bool_set_true:N\knowledge_knowledge_clear_bool
2238 }%
2239 \IfBooleanTF{#2}{%
2240 \bool_set_true:N\knowledge_knowledge_append_bool
2241 \bool_set_false:N\knowledge_knowledge_clear_bool
2242 }%
2243 %
2244 \tl_set_eq:NN\knowledge_namespace_tl
2245 \knowledge_default_namespace_tl
2246 %
2247 \tl_gset:Nn\knowledge_knowledge_immediate_synonym_directives_tl{synonym}
2248 %
2249 \bool_gset_false:N\knowledge_knowledge_is_synonym_bool
2250 %
2251 \tl_if_eq:nnTF{#4}{|}
2252 {\__knowledge_knowledge_cont_barnotation:nw{#3}|}
2253 {\tl_if_eq:nnTF{#4}{[}
2254 {\__knowledge_knowledge_cont_classical:nw{#3}[}
2255 {\__knowledge_knowledge_cont_classical:nw{#3}{#4}}}
2256 }
2257 % \end{macrocode}
2258 % \begin{macrocode}
2259 \cs_new:Nn\__knowledge_knowledge_recallsynonym:n{
2260 \bool_if:NF\knowledge_knowledge_is_synonym_bool
2261 {\tl_gset:Nn\knowledge_knowledge_synonymlink_tl{#1}
2262 \tl_gset_eq:NN\knowledge_knowledge_synonyminstance_tl
2263 \knowledge_knowledge_instance_tl}
2264 }
2265 % \end{macrocode}
2266 % \begin{macrocode}
2267 \cs_new:Nn\__knowledge_knowledge_end:{
2268 \group_end:
2269 \kl_hide_end:
2270 }
2271 % \end{macrocode}
2272 % \begin{macrocode}
2273 \NewDocumentCommand\__knowledge_knowledge_cont_classical:nw{m >{\SplitList{|}}o m }{
2274 %\iow_term:n{\__knowledge_knowledge_cont_classical~{#1}{#3}}
2275 \knowledge_knowledge_setparseat:NNn\l_tmpa_tl\l_tmpb_tl{#1}
2276 %
2277 \use:x{
2278 \exp_not:N\knowledge_knowledge_process:nn
2279 {\exp_not:V\l_tmpa_tl}
2280 {\exp_not:V\knowledge_knowledgedefault_tl,
2281 \exp_not:n{#3},
2282 \tl_if_empty:NTF\l_tmpb_tl{}{,scope={\exp_not:V\l_tmpb_tl}}}}
2283 %
2284 \exp_args:NV\__knowledge_knowledge_recallsynonym:n\l_tmpa_tl
2285 %\tl_show:N\l_tmpa_tl
2286 %\tl_show:N\knowledge_knowledge_instance_tl
2287 %\tl_show:N\knowledge_knowledge_immediate_synonym_directives_tl
2288 %
2289 \IfNoValueTF{#2}{}{
2290 \cs_set:Nn\l_tmpa_cs:n
2291 {\exp_args:Nnx\knowledge_knowledge_process:nn{##1}
2292 {\exp_not:V\knowledge_knowledge_immediate_synonym_directives_tl}}
2293 \ProcessList{#2}\l_tmpa_cs:n
2294 }
2295 %
2296 \__knowledge_knowledge_end:
2297 }
2298 % \end{macrocode}
2299 % \AP The code for the "`\vbar'-notation".
2300 % \csintro{\knowledge_knowledge_barnotation_firstline:n} and \csintro{\knowledge_knowledge_barnotation_nextlines:n}
2301 % contain the code to be executed when the first line is read, then the following ones. This is defined during parsing.
2302 % \csintro{\knowledge_knowledge_barnotation_exec_line:n} changes from one value to the other during the parsing.
2303 % \begin{macrocode}
2304 \cs_new:Npn\knowledge_knowledge_barnotation_firstline:n{}
2305 % \end{macrocode}
2306 % \begin{macrocode}
2307 \cs_new:Npn\knowledge_knowledge_barnotation_nextlines:n{}
2308 % \end{macrocode}
2309 % \AP\csintro{\__knowledge_knowledge_cont_barnotation:nw} takes as first as argument the directives to be used for the
2310 % defining the first "knowledge" (first argument in the "`\vbar'-notation"), and then it absorbs the content of the following line,
2311 % from the first~`\vbar' to the line return.
2312 % \begin{macrocode}
2313 \cs_new:Npn\__knowledge_knowledge_cont_barnotation:nw#1{
2314 \cs_set:Nn\knowledge_knowledge_barnotation_firstline:n{
2315 %\knowledge_knowledge_setparseat\g_tmpa_tl\g_tmpb_tl{#1}
2316 %\iow_term:n{\knowledge{##1}{#1}~(first)}
2317 \knowledge{##1}{#1}
2318 \cs_set_eq:NN
2319 \knowledge_knowledge_barnotation_exec_line:n
2320 \knowledge_knowledge_barnotation_nextlines:n}
2321 \cs_set:Nn\knowledge_knowledge_barnotation_nextlines:n
2322 {%\iow_term:n{\exp_not:n{\knowledge{##1}}{synonym}}
2323 \knowledge{##1}{synonym}}
2324 \cs_set_eq:NN
2325 \knowledge_knowledge_barnotation_exec_line:n
2326 \knowledge_knowledge_barnotation_firstline:n
2327 \kl_newknowledge_parse:w
2328 }
2329 % \end{macrocode}
2330 % \AP Now the core of the parsing works by calling \csintro{\kl_newknowledge_parse:w}.
2331 % It tests the existence of a starting `\vbar', and in this case absorb the line
2332 % using \cs{\knowledge_knowledge_barnotation_exec_line:w}
2333 % and calls \cs{\knowledge_knowledge_barnotation_exec_line:n} to
2334 % execute it. This operation is repeated
2335 % until there are no more starting `\vbar', and in this case \cs{\__knowledge_knowledge_end:}
2336 % is used.
2337 % \begin{macrocode}
2338 \cs_new:Npn\kl_newknowledge_parse:w{
2339 \peek_charcode_remove_ignore_spaces:NTF|
2340 {\kl_insert_klendline:w\knowledge_knowledge_barnotation_exec_line:w}
2341 {\__knowledge_knowledge_end:}
2342 }
2343 % \end{macrocode}
2344 % \begin{macrocode}
2345 \cs_new:Npn\knowledge_knowledge_barnotation_exec_line:w#1\kl_end_line:{
2346 \knowledge_knowledge_barnotation_exec_line:n{#1}
2347 \kl_newknowledge_parse:w
2348 }
2349 % \end{macrocode}
2350 % The catcode modifications for parsing a single line in "`\vbar'-notation":
2351 % \AP The macro \csintro{\kl_insert_klendline:w} replaces the next occurrence of
2352 % a line return by the control sequence \csintro{\kl_end_line:}.
2353 % This temporarily changes the catcode. However, as soon as it is done, a normal command
2354 % (meaning using the standard catcode regime) can then absorb the line.
2355 % \begin{macrocode}
2356 \char_set_catcode_active:N\^^M
2357 \cs_gset:Npn\kl_insert_klendline:w{\group_begin:\catcode`\^^M=\active\kl_insert_klendline_:w}
2358 \cs_gset:Npn\kl_insert_klendline_:w#1^^M{\group_end:#1\kl_end_line:}
2359 \char_set_catcode_end_line:N\^^M
2360 % \end{macrocode}
2361 % \AP How to break a line from the "`\vbar'-notation" in pieces.
2362 % The \csintro{\knowledge_knowledge_setparseat:NNn}\
2363 % takes two tl-variables and the line content.
2364 % The result is that the part before the first `@' is put inside the first variable,
2365 % and what is after and before the next `@' (possibly empty) in the second one.
2366 % \begin{macrocode}
2367 \cs_new:Nn\knowledge_knowledge_setparseat:NNn
2368 {\knowledge_knowledge_setparseat_:NNw#1#2#3@@\kl_end:}
2369 \cs_new:Npn\knowledge_knowledge_setparseat_:NNw#1#2#3@#4@#5\kl_end:
2370 {\tl_set:Nx#1{\tl_trim_spaces:n{#3}}
2371 \tl_set:Nx#2{\tl_trim_spaces:n{#4}}}
2372 % \end{macrocode}
2373 %
2374 %\AP The boolean \cs{\knowledge_knowledgestyle_bool}
2375 % is set to true in order \cs{\knowledge_knowledge_process:nn}
2376 % to know if it has been triggered by \cs{\knowledge}
2377 % or \cs{\knowledgestyle}.
2378 % \begin{macrocode}
2379 \NewDocumentCommand\knowledgestyle{ s m m }
2380 { \kl_hide_begin:
2381 \group_begin:
2382 \tl_set:Nn\knowledge_namespace_tl{style}
2383 %
2384 \bool_set_true:N\knowledge_knowledgestyle_bool
2385 \IfBooleanTF{#1}
2386 {\bool_set_true:N\knowledge_knowledgestyle_override_bool}
2387 {\bool_set_false:N\knowledge_knowledgestyle_override_bool}
2388 %
2389 \knowledge_knowledge_process:nn{#2}{#3}
2390 \group_end:
2391 \kl_hide_end:
2392 }
2393 % \end{macrocode}
2394 %
2395 %
2396 %
2397 %
2398 %
2399 %\AP The \cs{\knowledge_knowledge_process:nn} command is the internal
2400 % version of \cs{\knowledge}. It takes two parameters:
2401 % the key, and the comma separated list of directives.
2402 % It uses internally the following `variables':
2403 % \begin{asparadesc}
2404 % \itemAP[\csintro{\knowledge_knowledgestyle_bool}] is it a \cs{\knowledgestyle} macro?
2405 % \itemAP[\csintro{\knowledge_knowledgestyle_override_bool}] has it been required to override a previous "knowledge"?
2406 % \itemAP[\csintro{\knowledge_knowledge_now_bool}] set if in the preamble or by the directive "now".
2407 % \itemAP[\csintro{\knowledge_knowledge_alsonow_bool}] set by directive "also now".
2408 % \itemAP[\csintro{\knowledge_knowledge_is_synonym_bool}] set by the "synonym" directive.
2409 % \itemAP[\csintro{\knowledge_knowledge_autoref_bool}] set by the "autoref" directive
2410 % \itemAP[\csintro{\knowledge_knowledge_clear_bool}]
2411 % \itemAP[\csintro{\knowledge_knowledge_append_bool}]
2412 % \itemAP[\csintro{\knowledge_knowledge_export_bool}]
2413 % \itemAP[\csintro{\knowledge_knowledge_synonymlink_tl}] name of the "knowledge" to link to if a "synonym" directive is used.
2414 % \itemAP[\csintro{\knowledge_knowledge_synonyminstance_tl}] "instance" of the "knowledge" to link if a "synonym" directive is used.
2415 % \itemAP[\csintro{\knowledge_knowledge_immediate_synonym_directives_tl}] gathers the "directives" that have to be propagated to implicit synonyms.
2416 % \itemAP[\csintro{\knowledge_knowledge_key_tl}] the text for which the "knowledge" has to be defined.
2417 % \itemAP[\csintro{\knowledge_knowledge_namespace_tl}] the "namespace".
2418 % \itemAP[\csintro{\knowledge_knowledge_instance_tl}]
2419 % \itemAP[\csintro{\knowledge_knowledge_directives_tl}] gathers the "directives" that have to be executed for defining the "knowledge" (either to be executed now if \cs{\knowledge_knowledge_now_bool} is set, or to be propagated to the "kaux file" for execution during the next compilation phase)
2420 % \end{asparadesc}
2421 % \begin{macrocode}
2422 \bool_new:N\knowledge_knowledgestyle_bool
2423 \bool_new:N\knowledge_knowledgestyle_override_bool
2424 \bool_new:N\knowledge_knowledge_now_bool
2425 \bool_new:N\knowledge_knowledge_alsonow_bool
2426 \bool_new:N\knowledge_knowledge_is_synonym_bool
2427 \bool_new:N\knowledge_knowledge_autoref_bool
2428 \bool_new:N\knowledge_knowledge_clear_bool
2429 \bool_new:N\knowledge_knowledge_append_bool
2430 \tl_clear_new:N\knowledge_knowledge_synonymlink_tl
2431 \tl_clear_new:N\knowledge_knowledge_synonyminstance_tl
2432 \tl_clear_new:N\knowledge_knowledge_immediate_synonym_directives_tl
2433 \tl_clear_new:N\knowledge_knowledge_export_bool
2434 \tl_clear_new:N\knowledge_knowledge_key_tl
2435 \tl_clear_new:N\knowledge_knowledge_namespace_tl
2436 \tl_clear_new:N\knowledge_knowledge_instance_tl
2437 \tl_clear_new:N\knowledge_knowledge_directives_tl
2438 % \end{macrocode}
2439 % The macro \csintro{\knowledge_knowledge_process:nn}
2440 % is the heart of \cs{\knowledge} command.
2441 % \begin{macrocode}
2442 \cs_new:Nn\knowledge_knowledge_process:nn
2443 { \bool_set_false:N\knowledge_knowledge_export_bool
2444 \bool_set_false:N\knowledge_knowledge_autoref_bool
2445 \bool_set_false:N\knowledge_knowledge_now_bool
2446 \bool_set_false:N\knowledge_knowledge_alsonow_bool
2447 %
2448 \tl_set:Nn\knowledge_knowledge_key_tl{#1}
2449 %
2450 \tl_set_eq:NN\knowledge_knowledge_namespace_tl\knowledge_namespace_tl
2451 \scope_get_attractive_instance:N\knowledge_knowledge_instance_tl
2452 %
2453 \tl_set:Nn\knowledge_knowledge_directives_tl{}
2454 %
2455 \keys_set:nn { knowledge~directives } { #2 }
2456 %
2457 \mode_if_preamble:TF
2458 {\bool_set_true:N\knowledge_knowledge_now_bool}
2459 {}
2460 %
2461 \bool_if:NT\knowledge_knowledge_clear_bool
2462 {\tl_put_left:Nn\knowledge_knowledge_directives_tl{clear,}}
2463 \bool_if:NT\knowledge_knowledge_append_bool
2464 {\tl_put_left:Nn\knowledge_knowledge_directives_tl{force,}}
2465 %
2466 \bool_if:nTF{\knowledge_knowledge_now_bool}
2467 {\use:x{
2468 \exp_not:N\knowledge_new_now:nnnn
2469 {\knowledge_knowledge_namespace_tl}
2470 {\knowledge_knowledge_instance_tl}
2471 {\exp_not:V\knowledge_knowledge_key_tl}
2472 {\exp_not:V\knowledge_knowledge_directives_tl}}
2473 }{}
2474 \bool_if:nTF{\knowledge_knowledge_alsonow_bool}
2475 {\use:x{
2476 \exp_not:N\knowledge_set_now:nnnn
2477 {\knowledge_knowledge_namespace_tl}
2478 {\knowledge_knowledge_instance_tl}
2479 {\exp_not:V\knowledge_knowledge_key_tl}
2480 {\exp_not:V\knowledge_knowledge_directives_tl}}
2481 }{}
2482 \bool_if:NTF\knowledge_knowledge_now_bool
2483 {}
2484 { \KAuxWriteX*{
2485 \exp_not:N\KAuxKnowledge
2486 {\knowledge_knowledge_namespace_tl}
2487 {\knowledge_knowledge_instance_tl}
2488 {\exp_not:V\knowledge_knowledge_key_tl}
2489 {\exp_not:V\knowledge_knowledge_directives_tl}}
2490 }
2491 \bool_if:NT\knowledge_knowledge_autoref_bool
2492 {\KAuxWriteX*
2493 {\exp_not:N\KAuxAutoref
2494 {\knowledge_knowledge_namespace_tl}
2495 {\knowledge_knowledge_instance_tl}
2496 {\exp_not:V\knowledge_knowledge_key_tl}}
2497 }
2498 %
2499 \bool_if:NT\knowledge_knowledge_export_bool
2500 {\KAuxWriteX*{
2501 \exp_not:N\KAuxExport
2502 {\knowledge_knowledge_namespace_tl}
2503 {\knowledge_knowledge_instance_tl}
2504 {\exp_not:V\knowledge_knowledge_key_tl}
2505 {\exp_not:V\knowledge_knowledge_directives_tl}}
2506 }
2507 }
2508 % \end{macrocode}
2509 % \begin{macrocode}
2510 \cs_new:Nn\knowledge_delay_directive:n
2511 {\tl_if_empty:NTF\knowledge_knowledge_directives_tl
2512 {\tl_set:Nn\knowledge_knowledge_directives_tl{#1}}
2513 {\tl_put_right:Nn\knowledge_knowledge_directives_tl{,#1}}}
2514 \cs_generate_variant:Nn\knowledge_delay_directive:n{x}
2515 % \end{macrocode}
2516 %
2517 %
2518 %
2519 % \subsubsection{The \cs{\knowledgedirective} command}
2520 %
2521 % \begin{macrocode}
2522 \NewDocumentCommand\knowledgedirective{smom}{
2523 \kl_hide_begin:
2524 \keys_if_exist:nnTF{ knowledge~directives }{ #2 }
2525 {\IfBooleanTF{#1}
2526 {\knowledgedirective:nnn{#2}{#3}{#4}}
2527 {\msg_error:nnn{ knowledge }{ knowledgedirective~redefinition }{#2}}}
2528 {\knowledgedirective:nnn{#2}{#3}{#4}}
2529 \kl_hide_end:
2530 }
2531 \cs_new:Nn\knowledgedirective:nnn{
2532 \cs_set:cn
2533 {knowledgedirective_#1:n}
2534 {\keys_set:nn{ knowledge~directives }{#3}}
2535 \keys_define:nn{ knowledge~directives }
2536 {#1 .code:n = {\use:c{knowledgedirective_#1:n}{##1}}}
2537 \IfNoValueF{#2}
2538 {\keys_define:nn{ knowledge~directives }{#1 .default:n = {#2}}}
2539 }
2540 % \end{macrocode}
2541 %
2542 % \subsubsection{The \cs{\knowledgedefault} command}
2543 %
2544 % It uses internally the variable \csintro{\knowledge_knowledgedefault_tl}.
2545 % \begin{macrocode}
2546 \tl_new:N\knowledge_knowledgedefault_tl
2547 \NewDocumentCommand\knowledgedefault{sm}{
2548 \kl_hide_begin:
2549 \IfBooleanTF{#1}
2550 {\tl_put_right:Nn\knowledge_knowledgedefault_tl{,#2}}
2551 {\tl_set:Nn\knowledge_knowledgedefault_tl{#2}}
2552 \kl_hide_end:
2553 }
2554 % \end{macrocode}
2555 %
2556 %
2557 % \subsubsection{Main \cs{\kl} code}
2558 %
2559 %
2560 % The \cs{\kl}, and its internal version \cs{\klTKS} are the central ones in the package. Indeed, all commands such as \cs{\intro}, \cs{\phantomintro}, \cs{\reintro} or the \kl{"...@...@..."} notation eventually end to a call to these functions.
2561 %\AP Essentially, \cs{\kl} parses its parameters, and then call its internal version \cs{\knowledge_klTKS:nnn}
2562 % with proper parameters: Text, Knowledge, Scope (the scope can be empty):
2563 %
2564 % \AP The macro \csintro{\knowledge_kl:w} is the part of \cs{\kl}-like commands that parses the arguments, and then calls \cs{\knowledge_klTKS:nnn} with proper arguments.
2565 % \begin{macrocode}
2566 \NewDocumentCommand\knowledge_kl:w{d() o d() m}{
2567 \IfNoValueTF{#1}
2568 {\IfNoValueTF{#3}
2569 {\IfNoValueTF{#2}
2570 {\knowledge_klTKS:nnn{#4}{#4}{}}
2571 {\knowledge_klTKS:nnn{#4}{#2}{}}}
2572 {\IfNoValueTF{#2}
2573 {\knowledge_klTKS:nnn{#4}{#4}{#3}}
2574 {\knowledge_klTKS:nnn{#4}{#2}{#3}}}}
2575 {\IfNoValueTF{#2}
2576 {\knowledge_klTKS:nnn{#4}{#4}{#1}}
2577 {\knowledge_klTKS:nnn{#4}{#2}{#1}}}
2578 }
2579 % \end{macrocode}
2580 %
2581 % \AP The following macro, \csintro{\knowledge_klTKS:nnn} is an internal version of \cs{\kl}-like commands.
2582 % It takes explicitly (1) the text to be displayed,
2583 % (2) the \kl{knowledge name} to be used for the search, and (3) a \kl{label} pointing to the \kl[instance]{instance of scope}
2584 % in which the \kl{knowledge name} has to be searched for. This \kl{label} can be empty, and in this case, this means that
2585 % the stack of instances that are visible in the context of the call of the command should be inspected for searching for
2586 % the knowledge.
2587 % \AP The macro \csintro{\klTKS} is a version of \cs{\knowledge_klTKS:nnn}
2588 % that can be used without the "expl3@@package" syntax.
2589 % \begin{macrocode}
2590 \tl_clear_new:N\knowledge_kl_text_tl
2591 \tl_clear_new:N\knowledge_kl_base_key_tl
2592 \tl_clear_new:N\knowledge_kl_key_tl
2593 \tl_clear_new:N\knowledge_kl_autoref_tl
2594 \tl_clear_new:N\knowledge_kl_resourcetoken_tl
2595 \tl_clear_new:N\knowledge_kl_resourcearguments_tl
2596 \tl_clear_new:N\knowledge_kl_display_process_tl
2597 \tl_clear_new:N\knowledge_kl_display_sequence_tl
2598 \tl_new:N\knowledge_namespace_tl
2599 \tl_gnew:Nn\knowledge_display_code_tl{INTERNAL~ERROR~DISPLAY~CODE}
2600 % \end{macrocode}
2601 % \begin{macrocode}
2602 \NewDocumentCommand\klTKS{}{\knowledge_klTKS:nnn}
2603 % \end{macrocode}
2604 % \begin{macrocode}
2605 \cs_new:Nn\knowledge_klTKS:nnn{
2606 %\iow_term:n{\knowledge_klTKS:nnn{#1}{#2}{#3}}
2607 \group_begin:
2608 %
2609 \tl_set:Nx\knowledge_kl_linkscope_tl{\tl_to_str:n{#3}}
2610 \tl_set:Nn\knowledge_kl_linkinstance_tl{}
2611 \tl_set:Nn\knowledge_kl_text_tl{#1}
2612 \tl_set:Nx\knowledge_kl_base_key_tl{\tl_to_str:n{#2}}
2613 \tl_set_eq:NN\knowledge_kl_key_tl\knowledge_kl_base_key_tl
2614 \int_set_eq:NN\knowledge_kl_recursion_int\knowledge_kl_max_recursion_int
2615 %\debug_vars:nn{klTKS:nnn}
2616 % {\knowledge_kl_linkscope_tl\knowledge_kl_linkinstance_tl
2617 % \knowledge_kl_text_tl\knowledge_kl_base_key_tl}
2618 %
2619 \tl_if_exist:cTF
2620 {knowledge_kl_\tl_to_str:V\knowledge_kl_modifiers_tl _init_tl}
2621 {\use:c{knowledge_kl_\tl_to_str:V\knowledge_kl_modifiers_tl _init_tl}}
2622 {\knowledge_error:x
2623 {Unknown~variant~modifier~sequence~`\exp_not:V\knowledge_kl_modifiers_tl'.}}
2624 %
2625 \knowledge_kl_modifiers_reset:
2626 %
2627 \knowledge_kl_use_styles:x{\knowledge_kl_default_style_tl}
2628 %
2629 \knowledge_kl_find:
2630 \group_end:
2631 \knowledge_display_code_tl
2632 }
2633 % \end{macrocode}
2634 %
2635 % At this step, the code has launched \cs{\knowledge_kl_find:}
2636 % Internally, it counts using \cs{\knowledge_kl_recursion_int}
2637 % the number of indirections, and bounds
2638 % starting from the integer \cs{\knowledge_kl_max_recursion_int}
2639 % (40 by default).
2640 % \begin{macrocode}
2641 \int_const:Nn\knowledge_kl_max_recursion_int{40}
2642 \int_new:N\knowledge_kl_recursion_int
2643 % \end{macrocode}
2644 % \noindent
2645 % and then launches the search.
2646 %
2647 %
2648 %\AP The core of the \cs{\kl} command is \csintro{\knowledge_kl_find:}.
2649 % It uses the variables \cs{\knowledge_kl_recursion_int} for checking non-recursion.
2650 % It searches for the entry \cs{\knowledge_kl_key_tl}, either in searching in the \kl{stack of visible scope instances},
2651 % or following \cs{\knowledge_kl_linkscope_tl} if the label exists.
2652 % It first works on whether a scope label is given or not:
2653 % \begin{macrocode}
2654 \cs_new:Nn\knowledge_kl_find:{
2655 \knowledge_kl_find_recursion_exceeded:TF
2656 \knowledge_kl_error_recursion:
2657 {\tl_if_empty:NTF\knowledge_kl_linkscope_tl
2658 {\tl_if_empty:NTF\knowledge_kl_linkinstance_tl
2659 {\knowledge_kl_find_withoutscope:}
2660 {\knowledge_kl_find_withinstance:}}
2661 \knowledge_kl_find_withscopetag:}
2662 }
2663 % \end{macrocode}
2664 % \begin{macrocode}
2665 \cs_set:Npn\knowledge_kl_find_recursion_exceeded:TF{
2666 \int_decr:N\knowledge_kl_recursion_int
2667 \int_compare:nNnTF\knowledge_kl_recursion_int=0
2668 }
2669 % \end{macrocode}
2670 % In case a scope label is provided, then the label is tested for being known; if it is the case, it is followed, and the knowledge is looked for and executed.
2671 % \begin{macrocode}
2672 \cs_new:Nn\knowledge_kl_find_withinstance:{
2673 \knowledge_knowledge_if_exist:VVVTF
2674 \knowledge_kl_namespace_tl
2675 \knowledge_kl_linkinstance_tl
2676 \knowledge_kl_key_tl
2677 {\knowledge_kl_exec:nnV
2678 \knowledge_kl_namespace_tl
2679 \knowledge_kl_linkinstance_tl
2680 \knowledge_kl_key_tl}
2681 { \knowledge_kl_error_unknown_in_scope_firsttime: }
2682 }
2683 % \end{macrocode}
2684 % \AP \csintro{\knowledge_kl_find_withscopetag:} searches for the knowledge, with a given "scopetag".
2685 % \begin{macrocode}
2686 \cs_new:Nn\knowledge_kl_find_withscopetag:{
2687 %\debug_vars:nn{\knowledge_kl_find_withscopetag:}{\knowledge_kl_linkscope_tl}
2688 \exp_args:NV\knowledge_scopetag_warn_if_unknown:n\knowledge_kl_linkscope_tl
2689 \tl_clear:N\l_tmpa_tl
2690 \exp_args:Nc\seq_map_function:NN
2691 {\knowledge_scopetag_tl[\knowledge_kl_linkscope_tl]_seq}
2692 \knowledge_kl_find_scopetag_tmpa:n
2693 % tests whether the search was successful
2694 \tl_if_empty:NTF\l_tmpa_tl
2695 {\knowledge_kl_error_unknown_in_scope_firsttime:}
2696 {\knowledge_kl_exec:VVV
2697 \knowledge_kl_namespace_tl
2698 \l_tmpa_tl
2699 \knowledge_kl_key_tl}
2700 }
2701 % \end{macrocode}
2702 % \AP \csintro{\knowledge_kl_find_scopetag:Nn} takes a tl variable for the result, and
2703 % a "scopetag" as argument. If the tl is empty, and a knowledge using "scopetag" as instance is found,
2704 % then the "scopetag" is transferred to the token list. \csintro{\knowledge_kl_find_scopetag_tmpa:n}
2705 % uses as variable \cs{\l_tmpa_tl}
2706 % \begin{macrocode}
2707 \cs_new:Npn\knowledge_kl_find_scopetag_tmpa:n{
2708 \knowledge_kl_find_scopetag:Nn\l_tmpa_tl
2709 }
2710 \cs_new:Nn\knowledge_kl_find_scopetag:Nn{
2711 \tl_if_empty:NTF#1
2712 {\knowledge_knowledge_if_exist:VnVTF
2713 \knowledge_kl_namespace_tl
2714 {#2}%instance
2715 \knowledge_kl_key_tl
2716 {\tl_set:Nn#1{#2}}
2717 {}}
2718 {}
2719 }
2720 % \end{macrocode}
2721 % \begin{macrocode}
2722 \cs_new:Nn\knowledge_kl_find_withoutscope:{
2723 %\debug_vars:nn{\knowledge_kl_find_withoutscope:}
2724 % {\knowledge_kl_key_tl\knowledge_kl_linkinstance_tl}
2725 %\exp_args:NVVV\knowledge_knowledge_if_exist:nnnTF
2726 % \knowledge_kl_namespace_tl\knowledge_kl_linkinstance_tl\knowledge_kl_key_tl
2727 % {\tl_show:n{found}}{\tl_show:n{not~found}}
2728 \knowledge_kl_find_in_scope:VNTF
2729 \knowledge_kl_key_tl
2730 \knowledge_kl_linkinstance_tl
2731 {\knowledge_kl_exec:VVV
2732 \knowledge_kl_namespace_tl
2733 \knowledge_kl_linkinstance_tl
2734 \knowledge_kl_key_tl}
2735 {\knowledge_kl_error_unknown_firsttime:}
2736 }
2737 % \end{macrocode}
2738 % \AP \csintro{\knowledge_kl_find_in_scope:nNTF} introduces the "namespace" as
2739 % first argument in a call to \cs{\knowledge_kl_find_in_scope:nnNTF}
2740 % \begin{macrocode}
2741 \cs_new:Npn\knowledge_kl_find_in_scope:nNTF{
2742 \exp_args:NV\knowledge_kl_find_in_scope:nnNTF
2743 \knowledge_kl_namespace_tl
2744 }
2745 % \end{macrocode}
2746 % \AP We now code the macro \csintro{\knowledge_kl_find_in_scope:nnNTF}, that
2747 % takes the \kl{namespace}, and the \kl{knowledge}, and answers the \kl{instance} (if it exists),
2748 % and sets the tl-token (third argument) to contain the scope in which it has been found.
2749 % \begin{macrocode}
2750 \cs_new:Nn\knowledge_kl_find_in_scope:nnNTF{
2751 \group_begin:
2752 %\tl_show:n{\knowledge_kl_find_in_scope:~#1/#2/#3}
2753 \tl_set:Nn\l_tmpa_tl{#1}
2754 \tl_set:Nn\l_tmpb_tl{#2}
2755 \bool_gset_false:N\g_tmpa_bool
2756 \seq_map_function:NN
2757 \knowledge_visible_instances_seq
2758 \knowledge_kl_find_in_scope_test:n
2759 \group_end:
2760 \bool_if:NTF\g_tmpa_bool
2761 {\tl_set_eq:NN#3\g_tmpa_tl #4}
2762 {#5}
2763 }
2764 % \end{macrocode}
2765 % \cs{\knowledge_kl_find_in_scope_test:n} tests the existence of a "knowledge"
2766 % in the "scopetag" given as argument. For this it searches through all the instances associated to the
2767 % "scopetag", and sets \cs{\g_tmpa_bool} when found.
2768 % One has to check that the visible instance is not the mark.
2769 % \begin{macrocode}
2770 \cs_new:Nn\knowledge_kl_find_in_scope_test:n{
2771 \tl_if_eq:nnTF{#1}{|}{}{
2772 \knowledge_kl_find_in_scope_test_:n{#1}
2773 \seq_if_exist:cTF{\knowledge_scopetag_tl[#1]_seq}
2774 {\exp_args:Nc\seq_map_function:NN
2775 {\knowledge_scopetag_tl[#1]_seq}
2776 \knowledge_kl_find_in_scope_test_:n
2777 }{}}
2778 }
2779 % \end{macrocode}
2780 % \begin{macrocode}
2781 \cs_new:Nn\knowledge_kl_find_in_scope_test_:n{
2782 \exp_args:Nc\ktuple_if_exist:NTF
2783 {\knowledge_id:nnV{\l_tmpa_tl}{#1}\l_tmpb_tl}
2784 {\bool_gset_true:N\g_tmpa_bool
2785 \tl_gset:Nn\g_tmpa_tl{#1}
2786 \seq_map_break:}{}
2787 }
2788 % \end{macrocode}
2789 % \begin{macrocode}
2790 \cs_generate_variant:Nn\knowledge_kl_find_in_scope:nnNTF{nVNTF}
2791 \cs_generate_variant:Nn\knowledge_kl_find_in_scope:nNTF{VNTF}
2792 % \end{macrocode}
2793 % \AP
2794 % When the \kl{knowledge} is found, it is eventually executed using
2795 % \begin{code}
2796 % \csintro{\knowledge_kl_exec:nnn}\{namespace\}\{instance\}\{key\}
2797 % \end{code}
2798 % The argument is a control sequence pointing to the \kl{knowledge}. It is first executed
2799 % in order to set the variables.
2800 % Then, the token of the \kl{knowledge} is stored in the variable \cs{\knowledge_kl_resourcetoken_tl}.
2801 % Then, if a \kl{style} is given, it is executed. The next step is to update the \kl{knowledge}
2802 % which means essentially updating its internal counters of use.
2803 % \begin{macrocode}
2804 \cs_new:Nn\knowledge_kl_exec:nnn{
2805 \tl_set:Nx\knowledge_kl_resourcearguments_tl
2806 {{#1}{#2}{\exp_not:n{#3}}}
2807 %\debug_vars:nn{\knowledge_kl_exec:nnn}{\knowledge_kl_resourcearguments_tl}
2808 \exp_args:NNc\tl_set:Nn\knowledge_kl_resourcetoken_tl
2809 {\knowledge_id:nnn{#1}{#2}{#3}}
2810 \expandafter\ktuple_countuse_gincr:N\knowledge_kl_resourcetoken_tl
2811 %
2812 \tl_clear:N\knowledge_kl_link_tl
2813 \tl_clear:N\knowledge_kl_linkscope_tl
2814 \tl_clear:N\knowledge_kl_linkinstance_tl
2815 \tl_map_function:NN
2816 \knowledge_kl_styledirectives_tl
2817 \tl_clear:N
2818 %
2819 \knowledge_kl_resourcetoken_tl
2820 %
2821 \bool_if:NTF\kl_unknown_knowledge_bool
2822 {\knowledge_kl_error_unknown:}
2823 {\tl_map_function:NN
2824 \knowledge_kl_styledirectives_tl
2825 \knowledge_kl_use_styles:x
2826 %
2827 \tl_if_exist_ne:NTF\knowledge_kl_linkscope_tl{
2828 \tl_if_exist_ne:NTF\knowledge_kl_link_tl{}{
2829 \tl_set_eq:NN\knowledge_kl_link_tl
2830 \knowledge_kl_key_tl
2831 }}{}
2832 %
2833 \tl_if_exist_ne:NTF\knowledge_kl_link_tl
2834 { \tl_set_eq:NN
2835 \knowledge_kl_key_tl
2836 \knowledge_kl_link_tl
2837 \knowledge_kl_find:}
2838 \knowledge_kl_compute_code:
2839 }
2840 }
2841 \cs_generate_variant:Nn\knowledge_kl_exec:nnn{nnV,VVV}
2842 % \end{macrocode}
2843 % \begin{macrocode}
2844 \cs_new:Nn\knowledge_kl_use_styles:n
2845 {\clist_map_function:nN{#1}\knowledge_kl_use_style:n}
2846 \cs_generate_variant:Nn\knowledge_kl_use_styles:n {x}
2847 % \end{macrocode}
2848 % \begin{macrocode}
2849 \cs_new:Nn\knowledge_kl_use_style:n{
2850 \tl_if_empty:nTF{#1}{}
2851 {\knowledge_kl_find_in_scope:nnNTF
2852 {style}
2853 {#1}
2854 \l_tmpa_tl
2855 {\use:c{\knowledge_id:nVn{style}\l_tmpa_tl{#1}}}
2856 {\knowledge_error:n{Unknown~style~`#1'}}}
2857 }
2858 % \end{macrocode}
2859 %
2860 % \begin{macrocode}
2861 \cs_new:Nn\knowledge_usestyle:nn{
2862 \group_begin:
2863 \tl_set:Nn\knowledge_kl_forcedtext_tl{#2}
2864 \knowledge_kl_use_styles:x{#1}
2865 \knowledge_kl_compute_code:
2866 \group_end:
2867 \knowledge_display_code_tl
2868 }
2869 \NewDocumentCommand\knowledgeusestyle{mm}{\knowledge_usestyle:nn{#1}{#2}}
2870 % \end{macrocode}
2871 %
2872 %
2873 %
2874 %
2875 % \subsubsection{Error handling}
2876 %
2877 %
2878 %
2879 % \begin{macrocode}
2880 \NewKAuxCommand\KAuxErrorKnowledgeRecursive{mmmm}
2881 {Package~'knowledge:~A~knowledge~has~recursive~definition.~
2882 {namespace}{scope}{undefined~knowledge}{base~knowledge}}
2883 {}
2884 \cs_new:Nn\knowledge_kl_error_recursion: {
2885 \msg_error:nnxxx{ knowledge }{ knowledge~recursion }
2886 {\exp_not:V\knowledge_kl_base_key_tl}
2887 {\exp_not:V\knowledge_kl_namespace_tl}
2888 {\exp_not:V\knowledge_kl_key_tl}
2889 \knowledge_kl_error_display:n{ recursive~knowledge }
2890 }
2891 % \end{macrocode}
2892 % \begin{macrocode}
2893 \NewKAuxCommand\KAuxErrorKnowledgeUnknown{mmmmm}
2894 {Package~'knowledge:~A~knowledge~is~not~defined.~
2895 {namespace}{scope/empty}{undefined~knowledge}{base~knowledge}{command}}
2896 {}
2897 \NewKAuxCommand\KAuxErrorLabelUnknown{m}
2898 {Package~'knowledge:~A~label~is~not~defined.~
2899 {label}}
2900 {}
2901 % \end{macrocode}
2902 % \begin{macrocode}
2903 \DeclareKAuxPhaseCommand\KAuxErrorKnowledgeUnknown{diagnose-unknown-suggest}
2904 { \knowledge_diagnose_line:
2905 \tl_set_eq:Nc\l_tmpa_tl{knowledge_kl_\cs_to_str:N#5_suggestion_tl}
2906 \tl_if_eq:nnTF{#1}{default}
2907 {}
2908 {\tl_if_empty:NTF\l_tmpa_tl{}{\tl_put_right:Nn\l_tmpa_tl{,}
2909 \tl_put_right:Nn\l_tmpa_tl{namespace={#1}}}}
2910
2911 \bool_if:NTF\knowledge_configuration_diagnosebar_bool{
2912 %\knowledge_diagnose:x{\char_escape_tl knowledge{\exp_not:V\l_tmpa_tl}}
2913 \tl_set:Nn\l_tmpa_tl{~~~|~~#3}
2914 \tl_if_empty:nTF{#2}{}
2915 {\tl_put_right:Nn\l_tmpa_tl{@#2}}
2916 \knowledge_diagnose:x{\exp_not:V\l_tmpa_tl}
2917 }{
2918 \tl_if_empty:nTF{#2}{}
2919 {\tl_if_empty:NTF\l_tmpa_tl{}{\tl_put_right:Nn\l_tmpa_tl{,}}
2920 \tl_put_right:Nn\l_tmpa_tl{scope={#2}}}
2921 \knowledge_diagnose:x
2922 {\char_escape_tl knowledge\exp_not:n{{#3}}{\exp_not:V\l_tmpa_tl}}
2923 }
2924 }
2925 % \end{macrocode}
2926 % The code executed when an undefined knowledge is met. It
2927 % \begin{macrocode}
2928 \cs_new:Nn\knowledge_kl_error_unknown_firsttime:
2929 {\bool_if:cTF{\knowledge_variant_var:n{unknownwarning_bool}}
2930 {\IfKnowledgePaperModeF{
2931 \knowledge_warning:nnxxx{ knowledge }{ knowledge~unknown }
2932 {\exp_not:V\knowledge_kl_key_tl}
2933 {\exp_not:V\knowledge_kl_namespace_tl}
2934 {\seq_use:Nn\knowledge_visible_instances_seq{|}}}
2935 }{}
2936 \bool_if:cTF{\knowledge_variant_var:n{unknowndiagnose_bool}}
2937 {\KAuxWriteX{
2938 \KAuxErrorKnowledgeUnknown
2939 {\knowledge_kl_namespace_tl}
2940 {}
2941 {\tl_to_str:V\knowledge_kl_key_tl}
2942 {\tl_to_str:V\knowledge_kl_base_key_tl}
2943 {\exp_not:V\knowledge_kl_command_tl}
2944 }}{}
2945 \use:x{\exp_not:N\knowledge_set_now:nnnn
2946 {\knowledge_kl_namespace_tl}{base}
2947 {\exp_not:V\knowledge_kl_key_tl}
2948 {UnKnoWn~KNowLedGE}}
2949 \tl_gset:Nx\knowledge_display_code_tl
2950 {\exp_not:N\knowledge_usestyle:nn
2951 {\use:c{\knowledge_variant_var:n{unknown_style_tl}}}
2952 {\exp_not:V\knowledge_kl_text_tl}}
2953 }
2954 \cs_new:Nn\knowledge_kl_error_unknown:{
2955 \tl_gset:Nx\knowledge_display_code_tl
2956 {\exp_not:N\knowledge_usestyle:nn
2957 {\use:c{\knowledge_variant_var:n{unknown_style_cont_tl}}}
2958 {\exp_not:V\knowledge_kl_text_tl}}
2959 }
2960 % \end{macrocode}
2961 % \begin{macrocode}
2962 \cs_new:Nn\knowledge_kl_error_unknown_in_scope_firsttime:
2963 {\bool_if:cTF{\knowledge_variant_var:n{unknownwarning_bool}}
2964 {\IfKnowledgePaperModeF{
2965 \knowledge_warning:nnxxx{ knowledge }{ knowledge~unknown~in~scope }
2966 {\exp_not:V\knowledge_kl_key_tl}
2967 {\exp_not:V\knowledge_kl_namespace_tl}
2968 {\knowledge_kl_linkscope_tl}}}{}
2969 \bool_if:cTF{\knowledge_variant_var:n{unknowndiagnose_bool}}
2970 {\KAuxWriteX{
2971 \KAuxErrorKnowledgeUnknown
2972 {\knowledge_kl_namespace_tl}
2973 {\knowledge_kl_linkscope_tl}
2974 {\tl_to_str:V\knowledge_kl_key_tl}
2975 {\tl_to_str:V\knowledge_kl_base_key_tl}
2976 {\exp_not:V\knowledge_kl_command_tl}
2977 }}{}
2978 \use:x{\exp_not:N\knowledge_set_now:nnnn
2979 {\knowledge_kl_namespace_tl}
2980 {\knowledge_kl_linkscope_tl}
2981 {\exp_not:V\knowledge_kl_key_tl}
2982 {UnKnoWn~KNowLedGE}}
2983 \tl_gset:Nx\knowledge_display_code_tl
2984 {\exp_not:N\knowledge_usestyle:nn
2985 {\use:c{\knowledge_variant_var:n{unknown_style_tl}}}
2986 {\exp_not:V\knowledge_kl_text_tl}}
2987 }
2988 % \end{macrocode}
2989 % \begin{macrocode}
2990 \cs_new:Nn\knowledge_kl_error_label_unknown:{
2991 \KAuxWriteX{
2992 \KAuxErrorKnowledgeUnknown
2993 {\knowledge_kl_namespace_tl}
2994 {\knowledge_kl_linkscope_tl}
2995 {\tl_to_str:V\knowledge_kl_key_tl}
2996 {\tl_to_str:V\knowledge_kl_base_key_tl}
2997 {\exp_not:V\knowledge_kl_command_tl}
2998 }
2999 %
3000 \IfKnowledgePaperModeF{
3001 \knowledge_warning:nnxx{ knowledge }{ knowledge~unknown~label }
3002 {\exp_not:V\knowledge_kl_key_tl}
3003 {\knowledge_kl_linkscope_tl}}
3004 %
3005 \tl_gset:Nx\knowledge_display_code_tl
3006 {\exp_not:N\knowledge_usestyle:nn
3007 {\use:c{\knowledge_variant_var:n{unknown_style_tl}}}
3008 {\exp_not:V\knowledge_kl_text_tl}}
3009 }
3010 % \end{macrocode}
3011 % This is the code use by default when an error is met and the result has nevertheless to be displayed.
3012 % \ToImprove{this code is dependent on xcolor and shouldn't.}
3013 % \begin{macrocode}
3014 \cs_new:Nn\knowledge_kl_error_display:n
3015 {\textcolor{orange}{\tl_to_str:V\knowledge_kl_text_tl}}
3016 % \end{macrocode}
3017 %
3018 % \subsubsection{Variant modifiers and \cs{\knowledgevariantmodifier}}
3019 %
3020 % When a \cs\kl-like command is met, its name is added to
3021 % \csintro{\knowledge_kl_modifiers_tl}.
3022 % Each time a new command is added using \csintro{\knowledge_kl_modifiers_add:N},
3023 % it is appended to \cs{\knowledge_kl_modifiers_tl}, and a reduction is applied
3024 % using \cs{\knowledge_kl_modifiers_reduce:}:
3025 % \begin{macrocode}
3026 \tl_clear_new:N\knowledge_kl_modifiers_tl
3027 \cs_new:Nn\knowledge_kl_modifiers_reset:
3028 {\tl_gclear:N\knowledge_kl_modifiers_tl}
3029 \cs_new:Nn\knowledge_kl_modifiers_add:N
3030 {\tl_gput_right:Nn\knowledge_kl_modifiers_tl{#1*}
3031 \knowledge_kl_modifiers_reduce:}
3032 % \end{macrocode}
3033 %\AP This reduction is performed by \csintro{\knowledge_kl_modifiers_reduce:},
3034 % and configured using \cs{\knowledgevariantmodifier}.
3035 % \begin{macrocode}
3036 \NewDocumentCommand\knowledgevariantmodifier{mm}
3037 {\tl_new:cn
3038 {KLSV_\tl_to_str:n{#1*} _tl}
3039 {#2*}}
3040 \cs_new:Nn\knowledge_kl_modifiers_reduce:
3041 {\tl_if_exist:cTF
3042 {KLSV_\tl_to_str:V\knowledge_kl_modifiers_tl _tl}
3043 {\tl_gset_eq:Nc\knowledge_kl_modifiers_tl
3044 {KLSV_\tl_to_str:V\knowledge_kl_modifiers_tl _tl}}
3045 {}}
3046 % \end{macrocode}
3047 %
3048 % \subsubsection{Implementation of \cs{\knowledgenewvariant}}
3049 %
3050 % \begin{macrocode}
3051 \cs_new:Nn\knowledge_variant_var:n
3052 {knowledge_kl_\expandafter\cs_to_str:N\knowledge_kl_command_tl _#1}
3053 % \end{macrocode}
3054 % \begin{macrocode}
3055 \NewDocumentCommand\knowledgenewvariant{mm}{
3056 \tl_if_exist:cTF{knowledge_kl_\cs_to_str:N#1_command_tl}
3057 {\knowledge_error:n{Variant~#1~already~exists~(in~\knowledgenewvariant).}}
3058 {\exp_args:Nc\tl_new:Nn
3059 {knowledge_kl_\cs_to_str:N#1_command_tl}
3060 {#1}
3061 %
3062 %
3063 \NewDocumentCommand#1{s}
3064 {\knowledge_kl_modifiers_add:N#1
3065 \IfBooleanF{##1}{\knowledge_kl:w}}
3066 %
3067 \tl_new_eq:cN
3068 {knowledge_kl_\cs_to_str:N#1_namespace_tl}
3069 \knowledge_default_namespace_tl
3070 \tl_new:cx{knowledge_kl_\cs_to_str:N#1_suggestion_tl}{}
3071 \bool_new_true:c{knowledge_kl_\cs_to_str:N#1_unknownwarning_bool}
3072 \bool_new_true:c{knowledge_kl_\cs_to_str:N#1_unknowndiagnose_bool}
3073 \tl_new:cn{knowledge_kl_\cs_to_str:N#1_default_style_tl}{}
3074 \tl_new:cn{knowledge_kl_\cs_to_str:N#1_unknown_style_tl}{}
3075 \tl_new:cn{knowledge_kl_\cs_to_str:N#1_unknown_style_cont_tl}{}
3076 \tl_new:cx{knowledge_kl_\cs_to_str:N#1_autoknowledge_tl}{}
3077 \tl_new:cx{knowledge_kl_\cs_to_str:N#1_styledirectives_tl}{}
3078 %
3079 \bool_if:NT\knowledge_hyperref_active_bool
3080 {\exp_args:Nx\pdfstringdefDisableCommands
3081 {\exp_not:N\def\exp_not:N#1
3082 {\exp_not:N\knowledge_variant_arg_absorber:Nw
3083 \exp_not:c{\cs_to_str:N#1_PDFstring}}}}
3084 \exp_args:Nc\newcommand{\cs_to_str:N#1_PDFstring}[3]{##1}
3085 %
3086 \tl_new:cx{knowledge_kl_\tl_to_str:n{#1*} _init_tl}
3087 {
3088 \exp_not:n{\tl_set_eq:NN\knowledge_kl_command_tl}
3089 \exp_not:c{knowledge_kl_\cs_to_str:N#1_command_tl}
3090 \exp_not:n{\tl_set_eq:NN\knowledge_kl_namespace_tl}
3091 \exp_not:c{knowledge_kl_\cs_to_str:N#1_namespace_tl}
3092 \exp_not:n{\tl_set_eq:NN\knowledge_kl_default_style_tl}
3093 \exp_not:c{knowledge_kl_\cs_to_str:N#1_default_style_tl}
3094 \exp_not:n{\tl_set_eq:NN\knowledge_kl_unknown_style_tl}
3095 \exp_not:c{knowledge_kl_\cs_to_str:N#1_unknown_style_tl}
3096 \exp_not:n{\tl_set_eq:NN\knowledge_kl_unknown_style_cont_tl}
3097 \exp_not:c{knowledge_kl_\cs_to_str:N#1_unknown_style_cont_tl}
3098 \exp_not:n{\tl_set_eq:NN\knowledge_kl_styledirectives_tl}
3099 \exp_not:c{knowledge_kl_\cs_to_str:N#1 _styledirectives_tl}
3100 \exp_not:n{\tl_set_eq:NN\knowledge_kl_display_process_tl}
3101 \exp_not:c{knowledge_kl_\cs_to_str:N#1 _display_process_tl}
3102 \exp_not:c{knowledge_kl_\cs_to_str:N#1 _autoknowledge_tl}
3103 }
3104 %
3105 %\bool_if:NT\knowledge_hyperref_active_bool
3106 % {\pdfstringdefDisableCommands{\let#1\knowledge_fake_kl:n}}
3107 %
3108 }
3109 \knowledgesetvariant#1{#2}
3110 }
3111 \DeclareExpandableDocumentCommand\knowledge_variant_arg_absorber:Nw
3112 {m d() o d() m}
3113 {\IfNoValueTF{#2}
3114 {#1{#5}{#2}{#3}}
3115 {#1{#5}{#2}{#1}}}
3116
3117 \tl_new:N\knowledge_variant_command_tl
3118 \tl_new:N\knowledge_variant_key_tl
3119 \NewDocumentCommand\knowledgesetvariant{mm}{
3120 \tl_if_exist:cTF{knowledge_kl_\cs_to_str:N#1_command_tl}
3121 {
3122 \tl_set:Nn\knowledge_variant_command_tl{#1}
3123 \tl_set:Nx\knowledge_variant_key_tl{\cs_to_str:N#1}
3124 %
3125 \keys_set:nn{ knowledge~configure~variant }{#2}
3126 }
3127 {\knowledge_error:n{Variant~#1~does~not~exist~(in~\knowledgesetvariant).}}
3128 }
3129 % \end{macrocode}
3130 % \begin{macrocode}
3131 \keys_define:nn{ knowledge~configure~variant }{
3132 namespace .code:n =
3133 {\tl_set:cn{knowledge_kl_\knowledge_variant_key_tl _namespace_tl}{#1}},
3134 %
3135 display~code .code:n =
3136 {\tl_set:cn{knowledge_kl_\knowledge_variant_key_tl _display_process_tl}{#1}},
3137 %
3138 unknown~warning .choices:nn = {true,false}
3139 {\bool_set_text:cn{knowledge_kl_\knowledge_variant_key_tl _unknownwarning_bool}{#1}},
3140 %
3141 unknown~diagnose .choices:nn = {true,false}
3142 {\bool_set_text:cn{knowledge_kl_\knowledge_variant_key_tl _unknowndiagnose_bool}{#1}},
3143 %
3144 default~style .code:n =
3145 {\tl_set:cn{knowledge_kl_\knowledge_variant_key_tl _default_style_tl}{#1}},
3146 unknown~style .code:n =
3147 {\tl_set:cn{knowledge_kl_\knowledge_variant_key_tl _unknown_style_tl}{#1}},
3148 unknown~style~cont .code:n =
3149 {\tl_set:cn{knowledge_kl_\knowledge_variant_key_tl _unknown_style_cont_tl}{#1}},
3150 %
3151 auto~knowledge .code:n =
3152 { \tl_set:cx{knowledge_kl_\knowledge_variant_key_tl _autoknowledge_tl}
3153 {\exp_not:N\exp_args:NV
3154 \exp_not:N\knowledge
3155 {\exp_not:N\knowledge_kl_key_tl}
3156 {namespace=\exp_not:N\knowledge_kl_namespace_tl,\exp_not:n{#1}}}},
3157 %
3158 style~directive .code:n = {
3159 \tl_clear:c{knowledge_kl_\knowledge_variant_key_tl _styledirectives_tl}
3160 \clist_map_inline:nn{#1}
3161 {\use:x{\NewKnowledgeParamTl*
3162 [\exp_not:c{knowledge_kl_##1_tl}]}
3163 {##1}
3164 \tl_put_right:cx{knowledge_kl_\knowledge_variant_key_tl _styledirectives_tl}
3165 {\exp_not:c{knowledge_kl_##1_tl}}}
3166 },
3167 %
3168 suggestion .code:n =
3169 {\tl_set:cn{knowledge_kl_\knowledge_variant_key_tl _suggestion_tl}{#1}},
3170 %
3171 PDF~string .code:n =
3172 {\exp_args:Nc\renewcommand{\knowledge_variant_key_tl _PDFstring}[3]{#1}}
3173 }
3174 % \end{macrocode}
3175 %
3176 %
3177 % \subsubsection{Variants of the \cs{\kl} macro}
3178 %
3179 %
3180 %
3181 % When defining a new use of \texttt{knowledge}, many macros have to be taken into account.
3182 % Some have to be defined explicitly. Some other have default behaviours.
3183 % \begin{asparaitem}
3184 % \itemAP \csintro{\knowledge_kl_display_preprocess_tl} is the code to be used first before displaying. It does nothing for \cs{\kl}, but is set to \cs{\knowledge_kl_target_preprocess_tl} for \cs{\intro}, which performs the \cs{\label} command fir the introduction.
3185 % \itemAP \csintro{\knowledge_kl_display_process_tl} is the code to be executed for displaying the \kl{knowledge}. This is not defined by default.
3186 % \itemAP \csintro{\knowledge_kl_error_unknown:} The fallback code to execute when the \kl{knowledge} is unknown.
3187 % By default, it displays a warning and put the name in orange.
3188 % \itemAP \csintro{\knowledge_kl_update:N} is a code to be executed to update the knowledge. By default, it increments the ktuple counter `\cs{countuse_int}'.
3189 % \itemAP \csintro{\knowledge_kl_namespace_tl} is the \kl{namespace} in which the \kl{knowledge} is to be searched. Not defined by default.
3190 % \itemAP \csintro{\knowledge_kl_linkscope_tl} is a label that points to an instance of a \kl{scope}. It can be empty, and in this case, this means that the search proceeds in the \kl{stack of visible scope instances}. By default it is empty.
3191 % \itemAP \csintro{\knowledge_kl_max_recursion_int} is the maximum number of recursion allowed when searching for a \kl{knowledge}. It is by default 40.
3192 % \end{asparaitem}
3193 %
3194 % The codes can uses internally the following variables, that are accessible for the user when defining new \cs\kl-like commands:
3195 % \begin{asparaitem}
3196 % \itemAP \csintro{\knowledge_kl_command_tl} the command that has triggered the search
3197 % \itemAP \csintro{\knowledge_kl_base_key_tl} contains the name of the knowledge for which the search has started,
3198 % \itemAP \csintro{\knowledge_kl_text_tl} contains the text to be displayed,
3199 % \itemAP \csintro{\knowledge_kl_key_tl} contains the name of the knowledge searched at a particular moment,
3200 % \itemAP \csintro{\knowledge_kl_recursion_int} counts the number of indirection in the current, starting from \cs{\knowledge_kl_max_recursion_int}, and going down,
3201 % \itemAP \csintro{\knowledge_kl_link_tl} contains the link to be followed.
3202 % \itemAP \cs{\knowledge_kl_resourcetoken_tl} contains a single token, which is the location of the knowledge.
3203 % \end{asparaitem}
3204 %
3205 % \begin{macrocode}
3206 \let\nointro\phantomintro
3207 % \end{macrocode}
3208 %
3209 % When the \cs{\intro} is used more than once, new labels are introduced (appending b,c,d\dots),
3210 % in order to point to these targets easily.
3211 % \begin{macrocode}
3212 \tl_new:Nn\knowledge_kl_target_preprocess_tl
3213 {\tl_if_exist_ne:NTF\knowledge_kl_resourcetoken_tl
3214 {\expandafter\ktuple_countintro_get_int:NN
3215 \knowledge_kl_resourcetoken_tl\l_tmpa_int
3216 \tl_set:Nx\l_tmpa_tl
3217 {\int_compare:nNnTF\l_tmpa_int=1{}{\int_to_alph:n\l_tmpa_int}}}
3218 {\tl_set:Nn\l_tmpa_tl{}}
3219 \tl_if_exist_ne:NTF\knowledge_kl_autoref_tl
3220 {\exp_args:Nx\knowledge_latex_label:n
3221 {\expandafter\noexpand\knowledge_kl_autoref_tl\l_tmpa_tl}}
3222 {}}
3223 % \end{macrocode}
3224 % \begin{macrocode}
3225 \tl_new:N\knowledge_kl_style_tl
3226 \tl_new:N\knowledge_kl_namespace_tl
3227 \tl_new:N\knowledge_kl_command_tl
3228
3229 \cs_new:Nn\knowledge_kl_target_update:N
3230 {\ktuple_countintro_gincr:N#1}
3231 \cs_gset:Nn\knowledge_kl_default_update:N
3232 {\ktuple_countuse_gincr:N#1}
3233 % \end{macrocode}
3234 %
3235 %
3236 % \subsubsection{Code for mathematical variables (unstable)}
3237 %
3238 %
3239 %
3240 % \subsubsection{Display parameters}
3241 %
3242 %
3243 %\begin{compactdesc}
3244 %\item[\csintro{\kl_emphasize_bool}]
3245 %\item[\csintro{\kl_boldface_bool}]
3246 %\item[\csintro{\kl_underline_bool}]
3247 %\item[\csintro{\kl_typewriter_bool}]
3248 %\item[\csintro{\kl_fbox_bool}]
3249 %\item[\csintro{\kl_ensuremath_bool}]
3250 %\item[\csintro{\kl_ensuretext_bool}]
3251 %\item[\csintro{\kl_italic_bool}]
3252 %\item[\csintro{\kl_lowercase_bool}]
3253 %\item[\csintro{\kl_uppercase_bool}]
3254 %\item[\csintro{\knowledge_kl_forcedtext_tl}]
3255 %\item[\csreintro{\knowledge_kl_link_tl}]
3256 %\item[\csreintro{\knowledge_kl_linkscope_tl}]
3257 %\item[\csreintro{\knowledge_kl_display_process_tl}]
3258 %\item[\csintro{\knowledge_kl_default_invisible_display_process_tl}]
3259 %\end{compactdesc}
3260 %
3261 % \begin{macrocode}
3262 \NewKnowledgeParamBool*{fbox}\kl_fbox_bool
3263 \NewKnowledgeParamBool*{ensuremath}\kl_ensuremath_bool
3264 \NewKnowledgeParamBool*{ensuretext}\kl_ensuretext_bool
3265 \NewKnowledgeParamBool*{mathord}\kl_mathord_bool
3266 \NewKnowledgeParamBool*{mathop}\kl_mathop_bool
3267 \NewKnowledgeParamBool*{mathbin}\kl_mathbin_bool
3268 \NewKnowledgeParamBool*{mathrel}\kl_mathrel_bool
3269 \NewKnowledgeParamBool*{mathopen}\kl_mathopen_bool
3270 \NewKnowledgeParamBool*{mathclose}\kl_mathclose_bool
3271 \NewKnowledgeParamBool*{mathpunct}\kl_mathpunct_bool
3272 \NewKnowledgeParamBool*{emphasize}\kl_emphasize_bool
3273 \NewKnowledgeParamBool*{underline}\kl_underline_bool
3274 \NewKnowledgeParamBool*{typewriter}\kl_typewriter_bool
3275 \NewKnowledgeParamBool*{autoref~target}\kl_autoreftarget_bool
3276 \NewKnowledgeParamBool*{autoref~link}\kl_autoreflink_bool
3277 \NewKnowledgeParamBool*{invisible}\kl_invisible_bool
3278 \NewKnowledgeParamBool*{italic}\kl_italic_bool
3279 \NewKnowledgeParamBool*{smallcaps}\kl_smallcaps_bool
3280 \NewKnowledgeParamBool*{md}\kl_md_bool
3281 \NewKnowledgeParamBool*{up}\kl_up_bool
3282 \NewKnowledgeParamBool*{boldface}\kl_boldface_bool
3283 \NewKnowledgeParamBool*{detokenize}\kl_detokenize_bool
3284 \NewKnowledgeParamBool*{remove~space}\kl_removespace_bool
3285 \NewKnowledgeParamBool*{protect~link}\kl_protect_link_bool
3286 \NewKnowledgeParamBool*{lowercase}\kl_lowercase_bool
3287 \NewKnowledgeParamBool*{uppercase}\kl_uppercase_bool
3288 % \end{macrocode}
3289 %
3290 % \begin{macrocode}
3291 \NewKnowledgeParamTl*{link}
3292 \NewKnowledgeParamTl*[\knowledge_kl_linkscope_tl]{link~scope}
3293 \NewKnowledgeParamTl*[\knowledge_kl_linkinstance_tl]{link~instance}
3294 \NewKnowledgeParamTl*[\knowledge_kl_forcedtext_tl]{text}
3295 \NewKnowledgeParamTl*{wrap}
3296 % \end{macrocode}
3297 %
3298 %
3299 % \begin{macrocode}
3300
3301 \NewKnowledgeParamBool*{UnKnoWn~KNowLedGE}\kl_unknown_knowledge_bool
3302 \bool_set_false:N\kl_unknown_knowledge_bool
3303 \ktuple_new_int_index:n{countuse}
3304 \ktuple_countuse_expands_to:n{}
3305 \ktuple_new_int_index:n{countintro}
3306 \ktuple_countintro_expands_to:n{}
3307 % \end{macrocode}
3308 %
3309 % The "directive" "now"
3310 % \begin{macrocode}
3311 \keys_define:nn{ knowledge~directives }{
3312 now .code:n = { \bool_set_true:N\knowledge_knowledge_now_bool },
3313 also~now .code:n = { \bool_set_true:N\knowledge_knowledge_alsonow_bool }
3314 }
3315 % \end{macrocode}
3316 %
3317 % The special directive \kl{autorefhere} is used for variables, where for which it is not necessary
3318 % to explicitely use a \cs{\knowledge} command.
3319 % \begin{macrocode}
3320 \int_new:N\knowledge_autoref_count
3321
3322 \keys_define:nn{ knowledge~directives }{
3323 autoref .code:n = {
3324 \int_gincr:N\knowledge_autoref_count
3325 \knowledge_delay_directive:x
3326 {autoref = {autoref-\int_use:N\knowledge_autoref_count}}
3327 \bool_set_true:N\knowledge_knowledge_autoref_bool},
3328 autorefhere .code:n = {
3329 \int_gincr:N\knowledge_autoref_count
3330 \exp_args:Nx\label{autoref-\int_use:N\knowledge_autoref_count}
3331 \knowledge_delay_directive:x
3332 {autoref = {autoref-\int_use:N\knowledge_autoref_count}}}}
3333
3334 \ktuple_new_tl_index:n{autoref}
3335 \use:c{ktuple_autoref_expands_to:n}
3336 {\tl_set:Nn\knowledge_kl_autoref_tl{#1} }
3337
3338 \keys_define:nn { knowledgenow~directives } {
3339 autoref .code:n =
3340 { \knowledge_at:c{ktuple_autoref_gset:Nn}{#1} }
3341 }
3342 % \end{macrocode}
3343 % \begin{macrocode}
3344 \int_new:N\kl_countuse_int
3345 \int_new:N\kl_countintro_int
3346 \NewKAuxCommand\KAuxAutoref{mmm}
3347 {Package~knowledge:~Declares~an~autoref~knowledge.~
3348 Arguments~{namespace}{instance}{knowledge}.}
3349 \NewKAuxCommand\KAuxAutorefTarget{mmm}
3350 {Package~knowledge:~Declares~the~use~of~a~target~to~an~autoref~knowledge.~
3351 Arguments~{namespace}{instance}{knowledge}.}
3352 \DeclareKAuxPhaseCommand\KAuxAutoref{init}{}
3353
3354 \int_new:N\kl_digest_autorefused_int
3355 \int_new:N\kl_digest_autorefnointro_int
3356 \int_new:N\kl_digest_autoreftwiceintro_int
3357 \int_new:N\kl_digest_autorefunused_int
3358 \int_new:N\kl_digest_unknownknowledge_int
3359 \int_new:N\kl_digest_unknownlabel_int
3360
3361 \DeclareKAuxPhaseCommand\KAuxErrorKnowledgeUnknown{diagnose-digest}
3362 {\int_gincr:N\kl_digest_unknownknowledge_int}
3363 %\DeclareKAuxPhaseCommand\KAuxErrorLabelUnknown{diagnose-digest}
3364 % {\int_gincr:N\kl_digest_unknownlabel_int}
3365
3366 \DeclareKAuxPhaseCommand\KAuxAutoref{diagnose-digest}
3367 {\group_begin:
3368 \knowledge_sets_to:nnn{#1}{#2}{#3}
3369 \knowledge_at:N\ktuple_countuse_get_int:NN\kl_countuse_int
3370 \knowledge_at:N\ktuple_countintro_get_int:NN\kl_countintro_int
3371 \int_compare:nNnT\kl_countintro_int=1
3372 {
3373 \int_compare:nNnTF\kl_countuse_int=1
3374 {\int_gincr:N\kl_digest_autorefunused_int}
3375 {\int_gincr:N\kl_digest_autorefused_int}
3376 }
3377 \int_compare:nNnT\kl_countintro_int=0
3378 {\int_compare:nNnF\kl_countuse_int=0
3379 %{\int_gincr:N\kl_digest_autorefunused_int}
3380 {\int_gincr:N\kl_digest_autorefnointro_int}}
3381 \int_compare:nNnT\kl_countintro_int>1
3382 {\int_gincr:N\kl_digest_autoreftwiceintro_int}
3383 \group_end:}
3384
3385 \DeclareKAuxPhaseCommand\KAuxAutoref{autoref-not-introduced}
3386 {\group_begin:
3387 \knowledge_sets_to:nnn{#1}{#2}{#3}
3388 \knowledge_at:N\ktuple_countuse_get_int:NN\kl_countuse_int
3389 \knowledge_at:N\ktuple_countintro_get_int:NN\kl_countintro_int
3390 \int_compare:nNnT\kl_countuse_int>0
3391 {\int_compare:nNnT\kl_countintro_int=0
3392 {\knowledge_diagnose_line:
3393 \knowledge_diagnose:x{\char_escape_tl nointro{\exp_not:n{#1}}{\exp_not:n{#2}}{\exp_not:n{#3}}}}}
3394 \group_end:}
3395 \DeclareKAuxPhaseCommand\KAuxAutoref{autoref-introduced-not-used}
3396 {\group_begin:
3397 \knowledge_sets_to:nnn{#1}{#2}{#3}
3398 \knowledge_at:N\ktuple_countuse_get_int:NN\kl_countuse_int
3399 \knowledge_at:N\ktuple_countintro_get_int:NN\kl_countintro_int
3400 \int_compare:nNnT\kl_countuse_int=1
3401 {\int_compare:nNnT\kl_countintro_int=1
3402 {\knowledge_diagnose_line:
3403 \knowledge_diagnose:x{\kaux_current_location:\space
3404 \exp_not:n{{#3}{#1}{#2}}}}}
3405 \group_end:}
3406 \DeclareKAuxPhaseCommand\KAuxAutorefTarget{autoref-introduced-twice}
3407 {\group_begin:
3408 \knowledge_sets_to:nnn{#1}{#2}{#3}
3409 \knowledge_at:N\ktuple_countuse_get_int:NN\kl_countuse_int
3410 \knowledge_at:N\ktuple_countintro_get_int:NN\kl_countintro_int
3411 \int_compare:nNnT\kl_countintro_int>1
3412 {\knowledge_diagnose_line:
3413 \knowledge_diagnose:x{\kaux_current_location:\space
3414 \exp_not:n{{#3}{#1}{#2}}}}
3415 \group_end:}
3416 % \end{macrocode}
3417 % \begin{macrocode}
3418 \NewKAuxCommand\KAuxUseKnowledge{mmm}
3419 {Package~knowledge:~Identifies~the~use~of~some~knowledge.~
3420 Arguments~{namespace}{instance}{knowledge}.}
3421 % \end{macrocode}
3422 %
3423 %
3424 %\subsubsection{Display code}
3425 %
3426 %
3427 %
3428 % \csintro{\knowledge_kl_display_generic:n} applies a code to the current displaycode, which is surrounded by braces.
3429 % \csintro{\knowledge_kl_display_generic_cond:Nn} does the same thing under condition
3430 % of a boolean (see code of \cs{\knowledge_kl_default_compute_code:}).
3431 % \begin{macrocode}
3432 \cs_new:Nn\knowledge_kl_display_generic:n
3433 {\tl_gset:Nx\knowledge_display_code_tl
3434 {\exp_not:n{#1}{\exp_not:V\knowledge_display_code_tl}}}
3435 \cs_new:Nn\knowledge_kl_display_generic_cond:Nn
3436 {\bool_if:NTF#1
3437 {\tl_gset:Nx\knowledge_display_code_tl
3438 {\exp_not:n{#2}{\exp_not:V\knowledge_display_code_tl
3439 }}}{}}
3440 \cs_generate_variant:Nn\knowledge_kl_display_generic:n{x}
3441 \cs_generate_variant:Nn\knowledge_kl_display_generic_cond:Nn{Nx}
3442 % \end{macrocode}
3443 % \AP \csintro{\knowledge_kl_compute_code:} tests whether a special display process is given, or otherwise
3444 % defaults to the standard one.
3445 % \begin{macrocode}
3446 \cs_new:Nn\knowledge_kl_compute_code:{
3447 \tl_if_exist_ne:NTF\knowledge_kl_display_process_tl
3448 \knowledge_kl_display_process_tl
3449 \knowledge_kl_default_compute_code:
3450 }
3451 % \end{macrocode}
3452 % \begin{macrocode}
3453 \cs_new:Nn\knowledge_kl_default_compute_code:{
3454 \tl_if_exist_ne:NTF\knowledge_kl_forcedtext_tl
3455 {\tl_gset_eq:NN\knowledge_display_code_tl\knowledge_kl_forcedtext_tl}
3456 {\tl_gset_eq:NN\knowledge_display_code_tl\knowledge_kl_text_tl}
3457 \bool_if:NT\kl_detokenize_bool
3458 {\tl_gset:Nx\knowledge_display_code_tl
3459 {\tl_to_str:V\knowledge_display_code_tl}}
3460 \bool_if:NT\kl_removespace_bool
3461 {\tl_remove_all:Nn\knowledge_display_code_tl{~}}
3462 \bool_if:NT\kl_lowercase_bool
3463 {\tl_gset:Nx\knowledge_display_code_tl
3464 {\exp_args:NV\tl_lower_case:n\knowledge_display_code_tl}}
3465 \bool_if:NT\kl_uppercase_bool
3466 {\tl_gset:Nx\knowledge_display_code_tl
3467 {\exp_args:NV\tl_upper_case:n\knowledge_display_code_tl}}
3468 %
3469 \tl_if_empty:NTF\knowledge_kl_wrap_tl
3470 {}
3471 {\exp_args:NV\knowledge_kl_display_generic:n\knowledge_kl_wrap_tl}
3472 %
3473 \bool_if:NT\knowledge_xcolor_active_bool
3474 \knowledge_kl_compute_code_xcolor:
3475 %
3476 \knowledge_kl_compute_code_hyperreftarget:
3477 %
3478 \bool_if:NT\knowledge_hyperref_active_bool
3479 \knowledge_kl_compute_code_hyperref:
3480 %
3481 \knowledge_kl_display_generic_cond:Nn\kl_underline_bool\kl_underline:n
3482 \knowledge_kl_display_generic_cond:Nn\kl_md_bool\kl_md:n
3483 \knowledge_kl_display_generic_cond:Nn\kl_boldface_bool\kl_bf:n
3484 \knowledge_kl_display_generic_cond:Nn\kl_up_bool\kl_up:n
3485 \knowledge_kl_display_generic_cond:Nn\kl_italic_bool\kl_it:n
3486 \knowledge_kl_display_generic_cond:Nn\kl_smallcaps_bool\kl_smallcaps:n
3487 \knowledge_kl_display_generic_cond:Nn\kl_typewriter_bool\kl_tt:n
3488 % if math
3489 \knowledge_kl_display_generic_cond:Nn\kl_mathop_bool\mathop
3490 \knowledge_kl_display_generic_cond:Nn\kl_mathord_bool\mathord
3491 \knowledge_kl_display_generic_cond:Nn\kl_mathbin_bool\mathbin
3492 \knowledge_kl_display_generic_cond:Nn\kl_mathrel_bool\mathrel
3493 \knowledge_kl_display_generic_cond:Nn\kl_mathclose_bool\mathclose
3494 \knowledge_kl_display_generic_cond:Nn\kl_mathopen_bool\mathopen
3495 \knowledge_kl_display_generic_cond:Nn\kl_mathpunct_bool\mathpunct
3496 % if text
3497 \knowledge_kl_display_generic_cond:Nn\kl_emphasize_bool\kl_em:n
3498 \knowledge_kl_display_generic_cond:Nn\kl_fbox_bool\fbox
3499 %
3500 \knowledge_kl_display_generic_cond:Nn\kl_ensuremath_bool\ensuremath
3501 \knowledge_kl_display_generic_cond:Nn\kl_ensuretext_bool\ensuretext
3502 %
3503 \bool_if:NT\knowledge_makeidx_active_bool
3504 \knowledge_kl_compute_code_makeidx:
3505 %
3506 \bool_if:NT\kl_invisible_bool
3507 {\tl_gclear:N\knowledge_display_code_tl}
3508 }
3509 \def\ensuretext#1{\ensuremath{\text{#1}}}
3510 % \end{macrocode}
3511 % \begin{macrocode}
3512 \cs_new:Nn\knowledge_kl_compute_code_xcolor:{
3513 \tl_if_empty:NF\knowledge_kl_color_tl
3514 {\knowledge_kl_display_generic:x{\exp_not:N\textcolor{\knowledge_kl_color_tl}}}
3515 \tl_if_empty:NF\knowledge_kl_colorbox_tl
3516 {\knowledge_kl_display_generic:x{\exp_not:N\colorbox{\knowledge_kl_colorbox_tl}}}
3517 }
3518 % \end{macrocode}
3519 % \begin{macrocode}
3520 \cs_new:Nn\knowledge_kl_compute_code_hyperref:{
3521 \tl_if_exist_ne:NT\knowledge_kl_ref_tl
3522 {\knowledge_kl_display_generic:x
3523 {\exp_not:N\kl_hyperlink_silentref:nn
3524 {\exp_not:V\knowledge_kl_ref_tl}}}
3525 %
3526 \bool_if:NT\kl_autoreflink_bool
3527 {\tl_if_exist_ne:NT\knowledge_kl_autoref_tl
3528 {\knowledge_kl_display_generic:x
3529 {\exp_not:N\kl_hyperlink_silentref:nn
3530 {\exp_not:V\knowledge_kl_autoref_tl}}
3531 }}
3532 %
3533 \tl_if_exist_ne:NT\knowledge_kl_url_tl
3534 {\knowledge_kl_display_generic:x
3535 {\exp_not:N\kl_hyperlink_url:nn
3536 {\exp_not:V\knowledge_kl_url_tl}}}
3537 }
3538 % \end{macrocode}
3539 % \begin{macrocode}
3540 \cs_new:Nn\knowledge_kl_compute_code_hyperreftarget:{
3541 \bool_if:NT\kl_autoreftarget_bool
3542 {\tl_if_exist_ne:NT\knowledge_kl_autoref_tl
3543 {\expandafter\ktuple_countintro_get_int:NN
3544 \knowledge_kl_resourcetoken_tl\l_tmpa_int
3545 \tl_set:Nx\l_tmpa_tl
3546 {\int_compare:nNnTF\l_tmpa_int=1
3547 {} {\int_to_alph:n\l_tmpa_int}}
3548 \exp_args:Nx\LaTeXlabel{\exp_not:V\knowledge_kl_autoref_tl\l_tmpa_tl}}
3549 \expandafter\ktuple_countintro_gincr:N
3550 \knowledge_kl_resourcetoken_tl
3551 \KAuxWriteX
3552 {\exp_not:N\KAuxAutorefTarget
3553 \exp_not:o\knowledge_kl_resourcearguments_tl}}
3554 }
3555 % \end{macrocode}
3556 % \begin{macrocode}
3557 \cs_new:Npn\kl_up:n{\mode_if_math:TF\use:n\textup}
3558 \cs_new:Npn\kl_md:n{\mode_if_math:TF\use:n\textmd}
3559 \cs_new:Npn\kl_sc:n{\mode_if_math:TF\mathsc\textsc}
3560 \cs_new:Npn\kl_it:n{\mode_if_math:TF\mathit\textit}
3561 \cs_new:Npn\mathscerror#1{\knowledge_error:n{`smallcaps'~only~allowed~in~math~mode.}}
3562 \cs_new:Npn\kl_smallcaps:n{\mode_if_math:TF\mathscerror\textsc}
3563 \cs_new:Npn\kl_tt:n{\mode_if_math:TF\mathtt\texttt}
3564 \cs_new:Npn\kl_bf:n{\mode_if_math:TF\mathbf\textbf}
3565 \cs_new:Npn\kl_underline:n{\mode_if_math:TF\use:n\underline}
3566 \cs_new:Npn\kl_em:n{\mode_if_math:TF\use:n\emph}
3567 % \end{macrocode}
3568 % \begin{macrocode}
3569 \cs_new:Nn\knowledge_kl_default_display_code_color:n{
3570 \bool_if:NTF\knowledge_xcolor_active_bool
3571 {\tl_if_exist_ne:NTF\knowledge_kl_color_tl
3572 {\exp_not:N\textcolor{\knowledge_kl_color_tl}{#1}}{#1}}
3573 {#1}}
3574 % \end{macrocode}
3575 % \begin{macrocode}
3576 \cs_new:Nn\knowledge_kl_default_display_code_colorbox:n{
3577 \bool_if:NTF\knowledge_xcolor_active_bool
3578 {\tl_if_exist_ne:NTF\knowledge_kl_colorbox_tl
3579 {\exp_not:N\colorbox{\knowledge_kl_colorbox_tl}{#1}}{#1}}
3580 {#1}}
3581 % \end{macrocode}
3582 % \begin{macrocode}
3583 \cs_new:Nn\knowledge_kl_default_code_nestingprotect:n{
3584 \bool_if:NTF\kl_protect_link_bool
3585 {\exp_not:N\kl_kl_nesting_incr:#1\exp_not:N\kl_kl_nesting_decr:}{#1}
3586 }
3587 % \end{macrocode}
3588 % \begin{macrocode}
3589 \cs_new:Nn\knowledge_kl_default_code_wrap:n{
3590 \tl_if_exist_ne:NTF\knowledge_kl_wrap_tl
3591 {\exp_not:V\knowledge_kl_wrap_tl{#1}}{#1}}
3592 % \end{macrocode}
3593 % \begin{macrocode}
3594 \cs_new:Nn\knowledge_kl_default_display_code_text:{
3595 \knowledge_kl_default_code_wrap:n{
3596 \tl_if_exist_ne:NTF\knowledge_kl_forcedtext_tl
3597 {\exp_not:V\knowledge_kl_forcedtext_tl}
3598 {\exp_not:V\knowledge_kl_text_tl}
3599 }
3600 }
3601 % \end{macrocode}
3602 %
3603 %
3604 % \subsection{Displaying labels}
3605 % Code for \cs{\kref}, \cs{\kpageref}, \cs{\kcref} and \cs{\kCref}.
3606 %
3607 % \csintro{\knowledgedisplayref} is to be used when defining a variant. It takes a macro, and creates the display code
3608 % which applies it to the label reference of the knowledge. See code of \cs{\kref}, \cs{\kcref}, \dots
3609 % \begin{macrocode}
3610 \cs_new:Nn\knowledge_labelref_display_process:n{
3611 \tl_if_exist_ne:NTF\knowledge_kl_ref_tl
3612 {\tl_gset:Nx\knowledge_display_code_tl
3613 {\exp_not:n{#1}
3614 {\exp_not:V\knowledge_kl_ref_tl}}
3615 }{
3616 \bool_if:NTF\kl_autoreflink_bool
3617 {\tl_if_exist_ne:NT\knowledge_kl_autoref_tl
3618 {\tl_gset:Nx\knowledge_display_code_tl
3619 {\exp_not:n{#1}
3620 {\exp_not:V\knowledge_kl_autoref_tl}}
3621 }
3622 }{ \tl_gset:Nn\knowledge_display_code_tl{???}
3623 }
3624 }
3625 }
3626 \cs_set_eq:NN\knowledgedisplayref\knowledge_labelref_display_process:n
3627 % \end{macrocode}
3628 %
3629 % \subsubsection{Other commands}
3630 %
3631 % \paragraph{\cs{\AP} and \cs{\itemAP}}
3632 % The code for the command \cs{\AP} is inspired from a
3633 % \href{http://tex.stackexchange.com/questions/123392/add-a-marker-to-the-left-of-the-text}{\underline{code}} of \kl{egreg} on \kl{stackexchange}.
3634 % \begin{macrocode}
3635 \NewDocumentCommand\knowledge_anchor_point_cs:{}
3636 {\bool_if:NT\knowledge_hyperref_active_bool
3637 {\strut\vadjust{\knowledge_AP_internal:}}}
3638 \NewDocumentCommand\itemAP{o}{
3639 \IfNoValueTF{#1}
3640 {\item\knowledge_anchor_point_cs:}
3641 {\item[\knowledge_itemAP: #1]}}
3642 \let\AP\knowledge_anchor_point_cs:
3643 % \end{macrocode}
3644 % \begin{macrocode}
3645 \int_new:N\kl_hyperref_kl_nesting_count_int
3646 \bool_new:N\kl_hyperref_active_here_bool
3647 \bool_set_true:N\kl_hyperref_active_here_bool
3648 \cs_new:Npn\kl_if_hyperlinks_active_here:TF
3649 {\bool_if:NTF\knowledge_hyperlinks_active_bool
3650 {\bool_if:NTF\kl_hyperref_active_here_bool}
3651 \use_ii:nn}
3652 \cs_new:Nn\kl_kl_nesting_incr:
3653 {\int_gincr:N\kl_hyperref_kl_nesting_count_int
3654 \bool_gset:Nn\kl_hyperref_active_here_bool
3655 {\int_compare_p:nNn\kl_hyperref_kl_nesting_count_int=0}}
3656 \cs_new:Nn\kl_kl_nesting_decr:
3657 {\int_gdecr:N\kl_hyperref_kl_nesting_count_int
3658 \bool_gset:Nn\kl_hyperref_active_here_bool
3659 {\int_compare_p:nNn\kl_hyperref_kl_nesting_count_int=0}}
3660 % \end{macrocode}
3661 % We make replicates of the commands of \kl(package){hyperref} that we want to sometimes deactivate.
3662 % \begin{macrocode}
3663 \cs_new:Npn\kl_hyperlink_ref:nn#1
3664 {\kl_if_hyperlinks_active_here:TF{\hyperref[{#1}]}{\use:n}}
3665 \cs_new:Npn\kl_hyperlink_url:nn
3666 {\kl_if_hyperlinks_active_here:TF\href\use_ii:nn}
3667 % \end{macrocode}
3668 % \begin{macrocode}
3669 \cs_new:Npn\kl_hyperlink_silentref:nn#1
3670 {\kl_if_hyperref_exist:nTF{#1}
3671 {\kl_hyperlink_ref:nn{#1}}
3672 \use:n}
3673 \cs_new:Npn\kl_if_hyperref_exist:nTF#1
3674 {\expandafter\ifx\csname r@#1\endcsname\relax
3675 \expandafter\use_ii:nn\else
3676 \expandafter\use_i:nn\fi}
3677 % \end{macrocode}
3678 % \begin{macrocode}
3679 \cs_new:Nn\knowledge_AP_internal:{%
3680 \vbox to 0pt{
3681 \noindent
3682 \kern-\dp\strutbox
3683 \smash{\llap{\knowledge_AP_mark:\kern0.3em}}
3684 \vss}}
3685 \cs_new:Nn\knowledge_AP_mark:{%
3686 \setlength\unitlength{1em}
3687 \begin{picture}(0,0)
3688 \put(0,1.1){\expandafter\put\knowledge_option_anchor_point_shift_tl{
3689 \bool_if:NT\knowledge_hyperref_active_bool
3690 {\put(-0.05,-1.18){\phantomsection}}
3691 \bool_if:NT\knowledge_configuration_visibleAP_bool
3692 {\bool_if:NTF\knowledge_xcolor_active_bool
3693 {\textcolor{\knowledge_option_anchor_point_color_tl}}{\use:n}
3694 {\knowledge_option_anchor_point_shape_tl}}}}
3695 \end{picture}{}}
3696 \cs_new:Nn\knowledge_itemAP:{
3697 \hbox_overlap_left:n
3698 {\knowledge_AP_mark:\hspace{1em}}
3699 }
3700 % \end{macrocode}
3701 %
3702 % \subsection{Activate and deactivate the \kl{quotation notation}}
3703 %
3704 %
3705 % We first define the mofifier sequences used for \cs{\kl*}, \cs{\intro*} and \cs{\hkl*},
3706 % namely \csintro{\knowledge_variant_quote_modifier_tl},
3707 % \csintro{\knowledge_variant_doublequote_modifier_tl} and \csintro{\knowledge_variant_highlight_quote_modifier_tl}:
3708 % \begin{macrocode}
3709 \tl_new:Nn\knowledge_variant_quote_modifier_tl{\kl*}
3710 \tl_new:Nn\knowledge_variant_doublequote_modifier_tl{\intro*}
3711 \tl_new:Nn\knowledge_variant_highlight_quote_modifier_tl{\hkl*}
3712 % \end{macrocode}
3713 % \knowledgeconfigure{quotation=false}
3714 % \AP We now define the macros \csintro{\makequotationactive} and \csintro{\makequotationletter} that change the catcode ot quotes.
3715 % \begin{macrocode}
3716 \ExplSyntaxOff
3717 \def\makequotationactive
3718 {\catcode`"13\relax}
3719 \def\makequotationletter
3720 {\catcode`"12\relax}
3721 \let\quotesymbol"
3722 \ExplSyntaxOn
3723 % \end{macrocode}
3724 % \AP The macro \csintro{\kl_parse_quote:w} is called when a quote is met and \kl(option){quotation} is active, and
3725 % the macro \csintro{\kl_parse_doublequote:w} is called when double quotes are met.
3726 % \AP Both rely on \csintro{\klquotation_parse_arobas_sep:Nw} for splitting the arguments.
3727 % In the end, either \cs{\klquote_expand:nnn} or \cs{\kldoublequote_expand:nnn} are called (or an error is issued with \cs{\knowledge_error_doublequote_simplequote:n} if the double quotes are not properly closed):
3728 % \begin{macrocode}
3729 \makequotationactive
3730 \cs_new:Npn\kl_parse_quote:w#1"{
3731 \tl_if_empty:nTF{#1}
3732 \kl_parse_doublequote:w
3733 {\klquotation_parse_arobas_sep:Nw
3734 \kl_quote_expand:nnn #1 @@@\kl_end:}
3735 }
3736 % \end{macrocode}
3737 % \begin{macrocode}
3738 \cs_new:Npn\kl_parse_doublequote:w#1"#2"{
3739 \tl_if_empty:nTF{#2}
3740 {\klquotation_parse_arobas_sep:Nw
3741 \kl_doublequote_expand:nnn #1 @@@\kl_end:}
3742 {\knowledge_error_doublequote_simplequote:n{#1}}
3743 }
3744 % \end{macrocode}
3745 % \begin{macrocode}
3746 \cs_new:Npx\klquotation_parse_arobas_sep:Nw #1#2@#3@#4@#5\kl_end:
3747 {#1{#2}{#3}{#4}}
3748 % \end{macrocode}
3749 % \AP Once the arguments collected \csintro{\kl_quote_expand:nnn} and \csintro{\kl_doublequote_expand:nnn} are called.
3750 % \cs{\kl_quote_expand:nnn} checks first if the first character is a space (and then calling \cs{\_klquote_char_space:nnn}),
3751 % then whether the first token \texttt{<tok>} triggers a special behaviour by looking at the macro \cs{\_klquote_char_<tok>:nnn}.
3752 % If it is not the case, it defaults to \cs{\knowledge_variant_quote_modifier_tl}\cs{\klquote_expand_base:nnn}.
3753 % \begin{macrocode}
3754 \cs_new:Npn\kl_quote_expand:nnn#1
3755 {\tl_if_head_is_space:nTF{#1@}
3756 {\_klquote_char_space:nnn}
3757 {\cs_if_exist_use:cTF{_klquote_char_\tl_to_str:n{\tl_head:n{#1@}}:nnn}
3758 {}
3759 {\knowledge_variant_quote_modifier_tl\kl_quote_expand_base:nnn}}
3760 {#1}
3761 }
3762 \cs_new:Npn\_klquote_char_space:nnn
3763 {\bool_if:NTF\knowledge_configuration_quotation_strict_bool
3764 {\knowledge_klquote_error_space:nnn}
3765 {\knowledge_variant_quote_modifier_tl\kl_quote_expand_base:nnn}
3766 }
3767 % \end{macrocode}
3768 % \AP \cs{\kldoublequote_expand:nnn} use a similar logic.
3769 % \begin{macrocode}
3770 \cs_new:Npn\kl_doublequote_expand:nnn#1
3771 {\tl_if_head_is_space:nTF{#1@}
3772 {\_kldoublequote_char_space:nnn}
3773 {\cs_if_exist_use:cTF{_kldoublequote_char_\tl_head:n{#1@}:nnn}
3774 {}
3775 {\knowledge_variant_doublequote_modifier_tl\kl_quote_expand_base:nnn}}
3776 {#1}
3777 }
3778 \cs_new:Npn\_kldoublequote_char_space:nnn
3779 {\bool_if:NTF\knowledge_configuration_quotation_strict_bool
3780 {\knowledge_kldoublequote_error_space:nnn}
3781 {\knowledge_variant_doublequote_modifier_tl\kl_quote_expand_base:nnn}
3782 }
3783 % \end{macrocode}
3784 % \AP The process converges toward \csintro{\kl_quote_expand_base:nnn} which
3785 % produces the suitable \cs{\klKTS} command, possibly duplicating parameters if needed.
3786 % The \csintro{\kl_quote_expand_base_gobble:nnn} does the same thing, but throws away the
3787 % first character of the first argument (to be used if a quote modifer symbol is used).
3788 % \begin{macrocode}
3789 \cs_new:Npn\kl_quote_expand_base:nnn#1#2
3790 {\tl_if_empty:nTF{#2}{\klTKS{#1}{#1}}{\klTKS{#1}{#2}}}
3791 \cs_new:Npn\kl_quote_expand_base_gobble:nnn#1
3792 {\exp_args:Nx\kl_quote_expand_base:nnn{\tl_tail:n{#1}}}
3793 % \end{macrocode}
3794 % \AP Let us now define the code that captures syntax error in quotes.
3795 % We first define the \kl(option){quotation strict} option, and link it to the
3796 % boolean \csintro{\knowledge_configuration_quotation_strict_bool}:
3797 % \begin{macrocode}
3798 \KnowledgeConfigureBooleanOption[\knowledge_configuration_quotation_strict_bool]
3799 {quotation~strict}
3800 \bool_set_true:N\knowledge_configuration_quotation_strict_bool
3801 % \end{macrocode}
3802 % \AP When a punctiation among ``\texttt{.,:;!?)}'' is seen after a quote,
3803 % one calls the macro \cs{\knowledge_klquotation_punctuation:nnn}.
3804 % \begin{macrocode}
3805 \cs_new:Nn\knowledge_new_quote_macro:NN
3806 {\cs_set:cpn{_klquote_char_\tl_to_str:n{#1}:nnn} {#2}}
3807 \knowledge_new_quote_macro:NN . \knowledge_klquote_punctuation:nnn
3808 \knowledge_new_quote_macro:NN , \knowledge_klquote_punctuation:nnn
3809 \knowledge_new_quote_macro:NN : \knowledge_klquote_punctuation:nnn
3810 \knowledge_new_quote_macro:NN ; \knowledge_klquote_punctuation:nnn
3811 \knowledge_new_quote_macro:NN ! \knowledge_klquote_punctuation:nnn
3812 \knowledge_new_quote_macro:NN ? \knowledge_klquote_punctuation:nnn
3813 \knowledge_new_quote_macro:NN ) \knowledge_klquote_punctuation:nnn
3814 \cs_new:Nn\knowledge_new_doublequote_macro:NN
3815 {\cs_set:cpn{_kldoublequote_char_#1:nnn} {#2}}
3816 \knowledge_new_doublequote_macro:NN . \knowledge_kldoublequote_punctuation:nnn
3817 \knowledge_new_doublequote_macro:NN , \knowledge_kldoublequote_punctuation:nnn
3818 \knowledge_new_doublequote_macro:NN : \knowledge_kldoublequote_punctuation:nnn
3819 \knowledge_new_doublequote_macro:NN ; \knowledge_kldoublequote_punctuation:nnn
3820 \knowledge_new_doublequote_macro:NN ! \knowledge_kldoublequote_punctuation:nnn
3821 \knowledge_new_doublequote_macro:NN ? \knowledge_kldoublequote_punctuation:nnn
3822 \knowledge_new_doublequote_macro:NN ) \knowledge_kldoublequote_punctuation:nnn
3823 % \end{macrocode}
3824 % \begin{macrocode}
3825 \NewDocumentCommand\knowledgequotemodifier{mm}
3826 {\cs_set:cpn{_klquote_char_{#1}:nnn}{#2\kl_quote_expand_base_gobble:nnn}}
3827 \NewDocumentCommand\knowledgedoublequotemodifier{mm}
3828 {\cs_set:cpn{_kldoublequote_char_#1:nnn}{#2\kl_quote_expand_base_gobble:nnn}}
3829 % \end{macrocode}
3830 % \AP And \csintro{\knowledge_klquotation_punctuation:nnn} either triggers an error
3831 % if \kl(option){quotation strict} is activated, or defaults back to the unmodified behaviour.
3832 % \begin{macrocode}
3833 \cs_new:Npn\knowledge_klquote_punctuation:nnn
3834 {\bool_if:NTF\knowledge_configuration_quotation_strict_bool
3835 \knowledge_klquote_error_punctuation:nnn
3836 {\knowledge_variant_quote_modifier_tl\kl_quote_expand_base:nnn}
3837 }
3838 \cs_new:Npn\knowledge_kldoublequote_punctuation:nnn
3839 {\bool_if:NTF\knowledge_configuration_quotation_strict_bool
3840 \knowledge_kldoublequote_error_punctuation:nnn
3841 {\knowledge_variant_doublequote_modifier_tl\kl_quote_expand_base:nnn}
3842 }
3843 % \end{macrocode}
3844 % \AP We now define all the error messages corresponding to quotations.
3845 % \begin{macrocode}
3846 \cs_new:Nn\knowledge_error_doublequote_simplequote:n
3847 {\knowledge_error:x
3848 {Closing~double~quote~missing\iow_newline:\str_range:nnn{""#1"}{-35}{-1}~<-}}
3849 \cs_new:Nn\knowledge_klquote_error_space:nnn
3850 {\knowledge_error:x
3851 {Forbidden~space~after~quote\iow_newline:->~\str_range:nnn{"#1}{1}{35}}}
3852 \cs_new:Nn\knowledge_kldoublequote_error_space:nnn
3853 {\knowledge_error:x
3854 {Forbidden~space~after~double~quote\iow_newline:->~\str_range:nnn{""#1}{1}{35}}}
3855 \cs_new:Nn\knowledge_klquote_error_punctuation:nnn
3856 {\knowledge_error:x
3857 {Forbidden~'\tl_head:n{#1}'~after~quote\iow_newline:->~\str_range:nnn{"#1}{1}{35}}}
3858 \cs_new:Nn\knowledge_kldoublequote_error_punctuation:nnn
3859 {\knowledge_error:x
3860 {Forbidden~'\tl_head:n{#1}'~after~quote\iow_newline:->~\str_range:nnn{""#1}{1}{35}}}
3861 % \end{macrocode}
3862 % Dealing with errors.
3863 % \begin{macrocode}
3864 \knowledgequotemodifier{#}{\knowledge_variant_highlight_quote_modifier_tl}
3865 % \end{macrocode}
3866 % \AP We finally define the macros \csintro{\klactivatequotation} and \csintro{\kldeactivatequotation}
3867 % that activates and devates the quotes.
3868 % \begin{macrocode}
3869 \def\klactivatequotation
3870 {\makequotationactive
3871 \let"\kl_parse_quote:w}
3872 \def\kldeactivatequotation
3873 {\makequotationletter}
3874 % \end{macrocode}
3875 % \begin{macrocode}
3876 \kldeactivatequotation
3877 % \end{macrocode}
3878 %\knowledgeconfigure{quotation=true}
3879 %
3880 % We now provide the code that is used for easily activating and deactivating quotation.
3881 % \begin{macrocode}
3882 \cs_new:Nn\kl_protect_environment_quotation:n
3883 {\clist_map_inline:nn{#1}
3884 {\scope_area_set:nn{#1}
3885 { push~code={\knowledgeconfigure{quotation=false}},
3886 pop~code={\knowledgeconfigure{quotation=true}}}}
3887 }
3888 % \end{macrocode}
3889 %
3890
3891 % \subsection{Mathematics}
3892 %
3893 % \begin{macrocode}
3894 \knowledgenewvariant\rekl{
3895 namespace= default,
3896 default~style= {autoref~link},
3897 unknown~style= kl~unknown,
3898 unknown~style~cont= kl~unknown~cont,
3899 style~directive= style
3900 }
3901 \knowledgevariantmodifier{\intro*\rekl}{\reintro}
3902 \knowledgevariantmodifier{\rekl*\intro}{\reintro}
3903 \knowledgevariantmodifier{\reintro*\rekl}{\reintro}
3904 \knowledgevariantmodifier{\rekl*\reintro}{\reintro}
3905 \knowledgevariantmodifier{\rekl*\kl}{\rekl}
3906 \knowledgevariantmodifier{\kl*\rekl}{\rekl}
3907 \knowledgevariantmodifier{\rekl*\rekl}{\rekl}
3908 \knowledgevariantmodifier{\phantomintro*\rekl}{\phantomkl}
3909 \knowledgevariantmodifier{\rekl*\phantomintro}{\phantomkl}
3910 \knowledgevariantmodifier{\phantomintro*\kl}{\phantomintro}
3911 \knowledgevariantmodifier{\phantomkl*\kl}{\phantomkl}
3912
3913 \knowledgedirective{automatic~in~command}{notion,also~now}
3914 % \end{macrocode}
3915 % \begin{macrocode}
3916
3917 \tl_gset:Nn\knowledge_inner_modifer_re_tl{\rekl*}
3918
3919 \int_new:N\knowledge_inner_modifier_count_int
3920 \cs_new:Nn\knowledge_inner_command:
3921 {knowledge_cmdkl \int_to_arabic:n\knowledge_inner_modifier_count_int :n}
3922
3923 \NewDocumentCommand\withkl{mm}{
3924 \int_gincr:N\knowledge_inner_modifier_count_int
3925 \cs_gset:cpx
3926 {\knowledge_inner_command:}
3927 {\exp_not:N\cs_gset:Npn
3928 \exp_not:c{\knowledge_inner_command:}
3929 {\knowledge_inner_modifer_re_tl\knowledge_kl_modifiers_tl\exp_not:n{#1}}
3930 \knowledge_kl_modifiers_tl\exp_not:n{#1}}
3931 \knowledge_kl_modifiers_reset:
3932 #2
3933 \int_gdecr:N\knowledge_inner_modifier_count_int
3934 }
3935
3936 \cs_new:Nn\knowledge_error_unexpected_cmdkl:{
3937 \knowledge_error:n{\cmdkl~is~only~allowed~inside~\withkl~or~in~macros~defined~using~\knowledgenewcommand-like~commands}
3938 }
3939 \newrobustcmd\cmdkl[1]{
3940 \int_compare:nNnTF\knowledge_inner_modifier_count_int=0
3941 \knowledge_error_unexpected_cmdkl:
3942 \use:c{\knowledge_inner_command:}{#1}
3943 }
3944
3945 % 1: command to extend, 2:, 3,4,5,6: the newcommand arguments (token sequence, optional star, first optional parameter, second optional parameter, and body of the definition).
3946 \NewDocumentCommand\knowledge_knowledgified_newcommand:NNwn{mmsoom}{
3947 \use:x{
3948 \exp_not:n{#1{#2}}
3949 \IfBooleanTF {#3} {*} {}
3950 \IfNoValueF{#4}{\exp_not:n{[{#4}]}}
3951 \IfNoValueF{#5}{\exp_not:n{[{#5}]}} }
3952 {\withkl{\kl[{#2}]}{#6}}
3953 \knowledge{#2}{automatic~in~command}
3954 }
3955 \NewDocumentCommand\knowledge_knowledgified_NewDocumentCommand:NNnn{mmmm}{
3956 #1#2{#3}{\withkl{\kl[{#2}]}{#4}}
3957 \knowledge{#2}{automatic~in~command}
3958 }
3959 \NewDocumentCommand\KnowledgifyNewcommand{m}{
3960 \cs_if_exist:NTF#1{
3961 \exp_args:Nc\NewDocumentCommand{knowledge\cs_to_str:N#1}{}
3962 {\knowledge_knowledgified_newcommand:NNwn#1}
3963 }{
3964 \use:x{\exp_not:N\NewDocumentCommand
3965 \exp_not:c{knowledge\cs_to_str:N#1}
3966 {}
3967 {\exp_not:N\knowledge_error:n{
3968 The~command~\exp_not:N#1~should~be~defined~for~\exp_not:c
3969 {knowledge\cs_to_str:N#1}~to~be~usable.}}
3970 }
3971 }
3972 }
3973 \NewDocumentCommand\KnowledgifyNewDocumentCommand{m}{
3974 \cs_if_exist:NTF#1{
3975 \exp_args:Nc\NewDocumentCommand{Knowledge\cs_to_str:N#1}{}
3976 {\knowledge_knowledgified_NewDocumentCommand:NNnn#1}
3977 }{
3978 %\tl_show:n{Unknown~#1~in~\KnowledgifyNewDocumentCommand}
3979 \use:x{\exp_not:N\NewDocumentCommand
3980 \exp_not:c{knowledge\cs_to_str:N#1}
3981 {}
3982 {\exp_not:N\knowledge_error:n{
3983 The~command~\exp_not:N#1~should~be~defined~for~\exp_not:c
3984 {Knowledge\cs_to_str:N#1}~to~be~usable.}}
3985 }
3986 }
3987 }
3988 % \end{macrocode}
3989 % Commands from LaTeX.
3990 % \begin{macrocode}
3991 \KnowledgifyNewcommand\newcommand
3992 \KnowledgifyNewcommand\renewcommand
3993 \KnowledgifyNewcommand\newrobustcmd
3994 \KnowledgifyNewcommand\renewrobustcmd
3995 \KnowledgifyNewcommand\providerobustcmd
3996 % \end{macrocode}
3997 % Commands from "xparse@@package".
3998 % \begin{macrocode}
3999 \KnowledgifyNewDocumentCommand\NewDocumentCommand
4000 \KnowledgifyNewDocumentCommand\RenewDocumentCommand
4001 \KnowledgifyNewDocumentCommand\ProvideDocumentCommand
4002 \KnowledgifyNewDocumentCommand\DeclareDocumentCommand
4003 % \end{macrocode}
4004 % Commands from "mathcommand@@package" (
4005 % \csintro\knowledgedeclarecommand,
4006 % \csintro\knowledgenewmathcommand,
4007 % \csintro\knowledgenewtextcommand,
4008 % \csintro\knowledgerenewmathcommand,
4009 % \csintro\knowledgerenewtextcommand,
4010 % \csintro\knowledgedeclaremathcommand,
4011 % \csintro\knowledgedeclaretextcommand,
4012 % \csintro\KnowledgeNewDocumentMathCommand,
4013 % \csintro\KnowledgeNewDocumentTextCommand,
4014 % \csintro\KnowledgeRenewDocumentMathCommand,
4015 % \csintro\KnowledgeRenewDocumentTextCommand,
4016 % \csintro\KnowledgeProvideDocumentMathCommand,
4017 % \csintro\KnowledgeProvideDocumentTextCommand,
4018 % \csintro\KnowledgeDeclareDocumentMathCommand,
4019 % \csintro\KnowledgeDeclareDocumentTextCommand,
4020 % \csintro\knowledgenewcommandPIE,
4021 % \csintro\knowledgerenewcommandPIE,
4022 % \csintro\knowledgedeclarecommandPIE,
4023 % \csintro\knowledgenewmathcommandPIE,
4024 % \csintro\knowledgerenewmathcommandPIE,
4025 % \csintro\knowledgedeclaremathcommandPIE,
4026 % \csintro\KnowledgeNewDocumentCommandPIE,
4027 % \csintro\KnowledgeRenewDocumentCommandPIE,
4028 % \csintro\KnowledgeDeclareDocumentCommandPIE,
4029 % \csintro\KnowledgeProvideDocumentCommandPIE,
4030 % \csintro\KnowledgeNewDocumentMathCommandPIE,
4031 % \csintro\KnowledgeRenewDocumentMathCommandPIE,
4032 % \csintro\KnowledgeDeclareDocumentMathCommandPIE, and
4033 % \csintro\KnowledgeProvideDocumentMathCommandPIE).
4034 % \begin{macrocode}
4035 \KnowledgifyNewcommand\declarecommand
4036 \KnowledgifyNewcommand\newmathcommand
4037 \KnowledgifyNewcommand\newtextcommand
4038 \KnowledgifyNewcommand\renewmathcommand
4039 \KnowledgifyNewcommand\renewtextcommand
4040 \KnowledgifyNewcommand\declaremathcommand
4041 \KnowledgifyNewcommand\declaretextcommand
4042 \KnowledgifyNewDocumentCommand\NewDocumentMathCommand
4043 \KnowledgifyNewDocumentCommand\NewDocumentTextCommand
4044 \KnowledgifyNewDocumentCommand\RenewDocumentMathCommand
4045 \KnowledgifyNewDocumentCommand\RenewDocumentTextCommand
4046 \KnowledgifyNewDocumentCommand\ProvideDocumentMathCommand
4047 \KnowledgifyNewDocumentCommand\ProvideDocumentTextCommand
4048 \KnowledgifyNewDocumentCommand\DeclareDocumentMathCommand
4049 \KnowledgifyNewDocumentCommand\DeclareDocumentTextCommand
4050 \KnowledgifyNewcommand\newcommandPIE
4051 \KnowledgifyNewcommand\renewcommandPIE
4052 \KnowledgifyNewcommand\declarecommandPIE
4053 \KnowledgifyNewcommand\newmathcommandPIE
4054 \KnowledgifyNewcommand\renewmathcommandPIE
4055 \KnowledgifyNewcommand\declaremathcommandPIE
4056 \KnowledgifyNewDocumentCommand\NewDocumentCommandPIE
4057 \KnowledgifyNewDocumentCommand\RenewDocumentCommandPIE
4058 \KnowledgifyNewDocumentCommand\DeclareDocumentCommandPIE
4059 \KnowledgifyNewDocumentCommand\ProvideDocumentCommandPIE
4060 \KnowledgifyNewDocumentCommand\NewDocumentMathCommandPIE
4061 \KnowledgifyNewDocumentCommand\RenewDocumentMathCommandPIE
4062 \KnowledgifyNewDocumentCommand\DeclareDocumentMathCommandPIE
4063 \KnowledgifyNewDocumentCommand\ProvideDocumentMathCommandPIE
4064 % \end{macrocode}
4065 % \subsection{Subpackages}
4066 %
4067 % \subsubsection{Generalities}
4068 %
4069 % \begin{macrocode}
4070 \cs_new:Nn\knowledge_directives_require_aci_package:nn
4071 {\bool_if:cTF{knowledge_#2_syntax_bool}{}{
4072 \NewKnowledgeParamPackageError{#1}{#2}}}
4073 % \end{macrocode}
4074 %
4075 %
4076 % \subsubsection{The \kl{xcolor option}}
4077 %
4078 % \begin{macrocode}
4079 \NewKnowledgeParamTl*{color}
4080 \NewKnowledgeParamTl*{colorbox}
4081 \knowledge_directives_require_aci_package:nn
4082 {color,colorbox,cyclic~color}{xcolor}
4083 % \end{macrocode}
4084 %
4085 % \begin{macrocode}
4086 \tl_new:N\knowledge_cyclic_colors_tl
4087 \tl_new:N\knowledge_cyclic_color_tl
4088 % \end{macrocode}
4089 %
4090 % \begin{macrocode}
4091 \keys_define:nn{ knowledge~configuration~options }{
4092 cyclic~colors .code:n = {\tl_gset:Nn\knowledge_cyclic_colors_tl{#1}}
4093 }
4094 \knowledgeconfigure{cyclic~colors =
4095 {red,green,blue,cyan,magenta,yellow,gray,brown,lime,olive,orange,pink,purple,teal,violet}}
4096 % \end{macrocode}
4097 %
4098 % \begin{macrocode}
4099 \cs_new:Npn\__knowledge_cyclic_color_next:w#1,#2\__knowledge_cyclic_end
4100 {\tl_gset:Nn\knowledge_cyclic_colors_tl{#2,{#1}}
4101 \tl_gset:Nn\knowledge_cyclic_color_tl{#1}}
4102 \cs_new:Nn\knowledge_cycle_colors:
4103 {\expandafter\__knowledge_cyclic_color_next:w\knowledge_cyclic_colors_tl\__knowledge_cyclic_end}
4104
4105 \keys_define:nn{ knowledge~directives }{
4106 cyclic~color .code:n = {
4107 \knowledge_cycle_colors:
4108 \knowledge_delay_directive:x
4109 {color = {\knowledge_cyclic_color_tl}}}
4110 }
4111 % \end{macrocode}
4112 %
4113 % \subsubsection{The \kl{hyperref option}}
4114 %
4115 % \begin{macrocode}
4116 \NewKnowledgeParamTl*{url}
4117 \NewKnowledgeParamTl*{ref}
4118 \knowledge_directives_require_aci_package:nn
4119 {url}{hyperref}
4120 % \end{macrocode}
4121 % The commands \cs{\intro}, \cs{\kl} and \cs{\reintro} are made silent when
4122 % creating the pdf names of sections (for avoiding warnings of the "hyperref@@package").
4123 % \begin{macrocode}
4124 \bool_if:NT\knowledge_hyperref_active_bool{
4125 \DeclareExpandableDocumentCommand\knowledge_fake_klTKS{mmm}{#1}
4126 \pdfstringdefDisableCommands{%
4127 \let\klTKS\knowledge_fake_klTKS
4128 }
4129 }
4130 % \end{macrocode}
4131 %
4132 %
4133 % \subsubsection{The \kl{makeidx option} and \kl(option){imakeidx} options}
4134 % Since "imakeidx@@package" extends the syntax of "makeidx@@package",
4135 % internally, activating the package or the syntax for "imakeidx@@package"
4136 % does the same thing for "makeidx@@package".
4137 % \begin{macrocode}
4138 \bool_if:NT\knowledge_imakeidx_active_bool{\bool_set_true:N\knowledge_makeidx_active_bool}
4139 \bool_if:NT\knowledge_imakeidx_syntax_bool{\bool_set_true:N\knowledge_makeidx_syntax_bool}
4140 % \end{macrocode}
4141 % The index option has to variant, depending on whether the name of the index is used or not.
4142 % \begin{macrocode}
4143 \cs_new:Npn \knowledge_index:nn #1 #2{
4144 \tl_if_empty:nTF {#1}
4145 {\index{#2}}
4146 {\index[{#1}]{#2}}
4147 }
4148 \cs_generate_variant:Nn \knowledge_index:nn {xx}
4149 % \end{macrocode}
4150 % \begin{macrocode}
4151 \cs_new:Nn\knowledge_kl_compute_code_makeidx:{
4152 \bool_if:NF\knowledge_kl_noindex_bool{
4153 \tl_if_exist_ne:NTF\knowledge_kl_index_tl
4154 {\knowledge_index:xx
4155 {\tl_if_exist_ne:NTF \knowledge_kl_index_name_tl
4156 {\exp_not:V \knowledge_kl_index_name_tl}
4157 {}
4158 }
4159 {\tl_if_exist_ne:NTF\knowledge_kl_index_parent_key_tl
4160 {\exp_not:V\knowledge_kl_index_parent_key_tl!}
4161 {}
4162 \tl_if_exist_ne:NTF\knowledge_kl_index_key_tl
4163 {\exp_not:V\knowledge_kl_index_key_tl@}
4164 {}
4165 \exp_not:V\knowledge_kl_index_tl
4166 |
4167 \tl_if_exist_ne:NTF\knowledge_kl_index_modifier_tl
4168 {\exp_not:V\knowledge_kl_index_modifier_tl}
4169 {}
4170 \tl_if_exist_ne:NTF\knowledge_kl_index_number_style_tl
4171 {\exp_not:V\knowledge_kl_index_number_style_tl}
4172 {}
4173 }}{}
4174 }
4175 }
4176 % \end{macrocode}
4177 % \begin{macrocode}
4178 \def\knowledgeIntroIndexStyle#1{\textbf{#1}}
4179 \NewKnowledgeParamTl{index}
4180 \keys_define:nn { knowledge~directives }
4181 { index .code:n = {
4182 \tl_if_empty:nTF{#1}
4183 {\knowledge_delay_directive:x{index={\exp_not:V\knowledge_knowledge_key_tl}}}
4184 {\knowledge_delay_directive:n{index={#1}}}},
4185 index .default:n = {}
4186 }
4187
4188 \bool_if:NT\knowledge_makeidx_syntax_bool
4189 { \NewKnowledgeParamTl*[\knowledge_kl_index_number_style_tl]{index~style}
4190 \NewKnowledgeParamTl*[\knowledge_kl_index_parent_key_tl]{index~parent~key}
4191 \NewKnowledgeParamTl*[\knowledge_kl_index_key_tl]{index~key}
4192 \NewKnowledgeParamBool*{no~index}\knowledge_kl_noindex_bool
4193 \NewKnowledgeParamTl*[\knowledge_kl_index_modifier_tl]{index~modifier}
4194 }
4195 \bool_if:NT\knowledge_imakeidx_syntax_bool {
4196 \NewKnowledgeParamTl*[\knowledge_kl_index_name_tl]{index~name}
4197 }
4198 % \end{macrocode}
4199 % Activating error message is packages not loaded.
4200 % \begin{macrocode}
4201 \knowledge_directives_require_aci_package:nn
4202 {index,index~key,index~parent~key,index~style,index~modifier}{makeidx}
4203 \knowledge_directives_require_aci_package:nn
4204 {index~name}{imakeidx}
4205 % \end{macrocode}
4206 %
4207 %% \AP The "styles" ""debug index invisible@@style"", ""debug index from@@style"",
4208 % ""debug index to@@style"" are used to display index-related commands when the "configuration option" "debug index@@option" is set,
4209 % and ""index unknown@@style"" is used similarly when an error is met (in combination with "kl unknown@@style").
4210 % These styles wrap the knowledge in one of \csintro\knowledgeDisplayDebugIndex,
4211 % \csintro\knowledgeDisplayDebugIndexFrom, \csintro\knowledgeDisplayDebugIndexTo, and \csintro\knowledgeDisplayIndexUnknown.
4212 % Note that these "styles" and macros are not described in the main documentation, and a priori should not be referred to by the user.
4213 % \begin{macrocode}
4214 \bool_if:NT\knowledge_makeidx_syntax_bool{
4215 \NewDocumentCommand\knowledgeDisplayDebugIndex{m}{(index:#1)}
4216 \NewDocumentCommand\knowledgeDisplayDebugIndexFrom{m}{(index:#1(}
4217 \NewDocumentCommand\knowledgeDisplayDebugIndexTo{m}{)index:#1)}
4218 \NewDocumentCommand\knowledgeDisplayIndexUnknown{m}{[index?:#1]}
4219 \knowledgestyle{debug~index}{wrap=\knowledgeDisplayDebugIndex}
4220 \knowledgestyle{debug~index~from}{wrap=\knowledgeDisplayDebugIndexFrom,index~modifier={(}}
4221 \knowledgestyle{debug~index~to}{wrap=\knowledgeDisplayDebugIndexTo,index~modifier={)}}
4222 \knowledgestyle{index~unknown}{no~index,wrap=\knowledgeDisplayIndexUnknown}
4223 }
4224 % \end{macrocode}
4225 % \AP The "styles" ""debug index visibility@@style"" and ""debug index unknown visibility@@style""
4226 % are hooks for displaying or not the indexing commands. These are to be changed by the user using the "configuration directive" "debug index".
4227 % \begin{macrocode}
4228 \bool_if:NT\knowledge_makeidx_syntax_bool{
4229 \knowledgestyle{debug~index~visibility}{invisible=true}
4230 \knowledgestyle{debug~index~unknown~visibility}{invisible=true}
4231 \KnowledgeConfigureBooleanOptionTF{debug~index}
4232 {\IfKnowledgeCompositionModeTF
4233 {\knowledgestyle{debug~index~visibility}{invisible=false}
4234 \knowledgestyle{debug~index~unknown~visibility}{invisible=false}}
4235 {}}
4236 {\knowledgestyle{debug~index~visibility}{invisible=true}
4237 \knowledgestyle{debug~index~unknown~visibility}{invisible=true}}
4238 }
4239 % \end{macrocode}
4240 %
4241 % We define now the new variants \cs\kindex, \cs\kindexfrom, and \cs\kindexto, as well as the associated configuration.
4242 % Essentially it amounts to change the variable \cs{\knowledge_kl_index_modifier_tl} that, if defined, modify the behaviour of indexing.
4243 % \begin{macrocode}
4244 \bool_if:NT\knowledge_makeidx_syntax_bool{
4245 \knowledgenewvariant\kindex{
4246 namespace= default,
4247 default~style={composition,debug~index,debug~index~visibility},
4248 unknown~style={kl~unknown,index~unknown,debug~index~unknown~visibility},
4249 unknown~style~cont={kl~unknown~cont,index~unknown,debug~index~unknown~visibility},
4250 style~directive=kindex~style
4251 }
4252 \knowledgenewvariant\kindexfrom{
4253 namespace= default,
4254 default~style={composition,debug~index~from,debug~index~visibility},
4255 unknown~style={kl~unknown,index~unknown,debug~index~unknown~visibility},
4256 unknown~style~cont={kl~unknown~cont,index~unknown,debug~index~unknown~visibility},
4257 style~directive=kindex~style
4258 }
4259 \knowledgenewvariant\kindexto{
4260 namespace= default,
4261 default~style={composition,debug~index~to,debug~index~visibility},
4262 unknown~style={kl~unknown,index~unknown,debug~index~unknown~visibility},
4263 unknown~style~cont={kl~unknown~cont,index~unknown,debug~index~unknown~visibility},
4264 style~directive=kindex~style
4265 }
4266 \knowledgevariantmodifier{\kindex*\kl}{\kindex}
4267 \knowledgevariantmodifier{\kindexfrom*\kl}{\kindexfrom}
4268 \knowledgevariantmodifier{\kindexto*\kl}{\kindexto}
4269 }
4270 % \end{macrocode}
4271 %
4272 %
4273 %
4274
4275 %
4276 % \subsubsection{The \kl{cleveref option}}
4277 %
4278 % \begin{macrocode}
4279 \bool_if:NTF\knowledge_cleveref_active_bool{
4280 \knowledgenewvariant\kcref{
4281 namespace=default,
4282 unknown~style={kl~unknown,ref~unknown},
4283 unknown~style~cont={kl~unknown~cont,ref~unknown},
4284 default~style=autoref~link,
4285 display~code={\knowledgedisplayref\cref}
4286 }
4287 \knowledgenewvariant\kCref{
4288 namespace=default,
4289 unknown~style={kl~unknown,ref~unknown},
4290 unknown~style~cont={kl~unknown~cont,ref~unknown},
4291 default~style=autoref~link,
4292 display~code={\knowledgedisplayref\Cref}
4293 }
4294 \knowledgenewvariant\kcpageref{
4295 namespace=default,
4296 unknown~style={kl~unknown,ref~unknown},
4297 unknown~style~cont={kl~unknown~cont,ref~unknown},
4298 default~style=autoref~link,
4299 display~code={\knowledgedisplayref\cpageref}
4300 }
4301 \knowledgenewvariant\kCpageref{
4302 namespace=default,
4303 unknown~style={kl~unknown,ref~unknown},
4304 unknown~style~cont={kl~unknown~cont,ref~unknown},
4305 default~style=autoref~link,
4306 display~code={\knowledgedisplayref\Cpageref}
4307 }
4308 \knowledgenewvariant\knamecref{
4309 namespace=default,
4310 unknown~style={kl~unknown,ref~unknown},
4311 unknown~style~cont={kl~unknown~cont,ref~unknown},
4312 default~style=autoref~link,
4313 display~code={\knowledgedisplayref\namecref}
4314 }
4315 \knowledgenewvariant\knameCref{
4316 namespace=default,
4317 unknown~style={kl~unknown,ref~unknown},
4318 unknown~style~cont={kl~unknown~cont,ref~unknown},
4319 default~style=autoref~link,
4320 display~code={\knowledgedisplayref\nameCref}
4321 }
4322 \knowledgenewvariant\knamecrefs{
4323 namespace=default,
4324 unknown~style={kl~unknown,ref~unknown},
4325 unknown~style~cont={kl~unknown~cont,ref~unknown},
4326 default~style=autoref~link,
4327 display~code={\knowledgedisplayref\namecrefs}
4328 }
4329 \knowledgenewvariant\knameCrefs{
4330 namespace=default,
4331 unknown~style={kl~unknown,ref~unknown},
4332 unknown~style~cont={kl~unknown~cont,ref~unknown},
4333 default~style=autoref~link,
4334 display~code={\knowledgedisplayref\nameCrefs}
4335 }
4336 \knowledgevariantmodifier{\kcref*\kl}{\kcref}
4337 \knowledgevariantmodifier{\kCref*\kl}{\kCref}
4338 \knowledgevariantmodifier{\kcpageref*\kl}{\kcpageref}
4339 \knowledgevariantmodifier{\kCpageref*\kl}{\kCpageref}
4340 \knowledgevariantmodifier{\knamecref*\kl}{\knamecref}
4341 \knowledgevariantmodifier{\knameCref*\kl}{\knameCref}
4342 \knowledgevariantmodifier{\knamecrefs*\kl}{\knamecrefs}
4343 \knowledgevariantmodifier{\knameCrefs*\kl}{\knameCrefs}
4344 }{
4345 \cs_new:Nn\knowledge_cleveref_error:N{
4346 \newcommand#1{\knowledge_error:n{Use~package~'cleveref'~for~command~#1.}}
4347 }
4348 \knowledge_cleveref_error:N\kcref
4349 \knowledge_cleveref_error:N\kCref
4350 \knowledge_cleveref_error:N\kcpageref
4351 \knowledge_cleveref_error:N\kCpageref
4352 \knowledge_cleveref_error:N\knamecref
4353 \knowledge_cleveref_error:N\knameCref
4354 \knowledge_cleveref_error:N\knamecrefs
4355 \knowledge_cleveref_error:N\knameCrefs
4356 }
4357 % \end{macrocode}
4358 %
4359 % \subsection{Fixes}
4360 %
4361 % \subsubsection{Hyperref in twocolumn}
4362 %
4363 % A bug occurs when hyperref is used in twocolumn mode and a link spreads on the boundary between two pages. This causes a fatal error. Here is defined a workaround found at:
4364 %\url{http://tex.stackexchange.com/questions/249579/pdfendlink-ended-up-in-different-nesting-level-than-pdfstartlink-error-with}
4365 % for avoiding problems with hyperref links being split it is suficient to call
4366 % the macro \cs{\knowledgeFixHyperrefTwocolumn}
4367 % or \cs\knowledgeconfigure\texttt\{\kl{fix hyperref twocolumn}\texttt\}
4368 % \begin{macrocode}
4369 \ExplSyntaxOff\makeatletter
4370 \newcommand\knowledgeFixHyperrefTwocolumn{
4371 \RequirePackage{etoolbox}
4372 \newcount\c@additionalboxlevel
4373 \setcounter{additionalboxlevel}{0}
4374 \newcount\c@maxboxlevel
4375 \setcounter{maxboxlevel}{1}
4376 \patchcmd\@combinedblfloats{\box\@outputbox}{%
4377 \stepcounter{additionalboxlevel}%
4378 \box\@outputbox
4379 }{}{\errmessage{\noexpand\@combinedblfloats could not be patched}}
4380 %
4381 \AtBeginShipout{%
4382 \ifnum\value{additionalboxlevel}>\value{maxboxlevel}%
4383 \typeout{Warning: maxboxlevel might be too small, increase to %
4384 \the\value{additionalboxlevel}%
4385 }%
4386 \fi
4387 \@whilenum\value{additionalboxlevel}<\value{maxboxlevel}\do{%
4388 \typeout{* Additional boxing of page `\thepage'}%
4389 \setbox\AtBeginShipoutBox=\hbox{\copy\AtBeginShipoutBox}%
4390 \stepcounter{additionalboxlevel}%
4391 }%
4392 \setcounter{additionalboxlevel}{0}%
4393 }
4394 }
4395 \ExplSyntaxOn
4396 \makeatother
4397 % \end{macrocode}
4398 %
4399 %
4400 %
4401 % \subsection{configuration}
4402 %
4403 % \subsubsection{Patching}
4404 %
4405 % Since v1.22, the package corrects by default some behaviours that are annoying in other packages.
4406 % This can be avoided using the trigger \kl{no patch} when you load the document.
4407 % \begin{macrocode}
4408 \bool_if:NT\knowledge_option_texpatch_bool{
4409 \bool_if:NT\knowledge_hyperref_active_bool{
4410 \hypersetup{breaklinks}
4411 \hypersetup{hidelinks}
4412 }
4413 }
4414 % \end{macrocode}
4415 %
4416 % \subsubsection{Accessing the \kl{writing mode}}
4417 %
4418 % The paper can be compiled either in \kl{composition mode}, in \kl{paper mode}, or in \kl{electronic mode}.
4419 % But not both or none. In case of none, this is by default \kl{paper mode}. In case both are activated, this is \kl{paper mode}.
4420 % \begin{macrocode}
4421 \bool_if:NF\knowledge_option_electronic_bool
4422 {\bool_if:NF\knowledge_option_paper_bool}
4423 {\bool_set_true:N\knowledge_option_composition_bool}
4424 \bool_if:NT\knowledge_option_composition_bool
4425 {\bool_if:NT\knowledge_option_paper_bool}
4426 {\bool_set_false:N\knowledge_option_composition_bool}
4427
4428 \DeclareExpandableDocumentCommand\IfKnowledgeCompositionModeTF{}
4429 {\bool_if:NTF\knowledge_option_composition_bool}
4430 \newif\ifKnowledgeCompositionMode
4431 \IfKnowledgeCompositionModeTF
4432 \KnowledgeCompositionModetrue
4433 \KnowledgeCompositionModefalse
4434
4435 \DeclareExpandableDocumentCommand\IfKnowledgeElectronicModeTF{}
4436 {\bool_if:NTF\knowledge_option_electronic_bool}
4437 \newif\ifKnowledgeElectronicMode
4438 \IfKnowledgeElectronicModeTF
4439 \KnowledgeElectronicModetrue
4440 \KnowledgeElectronicModefalse
4441
4442 \DeclareExpandableDocumentCommand\IfKnowledgePaperModeTF{}
4443 {\bool_if:NTF\knowledge_option_paper_bool}
4444 \DeclareExpandableDocumentCommand\IfKnowledgePaperModeF{}
4445 {\IfKnowledgePaperModeTF{}}
4446 \newif\ifKnowledgePaperMode
4447 \IfKnowledgePaperModeTF
4448 \KnowledgePaperModetrue
4449 \KnowledgePaperModefalse
4450
4451 \DeclareExpandableDocumentCommand\IfXcolorTF{}
4452 {\bool_if:NTF\knowledge_xcolor_active_bool}
4453 \newif\ifXcolor
4454 \IfXcolorTF
4455 \Xcolortrue
4456 \Xcolorfalse
4457 % \end{macrocode}
4458 % When "xcolor@@package" is loaded, introduced indices are in red.
4459 % \begin{macrocode}
4460 \IfXcolorTF
4461 {\def\knowledgeIntroIndexStyle#1{\textcolor{red}{#1}}}
4462 {}
4463 % \end{macrocode}
4464 %
4465 %
4466 % \subsubsection{Variants of the \cs{\kl} macro}
4467 %
4468 % \begin{macrocode}
4469 \tl_new:Nn\knowledge_kl_default_style_tl{kl}
4470 \tl_new:Nn\knowledge_kl_unknown_style_tl{kl~unknown}
4471 \tl_new:Nn\knowledge_kl_unknown_style_cont_tl{kl~unknown~cont}
4472 \tl_new:Nn\knowledge_kl_styledirectives_tl{\knowledge_kl_style_tl}
4473 \ExplSyntaxOff
4474 \knowledgestyle{autoref link}{autoref link}
4475 \knowledgestyle{autoref target}{autoref target}
4476 \knowledgestyle{invisible}{invisible}
4477 \knowledgestyle{composition}{underline}
4478 \knowledgenewvariant\kl{
4479 namespace= default,
4480 default style= {autoref link},
4481 unknown style= kl unknown,
4482 unknown style cont= kl unknown cont,
4483 style directive= style
4484 }
4485 \knowledgenewvariant\hkl{
4486 namespace= default,
4487 default style={autoref link},
4488 unknown style= kl unknown,
4489 unknown style cont= kl unknown cont,
4490 style directive= highlight style
4491 }
4492 \knowledgenewvariant\intro{
4493 namespace= default,
4494 default style= {autoref target},
4495 unknown style= intro unknown,
4496 unknown style cont= intro unknown cont,
4497 style directive= intro style
4498 }
4499 \knowledgenewvariant\phantomintro{
4500 namespace=default,
4501 default style={invisible,autoref target},
4502 unknown style=invisible,
4503 unknown style cont=invisible
4504 }
4505 \knowledgenewvariant\reintro{
4506 namespace=default,
4507 % default style=intro,
4508 unknown style=intro unknown,
4509 unknown style cont=intro unknown cont,
4510 style directive=intro style
4511 }
4512 \knowledgestyle{ref unknown}{text=??}
4513 \knowledgenewvariant\kref{
4514 namespace=default,
4515 unknown style={kl unknown,ref unknown},
4516 unknown style cont={kl unknown cont,ref unknown},
4517 default style=autoref link,
4518 display code={\knowledgedisplayref\ref}
4519 }
4520 \knowledgenewvariant\kpageref{
4521 namespace=default,
4522 unknown style={kl unknown,ref unknown},
4523 unknown style cont={kl unknown cont,ref unknown},
4524 default style=autoref link,
4525 display code={\knowledgedisplayref\pageref}
4526 }
4527 \knowledgevariantmodifier{\hkl*\kl}{\hkl}
4528 \knowledgevariantmodifier{\kref*\kl}{\kref}
4529 \knowledgevariantmodifier{\kpageref*\kl}{\kpageref}
4530 \knowledgevariantmodifier{\intro*\kl}{\intro}
4531 \knowledgevariantmodifier{\reintro*\kl}{\reintro}
4532
4533 \ExplSyntaxOn
4534 % \end{macrocode}
4535 %
4536 % \subsubsection{Basic configuration}
4537 %
4538 % \begin{macrocode}
4539 \tl_set_eq:NN\knowledge_kl_namespace_tl\knowledge_default_namespace_tl
4540 % \end{macrocode}
4541 %
4542 % \begin{macrocode}
4543 \newcommand\robustdisplay[1]
4544 {\expandafter\use:n%\tl_trim_spaces:n
4545 \expandafter{\tl_to_str:n{#1}}}
4546 \newcommand\robustdisplaybracket[1]
4547 {[\robustdisplay{#1}]}
4548 % \end{macrocode}
4549 % The ignore directive does nothing...
4550 % \begin{macrocode}
4551 \knowledgedirective{ignore}{}
4552 % \end{macrocode}
4553 %
4554 % \begin{macrocode}
4555 \ExplSyntaxOff
4556 % in paper mode, nothing is displayed
4557 \IfKnowledgePaperModeTF{
4558 % \knowledgestyle{kl}{}
4559 \knowledgestyle{kl unknown}{}
4560 \knowledgestyle{kl unknown cont}{}
4561 % \knowledgestyle{intro}{emphasize,index style=knowledgeIntroIndexStyle}
4562 \knowledgestyle{intro unknown}{emphasize}
4563 \knowledgestyle{intro unknown cont}{emphasize}
4564 }{}
4565
4566 \IfKnowledgeCompositionModeTF{
4567 \IfXcolorTF{
4568 % \knowledgestyle{kl}{color={blue!70!black}}
4569 \knowledgestyle{kl unknown}{color=orange}
4570 \knowledgestyle{kl unknown cont}{color=brown}
4571 % \knowledgestyle{intro}{emphasize,color=blue,index style=knowledgeIntroIndexStyle}
4572 \knowledgestyle{intro unknown}{emphasize,color=orange}
4573 \knowledgestyle{intro unknown cont}{emphasize,color=brown}
4574 \knowledgestyle{composition}{color=teal}
4575 }{
4576 % \knowledgestyle{kl}{}
4577 \knowledgestyle{kl unknown}{underline}
4578 \knowledgestyle{kl unknown cont}{underline}
4579 % \knowledgestyle{intro}{emphasize,index style=introindexstyle}
4580 \knowledgestyle{intro unknown}{emphasize,underline}
4581 \knowledgestyle{intro unknown cont}{emphasize,underline}
4582 }
4583 }{}
4584
4585 \IfKnowledgeElectronicModeTF{
4586 \IfXcolorTF{
4587 % \knowledgestyle{kl}{color={blue!70!black}}
4588 \knowledgestyle{kl unknown}{}
4589 \knowledgestyle{kl unknown cont}{}
4590 % \knowledgestyle{intro}{emphasize,color=blue,index style=introindexstyle}
4591 \knowledgestyle{intro unknown}{emphasize}
4592 \knowledgestyle{intro unknown cont}{emphasize}
4593 }{
4594 % \knowledgestyle{kl}{}
4595 \knowledgestyle{kl unknown}{}
4596 \knowledgestyle{kl unknown cont}{}
4597 % \knowledgestyle{intro}{emphasize,index style=introindexstyle}
4598 \knowledgestyle{intro unknown}{emphasize}
4599 \knowledgestyle{intro unknown cont}{emphasize}
4600 }
4601 }{}
4602
4603 \ExplSyntaxOn
4604 \let\nointro\phantomintro
4605 % \end{macrocode}
4606 %
4607
4608 %
4609 % \subsubsection{The "notion@@option" configurations}
4610 % \label{section:notion option}
4611 %
4612 %\paragraph{The configuration option `\kl{notion}'.}
4613 %
4614 %The macro \csintro{\KnowledgeConfigureNotion}
4615 % takes an optional color name (default is blue), and a text.
4616 % it creates the equivalent of notion of this name, including, configuration of the colors, introducing of styles, and so on.
4617 %
4618 % \begin{macrocode}
4619 \NewDocumentCommand\KnowledgeConfigureNotion{ o m }{
4620 % \end{macrocode}
4621 % This code, which is called using \cs{\KnowledgeConfigureNotion} or
4622 % the configuration option \kl{notion} can serve as a basis for other configuration.
4623 %\begin{code}[10cm]
4624 % \begin{macrocode}
4625 % \ltmpa_clist for the directive description
4626 \clist_set:Nn\l_tmpa_clist{}
4627 \clist_push:Nn\l_tmpa_clist{autoref}
4628 \clist_push:Nn\l_tmpa_clist{style=#2}
4629 \clist_push:Nn\l_tmpa_clist{intro~style=intro~#2}
4630 \clist_push:Nn\l_tmpa_clist{highlight~style=highlight~#2}
4631 \bool_if:NT\knowledge_makeidx_active_bool{
4632 \cs_set:cpn{#2KnowledgeIndexStyle}##1{##1}
4633 \cs_set:cpn{#2KnowledgeIndexIntroStyle}{\textbf}
4634 \clist_push:Nn\l_tmpa_clist{kindex~style=index~#2}
4635 \exp_args:Nnx\knowledgestyle{index~#2}{index~style=#2KnowledgeIndexStyle}
4636 }
4637 \exp_args:Nnx\knowledgedirective{#2}\l_tmpa_clist
4638 % \l_tmpa_clist for normal style
4639 % \l_tmpb_clist for intro style
4640 \clist_set:Nn\l_tmpa_clist{}
4641 \clist_set:Nn\l_tmpb_clist{}
4642 \ifKnowledgePaperMode
4643 % paper mode
4644 \clist_push:Nn\l_tmpb_clist{emphasize}
4645 \fi
4646 \ifKnowledgeCompositionMode
4647 \ifXcolor
4648 % composition mode with colors
4649 \clist_push:Nn\l_tmpb_clist{emphasize}
4650 \IfNoValueTF{#1}{}
4651 {\clist_push:Nn\l_tmpa_clist{color={#1!70!black}}
4652 \clist_push:Nn\l_tmpb_clist{color={#1}}}
4653 \else
4654 % composition mode, no colors (use underline)
4655 \clist_push:Nn\l_tmpa_clist{underline}
4656 \clist_push:Nn\l_tmpb_clist{emphasize}
4657 \clist_push:Nn\l_tmpb_clist{underline}
4658 \fi
4659 \fi
4660 \ifKnowledgeElectronicMode
4661 \ifXcolor
4662 % electronic mode
4663 \clist_push:Nn\l_tmpb_clist{emphasize}
4664 \IfNoValueTF{#1}{}
4665 {\clist_push:Nn\l_tmpa_clist{color={#1!70!black}}
4666 \clist_push:Nn\l_tmpb_clist{color={#1}}}
4667 \clist_push:Nn\l_tmpb_clist{color={#1}}
4668 \else
4669 % electronic mode, no colors (use underline)
4670 \clist_push:Nn\l_tmpa_clist{underline}
4671 \clist_push:Nn\l_tmpb_clist{emphasize}
4672 \clist_push:Nn\l_tmpb_clist{underline}
4673 \fi
4674 \fi
4675 \bool_if:NT\knowledge_makeidx_active_bool{
4676 \clist_push:Nn\l_tmpa_clist{index~style=#2KnowledgeIndexStyle}
4677 \clist_push:Nn\l_tmpb_clist{index~style=#2KnowledgeIndexIntroStyle}
4678 }
4679 \exp_args:Nnx\knowledgestyle{#2}{\clist_use:Nn\l_tmpa_clist ,}
4680 \exp_args:Nnx\knowledgestyle{highlight~#2}{\clist_use:Nn\l_tmpa_clist ,}
4681 \exp_args:Nnx\knowledgestyle{intro~#2}{\clist_use:Nn\l_tmpb_clist ,}
4682 % \end{macrocode}
4683 %\end{code}
4684 % End of the interesting code.
4685 % \begin{macrocode}
4686 }
4687 % \end{macrocode}
4688 %
4689 % \subsection{Finalizing}
4690 %
4691 % At the end, the pending configurations have to be executed (this has to be done with the proper
4692 % catcode when for instance the \kl(option){quotation} notation is activated).
4693 %</package>
4694 %<*tail>
4695 % \begin{macrocode}
4696 \ExplSyntaxOn
4697 \exp_args:NNV\ExplSyntaxOff\knowledgeconfigure\knowledge_configuration_pending_tl
4698 % \end{macrocode}
4699 % \begin{macrocode}
4700 %\ScopeActivate
4701 % \end{macrocode}
4702 %</tail>
4703 %
4704 % \Finale
4705 %\endinput
4706
4707
4708
4709