Debian Codegraph @

texmf-dist/source/latex-dev/latex-lab/latex-lab-context.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: latex-lab-context.dtx (C) Copyright 2025 LaTeX Project
4 %
5 % It may be distributed and/or modified under the conditions of the
6 % LaTeX Project Public License (LPPL), either version 1.3c of this
7 % license or (at your option) any later version. The latest version
8 % of this license is in the file
9 %
10 % https://www.latex-project.org/lppl.txt
11 %
12 %
13 % The latex-lab bundle is developed in the LaTeX2e GitHub.
14 % Issues may be reported at
15 %
16 % https://github.com/latex3/latex2e/issues
17 %
18 \def\ltlabcontextversion{0.5d}
19 \def\ltlabcontextdate{2026-04-27}
20 %<*driver>
21 \DocumentMetadata{tagging=on,pdfstandard=ua-2,lang=en}
22 \documentclass{l3in2edoc}
23
24 \EnableCrossrefs
25 \CodelineIndex
26 \begin{document}
27 \DocInput{latex-lab-context.dtx}
28 \end{document}
29 %</driver>
30 %
31 % \fi
32 %
33 % \title{The \textsf{latex-lab-context} package\\
34 % Providing context for template instances and code that needs
35 % to know where and when it is executed}
36 %
37 % \author{\LaTeX{} Project\thanks{Initial implementation done by Frank Mittelbach}}
38 % \date{v\ltlabcontextversion\ \ltlabcontextdate}
39 %
40 % \maketitle
41 %
42 % \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
43 % \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
44 %
45 % \providecommand\hook[1]{\texttt{#1}}
46 %
47 % \begin{abstract}
48 % \end{abstract}
49 %
50 % \section{Introduction}
51 %
52 % In this module we implement the concept of \enquote{contexts}
53 % within the document and depending on the current context
54 % formatting behavior might change. The main application for this
55 % is in instances of templates (where things can be easily automated
56 % to react to context), but it is probably also applicable to other
57 % situations (but these might need more thought).
58 %
59 % \subsection{Definition of a \enquote{context}}
60 %
61 % The \enquote{context} is an attribute of every point of the
62 % formatted document, i.e., at each point during the formatting one
63 % can determine in which \enquote{context} the formatting happens
64 % and based on this adjust the formatting method.
65 %
66 % The \enquote{context} is not an entirely flat structure: we
67 % distinguish between the \enquote{primary context} and the
68 % \enquote{secondary context} both of which can be changed
69 % individually based on a number of rules as discussed below.
70 %
71 % Any context is denoted by a name (\verb=[a-z]*= letters only). The
72 % empty name is allowed to ease specification of the common case
73 % (i.e., the default \enquote{primary context} is the main galley
74 % and by default no \enquote{secondary context} is specified).
75 %
76 % \subsection{The \enquote{primary context}}
77 %
78 % The \enquote{primary context} is described with a fixed (but
79 % extensible?) set of names:
80 % \begin{itemize}
81 %
82 % \item \meta{empty} denotes that we are in \enquote{galley} context
83 % producing text for the page;
84 %
85 % \item \texttt{caption}
86 % denotes that we are typesetting the caption text of a \cs{caption} command
87 % --- this is also used for captions outside of floats,
88 %
89 % \item \texttt{footnote}
90 % denotes that we are typesetting the footnote text;
91 %
92 % \item \texttt{float}
93 % denotes that we are typesetting a float;
94 %
95 % \item \texttt{marginal}
96 % denotes that we are typesetting a marginal;
97 %
98 % \item \texttt{header}
99 % denotes that we are typesetting the running header;
100 %
101 % \item \texttt{footer}
102 % denotes that we are typesetting the running footer.
103 %
104 % \end{itemize}
105 %
106 % When the \enquote{primary context} is set it replaces the current
107 % \enquote{primary context} and resets the \enquote{secondary
108 % context} to \meta{empty}. The setting is local, i.e., it obeys
109 % grouping.
110 %
111 % It would be possible to be more granular, i.e., differentiate
112 % between types of float etc. But for now I suggest to start out
113 % with this small set.
114 %
115 % We may want to require that a context name is declared before its
116 % first use, e.g., via \cs{DeclarePrimaryContext} (right now this is
117 % not necessary.
118 %
119 %
120 % \subsection{The \enquote{secondary context}}
121 %
122 % The \enquote{secondary context} concerns itself (for now) only
123 % with font size changes, because that is most commonly a cause for
124 % layout changes. It is described through a fixed (but extensible?)
125 % set of names:
126 %
127 % \begin{itemize}
128 % \item
129 % \meta{empty} denotes that there is no special secondary
130 % context, i.e., that we are producing material in \cs{normalsize};
131 %
132 % \item \texttt{tiny}
133 % denotes that we are typesetting in \cs{tiny} font size;
134 %
135 % \item and similarly for \texttt{scriptsize},
136 % \texttt{footnotesize}, \texttt{small}, \texttt{large},
137 % \texttt{Large}, \texttt{LARGE}, \texttt{huge}, and \texttt{Huge}.
138 % \end{itemize}
139 % The above list of secondary contexts are automatically set by the
140 % standard \LaTeX{} font size commands (as part of \cs{@setfontsize}). Classes
141 % that use additional font size commands but do not use the
142 % \LaTeXe{} command for this need to explicitly set the secondary
143 % context with \cs{SetSecondaryContext} if they want their size be
144 % recognized as a context.
145 %
146 %
147 %
148 % \section{Setting context}
149 %
150 % \begin{function}{\SetPrimaryContext}
151 % \begin{syntax}
152 % \cs{SetPrimaryContext} \Arg{new context}
153 % \end{syntax}
154 % The \meta{new context} should be a name consisting only of
155 % lowercase letters a--z (and to make sense only a name that is
156 % actually used---typos will go undetected).
157 %
158 % This declaration checks if there is a rule for the combination of
159 % the current context and the requested \meta{new context} (see
160 % \cs{DeclarePrimaryContextRule}) and if so applies it to determine
161 % to which context the \enquote{primary context} should be set.
162 % If there is no such rule then \meta{new context} will be used
163 % unconditionally.
164 %
165 % As a second action the command executes
166 % \cs{SetSecondaryContext}\texttt{\{\}}, i.e., it clears the
167 % \enquote{secondary context}.
168 %
169 % The effects are local, i.e., both context changes revert to the
170 % previous value at the end of the current group.
171 % \end{function}
172 %
173 %
174 %
175 % \begin{function}{\DeclarePrimaryContextRule}
176 % \begin{syntax}
177 % \cs{DeclarePrimaryContextRule} \Arg{current}\Arg{new}\Arg{result}
178 % \end{syntax}
179 % This declaration defines the rule that when the current primary
180 % context is \meta{current} and \meta{new} is requested as the new
181 % context then instead \meta{result} is made the new
182 % \enquote{primary context}.
183 %
184 % If \texttt{*} is used in the first argument then this indicates
185 % any current context and thus \meta{new} is always replaced by
186 % \meta{result}. This can be more concisely written as
187 % \begin{quote}
188 % \cs{DeclarePrimaryContextRule*} \Arg{new}\Arg{result}
189 % \end{quote}
190 % if preferred.
191 %
192 % Declaring such rules is a global operation.
193 % \end{function}
194 %
195 %
196 %
197 % \begin{function}{\SetSecondaryContext}
198 % \begin{syntax}
199 % \cs{SetSecondaryContext} \Arg{new context}
200 % \end{syntax}
201 % The \meta{new context} should be a name consisting only of
202 % lowercase letters a--z (and to make sense only a name that is
203 % actually used---typos will go undetected).
204 %
205 % This declaration checks if there is a rule for the combination of
206 % the current secondary context and the requested \meta{new context}
207 % (see \cs{DeclareSecondaryContextRule}) and if so applies it to
208 % determine to which context the \enquote{secondary context} should
209 % be set. If there is no such rule then \meta{new context} will
210 % be used unconditionally.
211 %
212 % The effect is local, i.e., the context change reverts to the
213 % previous value at the end of the current group.
214 % \end{function}
215 %
216 %
217 %
218 % \begin{function}{\DeclareSecondaryContextRule}
219 % \begin{syntax}
220 % \cs{DeclareSecondaryContextRule} \Arg{current}\Arg{new}\Arg{result}
221 % \end{syntax}
222 % This declaration defines the rule that when the current secondary
223 % context is \meta{current} and \meta{new} is requested as the new
224 % context then instead \meta{result} is made the new
225 % \enquote{secondary context}.
226 %
227 % If \texttt{*} is used in the first argument then this indicates
228 % any current context and thus \meta{new} is always replaced by
229 % \meta{result}. This can be more concisely written as
230 % \begin{quote}
231 % \cs{DeclareSecondaryContextRule*} \Arg{new}\Arg{result}
232 % \end{quote}
233 % if preferred.
234 %
235 % Declaring such rules is a global operation.
236 % \end{function}
237 %
238 %
239 %
240 % \section{Context usage in template instances}
241 %
242 % If a template instance is used via
243 % \cs{UseInstance}\Arg{type}\Arg{inst-name} then this normally
244 % results in calling up an instance of type \meta{type} with the name
245 % \meta{inst-name}.\footnote{Such instances are defined with
246 % \cs{DeclareInstance} or \cs{DeclareInstanceCopy}, see the
247 % documentation in
248 % \texttt{lttemplates-doc.pdf}.}
249 %
250 % However, when the \enquote{primary context} and/or the \enquote{secondary
251 % context} is non-empty then \cs{UseInstance} searches for an
252 % instance that is especially tailored to the current context.
253 % This works as follows:
254 % \begin{itemize}
255 % \item The string\texttt{:}\meta{primary
256 % context}\texttt{:}\meta{secondary context} is appended to
257 % \meta{inst-name} and if that instance exist it is
258 % used.\footnote{Note that this means that
259 % if the \meta{primary context} is empty we effectively append
260 % \texttt{::}\meta{secondary context}.}
261 % \item
262 % If not then \meta{inst-name}\texttt{:}\meta{primary context} is
263 % tried next.
264 % \item
265 % If that doesn't exist either then \meta{inst-name} is used as usual.
266 % \end{itemize}
267 %
268 % This means it becomes trivial to alter the behavior of
269 % instances if they appear in a special typesetting context. For
270 % example, in \LaTeXe{} display blocks, e.g., lists, center, etc., all
271 % changed their vertical spacing setup if the surrounding text was
272 % in \cs{small} or in \cs{footnotesize} (but did nothing if you
273 % typeset in, say, \cs{Large} which had the strange effect that
274 % lists in \cs{Large} or \cs{huge} got whatever was set by a size
275 % command that changed list parameters).
276 %
277 % With the new context model all you have to do is to provide
278 % additional instances, e.g., if \texttt{itemize-1} is the instance
279 % name for itemized lists on the first level then
280 % \texttt{itemize-1:footnote} would be the instance to be used if an
281 % itemize environment is used within a footnote.
282 %
283 % In addition (or alternatively) you could setup
284 % \texttt{itemize-1::Large} which would be selected if the
285 % itemize is in the main galley (empty primary context) and the
286 % fontsize at the outside is \cs{Large}.
287 %
288 %
289 % \section{Notes}
290 %
291 % With special classes, e.g., \texttt{ltx-talk}, additional primaries
292 % could be added (and secondaries could be using the modes rather
293 % than or in addition to the fontsizes). Or the modes could be part
294 % of the primary names \dots. Could do with some experimentation
295 % what works best.
296 %
297 % If the design of a class requires identical behavior in different
298 % contexts, e.g., the same behavior in \texttt{header} and
299 % \texttt{footer}, it is possible to simplify the setup by only
300 % specifying the design for the \texttt{header} context and then
301 % declaring:
302 %\begin{verbatim}
303 % \DeclarePrimaryContextRule{*}{footer}{header}
304 %\end{verbatim}
305 %
306 % One can also add new primary or secondary contexts simply by making
307 % use of \cs{Set...Context} declarations with a new name and then
308 % use these named contexts when setting up special instance
309 % versions.
310 %
311 % What is currently not so easy is to get rid of context names that
312 % have been set up, e.g., to use your own \texttt{wipple} and
313 % \texttt{wopple} as secondary contexts and make sure that fontsize
314 % commands to not overwrite the context. Right now that would
315 % require a lot of rules like
316 %\begin{verbatim}
317 % \DeclareSecondaryContextRule{wipple}{tiny}{wipple}
318 % \DeclareSecondaryContextRule{wipple}{footnotesize}{wipple}
319 % \DeclareSecondaryContextRule{wipple}{scriptsize}{wipple}
320 % ...
321 %\end{verbatim}
322 % so perhaps this interface needs changes or some augmentation, if
323 % such thing turn out to be useful, e.g., supporting
324 % \verb=\DeclareSecondaryContextRule{*}{tiny}{*}= to indicate this.
325 %
326 % Another possibility is to support several independent dimensions
327 % as secondary contexts (then \enquote{fontsize} could be one and
328 % the \enquote{wipple-wopple} one the other. which of these are used
329 % when instances are selected could then (perhaps) be a function of
330 % the template type but for a heading type looking at
331 % front/main/back matter would be more appropriate.\footnote{Then, of
332 % course, a primary context change would not reset any secondary
333 % context.}
334 %
335 % Bottom line, what is here is nothing more than a first prototype
336 % and likely to change to some extent.
337 %
338 % \section{Currently mainly internal interfaces}
339 %
340 % \begin{variable}{\l_context_primary_str}
341 % This variable holds the current primary context name (or is empty
342 % if we are in the context of the main galley).
343 % \end{variable}
344 %
345 % \begin{variable}{\l_context_secondary_str}
346 % This variable holds the current secondary context name (or is
347 % empty if we are typesetting in \cs{normalsize}). It is by default
348 % automatically set by all fontsize commands in core \LaTeX{}. If
349 % additional secondary contexts are used then the variable may not
350 % be empty even if we are typesetting in \cs{normalsize}
351 % \end{variable}
352
353 % \section{Debugging}
354 %
355 % \begin{function}{\DebugContextsOn,\DebugContextsOff, \context_debug_on:, \context_debug_off:}
356 %
357 % These commands enable/disable debugging messages for context related
358 % processing.
359 %
360 % \end{function}
361 %
362 %
363 % \section{Changes to internals of \LaTeX}
364 %
365 % \begin{function}{\@setfontsize}
366 % This command has be augmented to execute \cs{SetSecondaryContext}
367 % with the name of the current fontsize command as the context argument.
368 % \end{function}
369 %
370 % \begin{function}{\@float}
371 % This command has been augmented to execute \cs{SetPrimaryContext}\texttt{\{float\}}.
372 % This results in the body of all real floats being typeset in the
373 % primary \texttt{float} context. Not covered are packages or
374 % methods that make floats not using \cs{@float} or making pseudo
375 % floats, e.g., something not floating but having a caption.
376 % \end{function}
377 %
378 %
379 %
380 %
381 %
382 % \section{Implementation}
383 % \begin{macrocode}
384 %<*package>
385 %<@@=tag>
386 % \end{macrocode}
387 % \begin{macrocode}
388 \ProvidesExplPackage {latex-lab-testphase-context}
389 {\ltlabcontextdate}
390 {\ltlabcontextversion}
391 {Providing context for instance, etc.}
392 % \end{macrocode}
393 %
394 % \begin{macrocode}
395 %<*package>
396 %<@@=context>
397 % \end{macrocode}
398 %
399 % \changes{v0.5c}{2026-03-12}{Switch from \texttt{x}- to \texttt{e}-type
400 % expansion in expl3 code}
401 %
402 %
403 % \subsubsection{Debugging}
404 %
405 %
406 % \begin{variable}{\g_@@_debug_bool}
407 %
408 % \begin{macrocode}
409 \bool_new:N \g_@@_debug_bool
410 % \end{macrocode}
411 % \end{variable}
412 %
413 %
414 % \begin{macro}{\@@_debug:n,\@@_debug_typeout:n}
415 %
416 % \begin{macrocode}
417 \cs_new_eq:NN \@@_debug:n \use_none:n
418 \cs_new_eq:NN \@@_debug_typeout:n \use_none:n
419 % \end{macrocode}
420 % \end{macro}
421 %
422 % \begin{macro}{\context_debug_on:,\context_debug_off:,
423 % \@@_debug_gset:}
424 % \begin{macrocode}
425 \cs_new_protected:Npn \context_debug_on:
426 {
427 \bool_gset_true:N \g_@@_debug_bool
428 \@@_debug_gset:
429 }
430 % \end{macrocode}
431 %
432 % \begin{macrocode}
433 \cs_new_protected:Npn \context_debug_off:
434 {
435 \bool_gset_false:N \g_@@_debug_bool
436 \@@_debug_gset:
437 }
438 % \end{macrocode}
439 %
440 % \begin{macrocode}
441 \cs_new_protected:Npn \@@_debug_gset:
442 {
443 \cs_gset_protected:Npe \@@_debug:n ##1
444 { \bool_if:NT \g_@@_debug_bool {##1} }
445 \cs_gset_protected:Npe \@@_debug_typeout:n ##1
446 { \bool_if:NT \g_@@_debug_bool { \typeout{[Context]~ ==>~ ##1} } }
447 }
448 % \end{macrocode}
449 % \end{macro}
450 %
451 %
452 % \begin{macro}{\DebugContextsOn,\DebugContextsOff}
453 %
454 % \begin{macrocode}
455 \cs_new_protected:Npn \DebugContextsOn { \context_debug_on: }
456 \cs_new_protected:Npn \DebugContextsOff { \context_debug_off: }
457 % \end{macrocode}
458 %
459 % \begin{macrocode}
460 \DebugContextsOff
461 % \end{macrocode}
462 % \end{macro}
463 %
464 %
465 %
466 %
467 % \begin{macro}{\l_context_primary_str}
468 % Variable to hold the name of the current primary context.
469 % \begin{macrocode}
470 \str_new:N \l_context_primary_str
471 % \end{macrocode}
472 % \end{macro}
473 %
474 %
475 %
476 %
477 % \begin{macro}{\SetPrimaryContext}
478 % Change the context: if the target context is empty, just do
479 % it. Otherwise if we have a rule for the current context and new context
480 % use that; otherwise if there is a star rule apply that; otherwise
481 % set the new value as requested,
482 % \begin{macrocode}
483 \cs_new_protected:Npn \SetPrimaryContext #1 {
484 \str_set:Ne \l_context_primary_str
485 { \tl_if_empty:nF { #1 }
486 { \cs_if_exist_use:cF
487 { g_@@_primary_ \l_context_primary_str _ #1 _tl }
488 { \cs_if_exist_use:cF
489 { g_@@_primary_ * _ #1 _tl }
490 { #1 }
491 }
492 }
493 }
494 \@@_debug_typeout:n{set~primary~ <-~ \l_context_primary_str }
495 % \end{macrocode}
496 % Also reset the secondary context.
497 % \begin{macrocode}
498 \SetSecondaryContext {}
499 }
500 % \end{macrocode}
501 % \end{macro}
502 %
503 %
504 % \begin{macro}{\DeclarePrimaryContextRule}
505 % Rules are simply stored in macro names:
506 % \begin{macrocode}
507 \cs_new_protected:Npn \DeclarePrimaryContextRule #1#2#3 {
508 \tl_gclear_new:c { g_@@_primary_ #1 _ #2 _tl }
509 \tl_gset:cn { g_@@_primary_ #1 _ #2 _tl } {#3}
510 }
511 % \end{macrocode}
512 % \end{macro}
513 %
514 %
515 % \begin{macro}{\l_context_secondary_str}
516 % Variable to hold the name of the current secondary context.
517 % \TODO{perhaps to be replaced by a multi-dimensional solution}
518 % \begin{macrocode}
519 \str_new:N \l_context_secondary_str
520 % \end{macrocode}
521 % \end{macro}
522 %
523 %
524 % \begin{macro}{\SetSecondaryContext}
525 % Similar to \cs{SetPrimaryContext}.
526 % \begin{macrocode}
527 \cs_new_protected:Npn \SetSecondaryContext #1 {
528 \str_set:Ne \l_context_secondary_str
529 { \tl_if_empty:nF { #1 }
530 { \cs_if_exist_use:cF
531 { g_@@_secondary_ \l_context_secondary_str _ #1 _tl }
532 { \cs_if_exist_use:cF
533 { g_@@_secondary_ * _ #1 _tl }
534 { #1 }
535 }
536 }
537 }
538 \@@_debug_typeout:n{set~ secondary~ <-~ \l_context_secondary_str }
539 }
540 % \end{macrocode}
541 % \end{macro}
542 %
543 %
544 % \begin{macro}{\DeclareSecondaryContextRule}
545 %
546 % \begin{macrocode}
547 \cs_new_protected:Npn \DeclareSecondaryContextRule #1#2#3 {
548 \tl_gclear_new:c { g_@@_secondary_ #1 _ #2 _tl }
549 \tl_gset:cn { g_@@_secondary_ #1 _ #2 _tl } {#3}
550 }
551 % \end{macrocode}
552 % \end{macro}
553 %
554 %
555 %
556 % \subsection{Supporting font size changes as a secondary context}
557 %
558 %
559 %
560 % \begin{macro}{\@setfontsize}
561 % We try to be careful when setting the name as we don't know if
562 % \cs{string} returns a backslash or not.
563 % \begin{macrocode}
564 \def\@setfontsize#1#2#3{\@nomath#1%
565 \ifx\protect\@typeset@protect
566 \let\@currsize#1%
567 \begingroup
568 \escapechar\m@ne
569 \expandafter
570 \endgroup
571 \expandafter
572 \SetSecondaryContext
573 \expandafter {\string#1}%
574 \fi
575 \fontsize{#2}{#3}\selectfont
576 }
577 % \end{macrocode}
578 %
579 % If we are typesetting in \cs{normalsize} we don't want that as
580 % the context, so here is a first application of a rule.
581 % \begin{macrocode}
582 \DeclareSecondaryContextRule{*}{normalsize}{}
583 % \end{macrocode}
584 % \end{macro}
585 %
586 %
587 %
588 % \subsection{Supporting primary context}
589 %
590 % \subsubsection{Float context}
591 %
592 % \begin{macro}{\@float}
593 % This should work with most float pages. There are of course some
594 % classes or packages that produce pseudo-floats without calling
595 % \cs{@float}. These need to be handled separately by adding a
596 % \cs{SetPrimaryContext} in their code.
597 % \begin{macrocode}
598 \AddToHook{cmd/@float/before}{\SetPrimaryContext{float}}
599 % \end{macrocode}
600 % \end{macro}
601 %
602 %
603 % \subsubsection{Caption context}
604 %
605 % \TODO{in latex-lab-float}
606 % \cs{@makecaption} is redefined there and the context should be set in that
607 % command (or its replacement one day).
608 %
609 % \TODO{longtable and anything else that runs around producing \cs{caption}s}
610 %
611 %
612 % \subsubsection{Footnote context}
613 %
614 % \TODO{in latex-lab-footnote}
615 %
616 %
617 %
618 % \subsubsection{Header context}
619 %
620 % \TODO{}
621 %
622 %
623 %
624 % \subsubsection{Footer context}
625 %
626 % \TODO{}
627 %
628 %
629 %
630 % \subsubsection{Marginal context}
631 %
632 % \TODO{}
633 %
634 %
635 %
636 %
637 %
638 % \subsection{Changes to lttemplates.dtx code}
639 %
640 % \begin{macrocode}
641 %<@@=template>
642 % \end{macrocode}
643 %
644 %
645 %
646 %
647 % \begin{macro}{\@@_use_instance_auxii:nn, \@@_use_instance_auxiii:nn}
648 % This is the command that is used by \cs{UseInstance} to select and
649 % execute a requested instance. So we now have to have a little
650 % more logic here.
651
652 % In the normal case (both primary and secondary context are empty)
653 % we now have 3 tests. We have up to 4 tests if only primary is
654 % non-empty and up to 5 tests if secondary is non-empty.
655 % \begin{macrocode}
656 \cs_new_eq:NN \@@_use_instance_auxiii:nn \@@_use_instance_auxii:nn
657 \cs_set_protected:Npn \@@_use_instance_auxii:nn #1#2 {
658 \@@_debug:n { \str_if_empty:NF \l_context_primary_str
659 { \@@_debug_typeout:n {primary~ context~ is~
660 '\l_context_primary_str' } } }
661 \@@_debug:n { \str_if_empty:NF \l_context_secondary_str
662 { \@@_debug_typeout:n {secondary~ context~ is~
663 '\l_context_secondary_str' } } }
664 \tl_if_empty:NTF \l__context_secondary_tl
665 {
666 \str_if_empty:NTF \l_context_primary_str
667 {
668 \@@_if_instance_exist:nnTF {#1} {#2}
669 { \@@_use_instance_auxiii:nn {#1} {#2} }
670 { \msg_error:nnnn { template } { unknown-instance } {#1} {#2} }
671 }
672 {
673 \@@_if_instance_exist:nnTF {#1} { #2 : \l_context_primary_str }
674 { \@@_use_instance_auxiii:nn{#1}
675 { #2 : \l_context_primary_str } }
676 {
677 \@@_if_instance_exist:nnTF {#1} {#2}
678 { \@@_use_instance_auxiii:nn {#1} {#2} }
679 { \msg_error:nnnn { template } { unknown-instance } {#1} {#2} }
680 }
681 }
682 }
683 {
684 \@@_if_instance_exist:nnTF {#1}
685 { #2 : \l_context_primary_str
686 : \l_context_secondary_str }
687 { \@@_use_instance_auxiii:nn {#1}
688 { #2 : \l_context_primary_str : \l_context_secondary_str } }
689 {
690 \str_if_empty:NTF \l_context_primary_str
691 {
692 \@@_if_instance_exist:nnTF {#1} {#2}
693 { \@@_use_instance_auxiii:nn {#1} {#2} }
694 { \msg_error:nnnn { template } { unknown-instance } {#1} {#2} }
695 }
696 {
697 \@@_if_instance_exist:nnTF {#1}
698 { #2 : \l_context_primary_str }
699 { \@@_use_instance_auxiii:nn {#1}
700 { #2 : \l_context_primary_str } }
701 {
702 \@@_if_instance_exist:nnTF {#1} {#2}
703 { \@@_use_instance_auxiii:nn {#1} {#2} }
704 { \msg_error:nnnn { template } { unknown-instance } {#1} {#2} }
705 }
706 }
707 }
708 }
709 }
710 % \end{macrocode}
711 % \end{macro}
712 %
713 % \changes{2026-03-19}{v0.5c}{Move template debugging support to kernel}
714 %
715 % \begin{macrocode}
716 %</package>
717 % \end{macrocode}